Getting touch coordinates of location on google map in android
I want to get the coordinates of the location that was touched and pass it to another action. Google map is showing in my activity, but its touch event is not working. I want to get the coordinates of the location that are touching and start another activity by passing those coordinates to another activity. Its code:
public class MapActivity extends Activity {
protected LocationManager locationManager;
String stlatitude = null, stlongitude = null;
private GoogleMap googleMap;
String locationName = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Bundle b = getIntent().getExtras();
stlatitude = b.getString("latitude");
stlongitude = b.getString("longitude");
locationName = b.getString("location_name");
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
try {
initilizeMap();
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
googleMap.setMyLocationEnabled(true);
MarkerOptions marker = new MarkerOptions().position(
new LatLng(Float.parseFloat(stlatitude), Float
.parseFloat(stlongitude))).title(locationName);
googleMap.addMarker(marker);
if (stlatitude != null || stlongitude != null) {
LatLng myLatLng = new LatLng(Float.parseFloat(stlatitude),
Float.parseFloat(stlongitude));
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
myLatLng, 12.0f));
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void onResume() {
super.onResume();
initilizeMap();
}
private void initilizeMap() {
if (googleMap == null) {
googleMap = ((MapFragment) getFragmentManager().findFragmentById(
R.id.map)).getMap();
// check if map is created successfully or not
if (googleMap == null) {
Toast.makeText(getApplicationContext(),
"Sorry! unable to create maps", Toast.LENGTH_SHORT)
.show();
}
}
}
public boolean onTouchEvent(MotionEvent event, MapView mapView)
{
//---when user lifts his finger---
if (event.getAction() == 1) {
GeoPoint p = mapView.getProjection().fromPixels(
(int) event.getX(),
(int) event.getY());
Toast.makeText(getBaseContext(),
p.getLatitudeE6() / 1E6 + "," +
p.getLongitudeE6() /1E6 ,
Toast.LENGTH_SHORT).show();
}
return false;
}
}
source to share
Google Maps provides an OnMapClickListener that accepts LatLng. First, set the onMapClickListener to your map and then make your stuff in the listener. From the top of my head, it should look something like
googleMap.setOnMapClickListener(new OnMapClickListener() {
@Override
public void onMapClick(LatLng point) {
Log.d("DEBUG","Map clicked [" + point.latitude + " / " + point.longitude + "]");
//Do your stuff with LatLng here
//Then pass LatLng to other activity
}
});
Place this in initilizeMap () after you have verified your googleMap is not null.
I am currently unable to test the code, but I'm sure it should look like this. Otherwise Google Docs about OnMapClickListener and LatLng should help.
Hope this helps you, let me know!
source to share