2

我有我正在使用的 android 应用程序,Google Map API v1 key它运行良好。现在我需要将我的代码提供给我的教授,并在一台新的台式机上从头开始向他展示工作演示。所以这意味着,现在我不能再使用Google Map API v1 key了,这就是我需要对我拥有的代码进行一些更改Google Map API key v1以便它可以与Google Map API v2 key.

我正在尝试在top half of the android screen和上显示谷歌地图in my bottom half, I am trying to show the list view

下面是可以正常工作的代码,Google Map API v1 key现在我需要在下面的代码中进行更改以使其正常工作,Google Map API v2 key这意味着我需要在 android 屏幕的上半部分显示 Google Map。

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));
}

这是我应该修改的方法。如何修改以下方法以使其适用于 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 method我正在使用 app2 视图获取 mapView id,然后在其他地方使用它。如果我要使用,Google Map API v2 key那么我需要MapFragment按照我已经注释掉的下一行中提到的那样使用。现在我想知道如果我正在使用如何map idapp2 viewGoogle 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();

我正在寻找类似的东西。

4

2 回答 2

6

在你的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)您需要将 google-play-services_lib 导入您的工作区。2) 添加 android-support-v4.jar 3) 在您的开发者控制台中激活 android google maps v2 并获取 api 密钥。4)向清单添加以下权限

<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" />
于 2013-05-21T06:43:39.033 回答
0
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" />
于 2013-05-21T04:54:58.203 回答