简体   繁体   English

如何从Google Map v2的视图中获取片段

[英]How to get fragment from the view for Google Map v2

I have my android application which was using Google Map API v1 key and it was working fine. 我有使用Google Map API v1 key android应用程序,并且运行良好。 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 . 因此,现在我不能再使用Google Map API v1 key ,这就是我需要对Google Map API key v1的代码进行一些更改以便可以与Google Map API v2 key

I am trying to show google map on top half of the android screen and in my bottom half, I am trying to show the list view . 我试图top half of the android screen显示google map,而in my bottom half, I am trying to show the list view

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. 以下是与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));
}

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 如何修改以下方法,使其可与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);

}

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. 如果您在上面的initViewMembers method看到我正在使用app2视图获取mapView id,然后在其他地方使用它。 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. 如果要使用Google Map API v2 key则需要使用我已注释掉的下一行中提到的MapFragment Now I am wondering how can I get the map id from the app2 view if I am using Google Map API key v2 现在,我想知道如果我使用的是Google Map API key v2如何从app2 view获取map id

So my question is how to get the MapFragment thing from the view? 所以我的问题是如何从视图中获取MapFragment东西?

Below is my horz_scroll_app xml file which has fragment for Google Map v2 以下是我的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>

I cannot do something like this in my above initViewMembers method- 我无法在上面的initViewMembers方法中执行类似的操作-

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

I am looking for something similar. 我正在寻找类似的东西。

Add this in your xml 在您的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) you need to import google-play-services_lib to your workspace. 1)您需要将google-play-services_lib导入您的工作区。 2) add android-support-v4.jar 3) in you developer console activate android google maps v2 and get the api key. 2)添加android-support-v4.jar 3)在开发人员控制台中激活android google maps v2并获取api密钥。 4) add follwing permissions to manifest 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" />
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" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM