我想向用户显示添加到地图上的标记的标题,但是我在网络上找到的解决方案都没有提供任何帮助。不幸的是,showInfoWindow或selectMarker都没有帮助:(
请在下面查看我的代码:
public class DetailAnimalPlantMapFragment extends Fragment implements OnMapReadyCallback, PermissionsListener {
private MapView mapView;
private MapboxMap mapboxMapMyLocation;
PermissionsManager permissionsManager;
List<Point> positions;
LatLng[] latLng;
LatLng myLocation;
@Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Mapbox.getInstance(getActivity(), getString(R.string.map_box_api));
View rootView = inflater.inflate(R.layout.fragment_ustka_object_map_mapbox, container,
false);
mapView = rootView.findViewById(R.id.mapViewMapBox);
Bundle bundle = getArguments();
Nature nature = (Nature) bundle.getSerializable("nature");
final double lat = nature.getLat();
final double lng = nature.getLng();
final String name = nature.getName();
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(MapboxMap mapboxMap) {
if(!nature.getWaypoints().isEmpty()){
positions = PolylineUtils.decode(nature.getWaypoints(),5);
latLng = new LatLng[positions.size()];
for (int i = 0; i < positions.size(); i++) {
latLng[i] = new LatLng(
positions.get(i).latitude(),
positions.get(i).longitude());
}
mapboxMap.addMarker(new MarkerOptions()
.setPosition(new LatLng(latLng[0]
.getLatitude(),latLng[0]
.getLongitude()))
.setTitle("Początek"));
mapboxMap.addMarker(new MarkerOptions()
.setPosition(new LatLng(latLng[latLng.length-1]
.getLatitude(),latLng[latLng.length-1]
.getLongitude()))
.setTitle("Koniec"));
mapboxMap.addPolyline(new PolylineOptions()
.add(latLng)
.color(Color.parseColor("#38afea"))
.width(5));
}
mapboxMapMyLocation = mapboxMap;
enableLocationPlugin();
CameraPosition position = new CameraPosition.Builder()
.target(new LatLng(lat,lng))
.zoom(11)
.tilt(30)
.build();
mapboxMapMyLocation.animateCamera(CameraUpdateFactory
.newCameraPosition(position), 4000);
Marker marker = mapboxMapMyLocation.addMarker(new MarkerOptions().setPosition(new LatLng(lat,lng))
.setTitle(name));
mapboxMapMyLocation.selectMarker(marker);
/*marker.showInfoWindow(mapboxMapMyLocation,mapView);*/
}
});
return rootView;
}
@Override
@SuppressWarnings( {"MissingPermission"})
public void onStart() {
super.onStart();
mapView.onStart();
}
@Override
public void onResume() {
super.onResume();
mapView.onResume();
}
@Override
public void onPause() {
super.onPause();
mapView.onPause();
}
@Override
public void onStop() {
super.onStop();
mapView.onStop();
}
@Override
public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}
@Override
public void onDestroyView() {
super.onDestroyView();
mapView.onDestroy();
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}
@Override
public void onMapReady(MapboxMap mapboxMap) {
DetailAnimalPlantMapFragment.this.mapboxMapMyLocation = mapboxMap;
enableLocationPlugin();
}
@SuppressWarnings( {"MissingPermission"})
private void enableLocationPlugin() {
// Check if permissions are enabled and if not request
if (PermissionsManager.areLocationPermissionsGranted(getActivity())) {
LocationLayerPlugin locationLayerPlugin = new LocationLayerPlugin(mapView, mapboxMapMyLocation);
// Set the plugin's camera mode
Location location = locationLayerPlugin.getLastKnownLocation();
myLocation = new LatLng(location.getLatitude(),location.getLongitude());
locationLayerPlugin.setCameraMode(CameraMode.TRACKING_GPS);
getLifecycle().addObserver(locationLayerPlugin);
} else {
permissionsManager = new PermissionsManager(this);
permissionsManager.requestLocationPermissions(getActivity());
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
}
@Override
public void onExplanationNeeded(List<String> permissionsToExplain) {
}
@Override
public void onPermissionResult(boolean granted) {
}
}
如您所见,我正在打开一个片段的mapView,同时指向用户的位置和对象的位置。