谷歌地图在另一个片段 - 像onLocationChanged功能无法正常工作 [英] Google map inside another fragment - Function like onLocationChanged is not working

查看:251
本文介绍了谷歌地图在另一个片段 - 像onLocationChanged功能无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的previous问题<一href=\"http://stackoverflow.com/questions/35458584/google-map-inside-another-fragment-error-when-calling-getmapasync\">Google地图在另一个片段 - 错误时调用getMapAsync 得到了回答。在谷歌地图显示和getMapAsync现在工作。

My previous question Google map inside another fragment - Error when calling getMapAsync was answered. The google map is showing and the getMapAsync is now working.

而我现在找到解决其他功能,如

And I'm now finding the solution to the other functions like

public void onLocationChanged(Location location) {...}

public void onConnected(Bundle bundle) {...}

请注意:
我想转换一个谷歌地图的活动为将在NavigationView被称为片段内工作。

Note: I'm trying to convert a google map activity to work inside a fragment that will be called in NavigationView.

samplemapFragment.java

package  com.sample.samplemap;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.MapsInitializer;

public class samplemapFragment extends Fragment implements OnMapReadyCallback {
    GoogleMap mGoogleMap;
    SupportMapFragment mFragment;

    MapView mapView;

    View mView;

    public samplemapFragment() {
        Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MapsInitializer.initialize(getContext());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_track_travel, container, false);
        return mView;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        mapView = (MapView) mView.findViewById(R.id.map);

        if (mapView != null) {
            // Initialise the MapView
            mapView.onCreate(null);
            mapView.onResume();
            // Set the map ready callback to receive the GoogleMap object
            mapView.getMapAsync(this);
        }

    }


    @Override
    public void onMapReady(GoogleMap googleMap) {
        MapsInitializer.initialize(getContext());
        mGoogleMap = googleMap;
        mGoogleMap.setMyLocationEnabled(true);
    }
}

fragment_sample_map.xml

<RelativeLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
tools:context="com.sample.samplemap.samplemapFragment"
xmlns:tools="http://schemas.android.com/tools">
    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto" android:layout_width="384dp"
    android:layout_height="300dp" android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_marginTop="44dp"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true" /> 
</RelativeLayout>


推荐答案

OnLocationChanged是不依赖于一个使用GoogleMap的方法,它实际上是相关的LocationListener的(或LocationClient)的API。要获取OnLocationChanged通知,你必须注册一个监听器​​,这将轮询设备的位置,当它注册的改变,将触发OnLocationChanged方法。

OnLocationChanged is not a method tied to a GoogleMap, it is actually related to the LocationListener (or LocationClient) API. To get notifications for OnLocationChanged, you will have to register a 'Listener' which will poll the device's location and when it registers a change, will trigger the OnLocationChanged method.

您可以找到这里的例子一个COM prehensive指南:
http://developer.android.com/guide/topics/location/strategies.html

You can find a comprehensive guide with examples here: http://developer.android.com/guide/topics/location/strategies.html

祝你好运!

这篇关于谷歌地图在另一个片段 - 像onLocationChanged功能无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆