I know how to set location from coordinates and when the maps activity starts it will jump to those coordinates but i want the user to be able to set a marker on the map and save the coordinates into some variables but i have no idea how to do that. Kindly point me in the right direction.
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions()
.position(sydney)
.title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
Added some pics for full view of my code. I am sorry im really new into this maps stuff. Every comment will help me learn more about this.
Set up a global variable for your activity
private LatLng myLatLng;
Next, you need to set up a marker drag listener for your map
map.setOnMarkerDragListener(new GoogleMap.OnMarkerDragListener() {
@Override
public void onMarkerDragStart(Marker marker) {
}
@Override
public void onMarkerDrag(Marker marker) {
}
@Override
public void onMarkerDragEnd(Marker marker) {
myLatLng = marker.getPosition();
}
});
If you want to save the marker as a variable...
private Marker myMarker;
private LatLng myLatLng;
myMarker = mMap.addMarker(new MarkerOptions()
.position(sydney)
.title("Marker in Sydney"));
myLatLng = myMarker.getPosition();
只需将您的最后一行替换为
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(sydney, 15));
First add marker on map
private void setUpMap()
{
.......
googleMap.setOnMarkerClickListener(this);
myMarker = googleMap.addMarker(new MarkerOptions()
.position(latLng)
.title("My Spot")
.snippet("This is my spot!")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
......
}
then use this code to get marker location
@Override
public void onMarkerDragEnd(Marker marker) {
// TODO Auto-generated method stub
Toast.makeText(
MainActivity.this,
"Lat " + map.getMyLocation().getLatitude() + " "
+ "Long " + map.getMyLocation().getLongitude(),
Toast.LENGTH_LONG).show();
System.out.println("yalla b2a "
+ map.getMyLocation().getLatitude());
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.