另一个片段内的谷歌地图 - 像 onLocationChanged 这样的功能不起作用
Posted
技术标签:
【中文标题】另一个片段内的谷歌地图 - 像 onLocationChanged 这样的功能不起作用【英文标题】:Google map inside another fragment - Function like onLocationChanged is not working 【发布时间】:2016-05-31 08:44:12 【问题描述】:我之前的问题Google map inside another fragment - Error when calling getMapAsync得到了解答。谷歌地图正在显示,getMapAsync 现在正在工作。
我现在正在寻找其他功能的解决方案,例如
public void onLocationChanged(Location location) ...
public void onConnected(Bundle bundle) ...
注意: 我正在尝试将谷歌地图活动转换为在 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_
android:layout_
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_
android:layout_ 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>
【问题讨论】:
这段代码是否在导航视图中显示地图?如果您想在位置更改侦听器上获取位置,那么您将位置更改代码放在活动类中。 【参考方案1】:OnLocationChanged 不是绑定到 GoogleMap 的方法,它实际上与 LocationListener(或 LocationClient)API 相关。要获取 OnLocationChanged 的通知,您必须注册一个“侦听器”,它将轮询设备的位置,并且当它注册更改时,将触发 OnLocationChanged 方法。
您可以在此处找到包含示例的综合指南: http://developer.android.com/guide/topics/location/strategies.html
祝你好运!
【讨论】:
我有一个有效的代码。它包括 onLocationChanged 函数。现在我找到了如何让它在另一个片段中工作的方法,因为它将在 NavigationView 中被调用,并且它导致大部分函数无法按预期工作以上是关于另一个片段内的谷歌地图 - 像 onLocationChanged 这样的功能不起作用的主要内容,如果未能解决你的问题,请参考以下文章
getSupportFragmentManager().findFragmentById 为 android 片段中的谷歌地图返回 null?