我在片段内部实现谷歌地图视图,一切正常但Marker没有进入其固定的新位置。我尝试了长按,听众,点击地图的其他部分,没有任何作用。
MapView片段
public class MapLocation_Fragment extends Fragment implements GoogleMap.OnMarkerDragListener,GoogleMap.OnMarkerClickListener,OnMapReadyCallback {
MapView mapView;
GoogleMap google_Map;
Marker Map_marker;
LatLng locclPosition;
double lati,longi;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.maplocation_fragment, container, false);
mapView = (MapView) view.findViewById(R.id.send_Location_MapView);
mapView.onCreate(savedInstanceState);
mapView.onResume();// Needed tog et the map to display immediately
lati=getArguments().getDouble("lat");
longi=getArguments().getDouble("longi");
locclPosition=new LatLng(lati,longi);
MapsInitializer.initialize(getActivity());
mapView.getMapAsync(this);
return view;
}
@Override
public void onResume() {
super.onResume();
mapView.onResume();
}
@Override
public void onPause() {
super.onPause();
mapView.onPause();
}
@Override
public void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}
@Override
public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}
@Override
public void onMarkerDragStart(Marker marker) {
Map_marker=marker;
}
@Override
public void onMarkerDrag(Marker marker) {
}
@Override
public void onMarkerDragEnd(Marker marker) {
}
@Override
public boolean onMarkerClick(Marker marker) {
return false;
}
@Override
public void onMapReady(GoogleMap googleMap) {
google_Map = googleMap;
if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
google_Map.setOnMarkerClickListener(MapLocation_Fragment.this);
google_Map.setOnMarkerDragListener(MapLocation_Fragment.this);
google_Map.setTrafficEnabled(true);
google_Map.setBuildingsEnabled(true);
google_Map.setMapType(googleMap.MAP_TYPE_NORMAL); //THIS OPTION WILL SHOW MAP IN SATELLITE FORM
google_Map.addMarker(new MarkerOptions()
.position(locclPosition)
.title("Current Location")
.draggable(true));
CameraPosition cameraPosition=new CameraPosition.Builder().target(locclPosition).zoom(19).build();
google_Map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
}
我需要标记移动到用户点击地图的地方,但经过大量的努力,它仍然根本没有移动。请帮帮我们。
答案 0 :(得分:0)
要在拖动标记时移动标记,您需要在onMarkerDragEnd
方法中编写一些代码,基本上是将标记的位置(locclPosition
)更新为拖动结束的位置(新坐标)
@Override
public void onMarkerDragEnd(Marker marker) {
locclPosition = marker.getPosition();
}