如何从谷歌地图V2的观点得到片段 [英] How to get fragment from the view for Google Map v2
问题描述
我有使用谷歌地图API v1密钥
,这是工作的罚款我的Android应用程序。现在,我需要给我的code到我的教授,并让他从头开始一个新的台式机上的工作演示。因此,这意味着,现在我不能使用谷歌地图API v1密钥
了,所以这是我需要做的code了一些变化,我对<$的原因C $ C>谷歌地图API V1 >键,以便它可以与。
I have my android application which was using Google Map API v1 key
and it was working fine. Now I need to give my code to my professor and show him the working demo from scratch on a new desktop machine. So that means, now I cannot use Google Map API v1 key
anymore so that is the reason I needed to make some changes in the code that I have for Google Map API key v1
so that it can be used with Google Map API v2 key
.
我试图显示在 Android的屏幕的上半部分
和在我的下半部分,我试图显示列表视图谷歌地图
。
下面是code这与谷歌地图API v1密钥正常工作
,现在我需要在以下code的变化,使其工作与谷歌地图API V2键
这意味着我需要显示谷歌地图在Android屏幕的上半部分。
Below is the code which works fine with Google Map API v1 key
and now I need to make changes in the below code to make it work with Google Map API v2 key
meaning I need to show Google Map on top half of the android screen.
private GoogleMap map;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = getIntent().getExtras();
user_id = bundle.getString("USERID");
LayoutInflater inflater = LayoutInflater.from(this);
scrollView = (MyHorizontalScrollView) inflater.inflate(R.layout.horz_scroll_with_list_menu, null);
setContentView(scrollView);
menu = inflater.inflate(R.layout.horz_scroll_menu, null);
app = inflater.inflate(R.layout.horz_scroll_app, null);
ViewGroup tabBar = (ViewGroup) app.findViewById(R.id.tabBar);
initViewMembers(app); // I need to modify something in this method only
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
initMyLocationOverview();
registerLocationListener();
Button button1 = (Button) menu.findViewById(R.id.button1);
Button button2 = (Button) menu.findViewById(R.id.button2);
useLastKnownLocation(locationManager);
final View[] children = new View[] { menu, app };
// Scroll to app (view[1]) when layout finished.
int scrollToViewIdx = 1;
scrollView.initViews(children, scrollToViewIdx,
new SizeCallbackForMenu(btnSlide));
}
这是我应该修改的方法。如何修改下面的方法,使其与谷歌地图V2工作
This is the method I am supposed to modify. How can I modify the below method so that it will work with Google Map V2
/**
* Initialize the map with default settings
*
*/
private void initViewMembers(View app2) {
mapView = (MapView) app2.findViewById(R.id.mapView);
//map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
listView = (ListView) app2.findViewById(R.id.mylist);
mapView.setBuiltInZoomControls(true);
mapController = mapView.getController();
mapController.setZoom(14);
mapView.setStreetView(true);
}
如果你在我的上方 initViewMembers方法请参见
我使用APP2视图来获取的MapView ID,然后在另一个地方使用它。如果我要使用谷歌地图API V2键
然后我需要使用 MapFragment
在我的下一行所提到的我已经注释掉。现在我想知道我怎么能获得地图编号
从 APP2视图
如果我使用谷歌地图API密钥V2
If you see in my above initViewMembers method
I am using app2 view to get the mapView id and then use it in another place. If I am going to use Google Map API v2 key
then I need to use MapFragment
as mentioned in my next line which I have commented out. Now I am wondering how can I get the map id
from the app2 view
if I am using Google Map API key v2
所以我的问题是如何从视图中获得MapFragment事?
So my question is how to get the MapFragment thing from the view?
下面是我的 horz_scroll_app xml文件
里面有片段为谷歌地图V2
Below is my horz_scroll_app xml file
which has fragment for Google Map v2
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/app"
android:layout_width="1dp"
android:layout_height="1dp"
android:layout_margin="2px"
android:orientation="vertical"
android:padding="2px" >
<LinearLayout
android:id="@+id/tabBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="@+id/BtnSlide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="0px"
android:padding="0px"
android:src="@drawable/button" />
</LinearLayout>
<!--
<com.google.android.maps.MapView
android:id="@+id/mapView"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:apiKey="0s_fADEBtq0-j_teQ1j-yaoDAivoHHtwN81rJ-g"
android:clickable="true"
android:state_enabled="true" />
-->
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment" />
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="0.8"
android:orientation="horizontal" >
<ListView
android:id="@+id/mylist"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" >
</ListView>
</LinearLayout>
</LinearLayout>
我不能做这样的事情在我的上面initViewMembers方法 -
I cannot do something like this in my above initViewMembers method-
地图=((MapFragment)app2.findFragmentById(R.id.map))的GetMap();
我要寻找类似的东西。
推荐答案
在您的XML添加此
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"/>
这是如何可以获取对象引用
This is how you can get the object reference
GoogleMap mMap = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map)).getMap();
1),则需要导入谷歌播放-services_lib到工作区。
2)添加Android的支持,v4.jar
3)你开发者控制台激活的Android谷歌地图V2并获得API密钥。
4)添加follwing权限来体现
1) you need to import google-play-services_lib to your workspace. 2) add android-support-v4.jar 3) in you developer console activate android google maps v2 and get the api key. 4) add follwing permissions to manifest
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<!--
The following two permissions are not required to use
Google Maps Android API v2, but are recommended.
-->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<permission
android:name="YOURPACKAGE.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="YOURPACKAGE.permission.MAPS_RECEIVE" />
这篇关于如何从谷歌地图V2的观点得到片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!