Android 谷歌地图我得到当前位置时,我改变位置标记不移动谷歌地图
这是我的密码:Android 谷歌地图我得到当前位置时,我改变位置标记不移动谷歌地图,android,Android,这是我的密码: public class MapViewFragment extends Fragment implements OnMapReadyCallback { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflat
public class MapViewFragment extends Fragment implements OnMapReadyCallback {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.maps, container, false);
Context context =this.getContext();
GPSService mGPSService = new GPSService(context);
mGPSService.getLocation();
if (mGPSService.isLocationAvailable == false) {
} else {
// Getting location co-ordinates
lat1 = mGPSService.getLatitude();
lng1 = mGPSService.getLongitude();
}
mMapView.onCreate(savedInstanceState);
ActivityCompat.requestPermissions(getActivity(),new String[]{
android.Manifest.permission.ACCESS_FINE_LOCATION}, 1);
call.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
call();
}
});
mMapView.getMapAsync(this); //this is important
}
});*/
}
return v;
}
@Override
public void onMapReady(GoogleMap googleMap) {
mGoogleMap = googleMap;
mGoogleMap.getUiSettings().setZoomControlsEnabled(true);
m1 = googleMap.addMarker(new MarkerOptions()
.position(new LatLng(lat1,lng1))
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE))
.anchor(0.5f, 0.5f)
.title("Title1")
.snippet("Snippet1"));
@Override
public void onResume() {
super.onResume();
mMapView.onResume();
}
@Override
public void onPause() {
super.onPause();
mMapView.onPause();
}
@Override
public void onDestroy() {
super.onDestroy();
mMapView.onDestroy();
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mMapView.onSaveInstanceState(outState);
}
@Override
public void onLowMemory() {
super.onLowMemory();
mMapView.onLowMemory();
}
}
在程序中,我得到当前位置lat和lang。当我移动到其他位置时,标记不会改变。无论何时我移动,位置都必须改变。
每次移动位置值时,我都会得到当前位置值。是否要执行任何方法来更改位置。我假设您已经创建了一个标记来指示当前位置。这样地 标记当前位置 现在需要实现LocationListener并在onLocationChanged回调中更新MapView
@Override
public void onLocationChanged(Location location) {
if(currentLocation != null){
currentLocation.remove();
}
TextView tvLocation = (TextView) findViewById(R.id.tv_location);
// Getting latitude of the current location
double latitude = location.getLatitude();
// Getting longitude of the current location
double longitude = location.getLongitude();
// Creating a LatLng object for the current location
LatLng latLng = new LatLng(latitude, longitude);
currentLocation = googleMap.addMarker(new MarkerOptions().position(latLng)));
// Showing the current location in Google Map
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
// Zoom in the Google Map
googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));
}
我假设您已经创建了一个标记来指示您当前的位置。这样地 标记当前位置 现在需要实现LocationListener并在onLocationChanged回调中更新MapView
@Override
public void onLocationChanged(Location location) {
if(currentLocation != null){
currentLocation.remove();
}
TextView tvLocation = (TextView) findViewById(R.id.tv_location);
// Getting latitude of the current location
double latitude = location.getLatitude();
// Getting longitude of the current location
double longitude = location.getLongitude();
// Creating a LatLng object for the current location
LatLng latLng = new LatLng(latitude, longitude);
currentLocation = googleMap.addMarker(new MarkerOptions().position(latLng)));
// Showing the current location in Google Map
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
// Zoom in the Google Map
googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));
}
移动到其他位置你的意思是你实际上移动并查看该位置?或者你的意思是使用mapview移动到其他位置?我可以从代码中获取当前位置。如何连续移动与谷歌地图上谷歌默认当前位置标记相同的标记androidThank感谢您的响应移动到其他位置您的意思是您实际移动并查看该位置?或者你的意思是使用mapview移动到其他位置?我可以从代码中获取当前位置。如何连续移动与google地图上google默认当前位置标记相同的标记Android感谢您的回复它对我不起作用。默认google地图在fragment not activity类中从当前位置移动。许多示例都在activity中。像这样,我想创建应用程序请帮助我是的,你在mapfragment所有你要做的就是链接一个侦听器。键入mMapView并在此处设置位置更改侦听器。。。像mMapView.OnMyLocationChangeListener(这个);实现了OnMapReadyCallback、OnMyLocationChangeListener,我给了您一个需要重写的完整方法。很快它就不适用于我了。默认google地图在fragment而非activity类中从当前位置移动。许多示例都在activity中。像这样,我想创建应用程序请帮助我是的,你在mapfragment所有你要做的就是链接一个侦听器。键入mMapView并在此处设置位置更改侦听器。。。像mMapView.OnMyLocationChangeListener(这个);实现了OnMapReadyCallback、OnMyLocationChangeListener,我给了您一个需要重写的完整方法。