我有一个片段,当正常启动时会显示一个mapview,当前用户位置显示为蓝点标记。我有一个功能,可以从其他地方启动此屏幕,并可以传递纬度和经度值。根据这些值,我更新“MapboxMap”的纬度和经度。该位置在地图上更新,相机缩放到该位置,但蓝点标记仍显示为用户当前位置。如何更新蓝点标记以显示为纬度和经度值给出的新位置:
我的代码更新位置:
private void goToUserLocation() {
double requestedLatitude = 0;
double requestedLongitude = 0;
if (mapboxMap == null || !getUserVisibleHint()) {
return;
}
if (!DeviceUtil.isLocationServiceEnabled(getContext().getApplicationContext())) {
showLocationDisabledSnackbar();
return;
}
enableUserLocationMarker();
Location location = mapboxMap.getMyLocation();
//Get the requested coordinates if it exists
if(location != null) {
try {
Uri intentData = getActivity().getIntent().getData();
if (null != intentData) {
String latitude = intentData.getQueryParameter("lat");
String longitude = intentData.getQueryParameter("lon");
if (!TextUtils.isEmpty(latitude) && !TextUtils.isEmpty(longitude)) {
requestedLatitude = Double.parseDouble(latitude);
requestedLongitude = Double.parseDouble(longitude);
if ((requestedLatitude >= -90 && requestedLatitude <= 90) &&
(requestedLongitude >= -90d && requestedLongitude <= 90d)) {
location.setLatitude(requestedLatitude);
location.setLongitude(requestedLongitude);
}
}
}
}catch (NumberFormatException ex) {
}
finally {
goToLocation(location);
}
}
fetchNearbyPages();
}
感谢任何帮助。
答案 0 :(得分:0)
希望这会有所帮助。
首先,覆盖onLocatioChanged方法并将更新的位置传递给单独创建的setCameraToCurrentPosition方法。恩。如下: -
public void onLocationChanged(Location location) {
setCameraToCurrentPosition(Location location);
}
private void setCameraToCurrentPosition(Location location) {
setCameraPosition(new LatLng(location.getLatitude(), location.getLongitude()), 16, 6500);
// Here you need to add the marker to current position of user on map.
}