如何从谷歌地图v2的视图中获取片段

问题描述:

我有我的android应用程序正在使用Google Map API v1 key,它工作正常。现在我需要将我的代码交给我的教授,并在新的台式机上向他展示从头开始的工作演示。这意味着,现在我不能再使用Google Map API v1 key了,所以这就是我需要对Google Map API key v1的代码进行一些更改的原因,以便它可以与Google Map API v2 key一起使用。如何从谷歌地图v2的视图中获取片段

我想在top half of the android screenin my bottom half, I am trying to show the list view上显示谷歌地图。

以下代码适用于Google Map API v1 key,现在我需要对下面的代码进行更改以使其与Google Map API v2 key配合使用,这意味着我需要在Android屏幕的上半部分显示Google地图。

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如果你在我上面initViewMembers method看到我使用APP 2期得到的MapView ID,然后在另一个地方使用它的工作

/** 
* 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); 

} 

。如果我打算使用Google Map API v2 key,那么我需要使用MapFragment,正如我在下一行中提到的那样,我已经注释到了这一点。现在我想知道我怎样才能从app2 viewmap id如果我使用Google Map API key v2

所以我的问题是如何从视图中MapFragment的事情吗?

下面是我horz_scroll_app xml file这对于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方法 -

map = ((MapFragment) app2.findFragmentById(R.id.map)).getMap();

我找类似的东西。

+2

“如果我要使用谷歌地图API第2键,然后我需要使用MapFragment” - 没有。 Maps V2拥有自己的'MapView'小部件,您可以直接使用它,而不是通过'MapFragment'。 – CommonsWare

+0

你能告诉我一个例子,将与我的上述情况,因为它看起来像谷歌地图API V2非常混乱。非常感谢您的帮助。 – ferhan

添加这在XML

<fragment 
     android:id="@+id/map" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     class="com.google.android.gms.maps.SupportMapFragment"/> 

这是你如何能得到的对象引用

GoogleMap mMap = ((SupportMapFragment) getSupportFragmentManager() 
       .findFragmentById(R.id.map)).getMap(); 

1)您需要导入谷歌播放,services_lib到您的工作空间。 2)添加android-support-v4.jar 3)在您的开发者控制台中激活android google maps v2并获取api密钥。 4)添加follwing权限体现

<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" /> 

I did like this.. 


public class CustomSupportMapFragment extends SupportMapFragment { 

    private GoogleMap map; 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup view, 
      Bundle savedInstance) { 
     View layout = super.onCreateView(inflater, view, savedInstance); 

     FrameLayout frameLayout = new FrameLayout(getActivity()); 
     frameLayout.setBackgroundColor(getResources().getColor(
       android.R.color.transparent)); 
     ((ViewGroup) layout).addView(frameLayout, new ViewGroup.LayoutParams(
       LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); 
     map=getMap(); 

     return layout; 
    } 

    /*public static CustomSupportMapFragment newInstance(GoogleMapOptions op) { 

     CustomSupportMapFragment frag = new CustomSupportMapFragment(); 

     return frag; 
    } 
*/ 
    public GoogleMap getMapInstance() { 
     // TODO Auto-generated method stub 

     return map; 
    } 

} 


xml.. 


    <fragment 
    android:id="@+id/map" 
    android:layout_width="match_parent" 
    android:layout_height="300dp" 
    class="com.maps.utils.CustomSupportMapFragment" /> 
+0

我已经知道这件事了。但他希望以适当的方式从头开始看,以便如果有其​​他学生要参与同一项目,那么他应该能够解释它们。所以安装旧apk不是解决方案。 – ferhan

+0

检查此链接http://www.vogella.com/articles/AndroidGoogleMaps/article.html – MohsinSyd

+0

我只是跟随该链接。我猜你没有正确阅读我的问题。我说的是,在我的XML文件中,我使用的是Google Map v1的MapView,它工作正常。现在我注释了'MapView'和'Google Map V2'的片段,所以这意味着我需要修改我的initviewmember方法以进行更改,我不知道该怎么做。从视图app2,我不知道如何获得'MapFragment' – ferhan