Android 另一个片段中的谷歌地图-onLocationChanged之类的功能不起作用
我先前的问题得到了回答。google地图正在显示,getMapAsync正在运行 我现在正在寻找其他函数的解决方案,比如Android 另一个片段中的谷歌地图-onLocationChanged之类的功能不起作用,android,google-maps,android-fragments,android-mapview,android-gps,Android,Google Maps,Android Fragments,Android Mapview,Android Gps,我先前的问题得到了回答。google地图正在显示,getMapAsync正在运行 我现在正在寻找其他函数的解决方案,比如 public void onLocationChanged(Location location) {...} public void onConnected(Bundle bundle) {...} 注: 我正在尝试将google地图活动转换为在NavigationView中调用的片段中工作 samplemapFragment.java fragment\u sampl
public void onLocationChanged(Location location) {...}
public void onConnected(Bundle bundle) {...}
注:
我正在尝试将google地图活动转换为在NavigationView中调用的片段中工作
samplemapFragment.java
fragment\u sample\u map.xml
OnLocationChanged不是一个绑定到GoogleMap的方法,它实际上与LocationListener(或LocationClient)API相关。要获取OnLocationChanged的通知,您必须注册一个“侦听器”,它将轮询设备的位置,当它注册更改时,将触发OnLocationChanged方法 您可以在此处找到一个包含示例的全面指南:
祝你好运 我有一个有效的代码。它包括onLocationChanged函数。现在,我正在寻找如何使它在另一个片段中工作的方法,因为它将在NavigationView中调用,并且它导致大多数函数无法按其假设的方式工作。下面的代码在NavigationView中显示地图吗?若您想在location changelistener上获取位置,那个么可以将locationchange代码放在活动类中。
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);
}
}
<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>