To get going with this tutorial I am using the code from my previous tutorial https://mirnauman.wordpress.com/2012/04/10/android-google-maps-tutorial-part-5-adding-multiple-images-to-google-maps-using-itemizedoverlay/
Objective:-
To get the Latitude and Longitude values of a location that is touched on Google Maps in Android.
Solution:-
All we need is a simple function onTouchEvent(MotionEvent event, MapView mapView), that can be added to either a Class that extends Overlay or a Class that extends ItemizedOverlay. In my tutorial I have used ItemizedOverlay so I ll add the function to my class “MirItemizedOverlay” that extends ItemizedOverlay.
Function to be added:-
@Override 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; }
Now when we Run the Application and touch the sceen. As soon as we lift the touch, the application will display the Latitude and Longitude values in a Toast.
Complete code of tutorial Google Maps Part 5 will become
package com.android.googlemapsapp; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.graphics.drawable.Drawable; import android.os.Bundle; import com.google.android.maps.GeoPoint; import com.google.android.maps.ItemizedOverlay; import com.google.android.maps.MapActivity; import com.google.android.maps.MapController; import com.google.android.maps.MapView; import com.google.android.maps.MyLocationOverlay; import com.google.android.maps.OverlayItem; import android.os.Bundle; import android.view.MotionEvent; import android.widget.Toast; public class GoogleMapsAppActivity extends MapActivity { private MapView mapView; private MapController mc; //MyLocationOverlay myLocOverlay = null; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mapView = (MapView) findViewById(R.id.mapview1); mc = mapView.getController(); mapView.setBuiltInZoomControls(true); Drawable makerDefault = this.getResources().getDrawable(R.drawable.star_big_on); MirItemizedOverlay itemizedOverlay = new MirItemizedOverlay(makerDefault); GeoPoint point = new GeoPoint(33480000, 73000000); OverlayItem overlayItem = new OverlayItem(point, "Islamabad", null); itemizedOverlay.addOverlayItem(33695043, 73000000, "Islamabad"); itemizedOverlay.addOverlayItem(33480000, 73000000, "Some Other Pakistani City"); itemizedOverlay.addOverlayItem(33380000, 73000000, "Some Other Pakistani City"); mapView.getOverlays().add(itemizedOverlay); MapController mc = mapView.getController(); mc.setCenter(new GeoPoint(33580000, 73000000)); // Some where near Islamabad. mc.zoomToSpan(itemizedOverlay.getLatSpanE6(), itemizedOverlay.getLonSpanE6()); } @Override protected boolean isRouteDisplayed() { // TODO Auto-generated method stub return false; } private class MirItemizedOverlay extends ItemizedOverlay<OverlayItem> { private List<OverlayItem> mOverlays = new ArrayList<OverlayItem>(); public MirItemizedOverlay(Drawable defaultMarker) { super(boundCenterBottom(defaultMarker)); // TODO Auto-generated constructor stub } @Override protected OverlayItem createItem(int i) { return mOverlays.get(i); } @Override public int size() { return mOverlays.size(); } public void addOverlayItem(OverlayItem overlayItem) { mOverlays.add(overlayItem); populate(); } public void addOverlayItem(int lat, int lon, String title) { GeoPoint point = new GeoPoint(lat, lon); OverlayItem overlayItem = new OverlayItem(point, title, null); addOverlayItem(overlayItem); } @Override 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; } } }
Related articles
- Android Google Maps Tutorial Part 5, Adding Multiple Images To Google Maps Using ItemizedOverlay. (mirnauman.wordpress.com)
- Android Google Maps Tutorial Part 3. Adding An Image to GoogleMaps Using Map Overlays. (mirnauman.wordpress.com)
- Android Google Maps Tutorial Part 2. Using GPS in Android and Animating Google Maps to the Current GPS location. (mirnauman.wordpress.com)
- Android Google Maps Tutorial Part 1. Basic Development. (mirnauman.wordpress.com)
19 comments
Comments feed for this article
April 17, 2012 at 7:21 am
Android Google Maps Tutorial Part 4, Adding Menu & Some Additional Functionality Like Zooming, Changing Map View, Animating To GPS Current Location Using Menu Button. « Mir
[…] Android Google Maps Tutorial Part 6, Getting The Location That Is Touched. (mirnauman.wordpress.com) […]
April 18, 2012 at 9:58 am
iman
hello….
thank you for helping us in the previous part .
now we are hoping that you help us solving this problem…….well , want to inclode a GPS tracker so that a line will be drawn between our location and the desired one.
April 18, 2012 at 10:24 am
Mir
thanks for appreciating my efforts. am already working on drawing polygons using set of coordinates from kml files. guess i have to start with a single line between two points. that might be more helpful than the complex polygons.
April 26, 2012 at 9:24 am
Iman
hello… could you plz keep us up to date with your progress about the GPS tracker…….and thank you very much.
April 26, 2012 at 11:45 am
Mir
Iman,
Plz check the following link
April 27, 2012 at 4:34 am
Android Google Maps Tutorial Part 3. Adding An Image to GoogleMaps Using Map Overlays. « Mir
[…] Android Google Maps Tutorial Part 6, Getting The Location That Is Touched. (mirnauman.wordpress.com) […]
May 9, 2012 at 3:57 am
Mir
Reblogged this on Mir.
May 11, 2012 at 9:26 pm
andrias
Hi Mir i have a question is can you help me with this i want when i touch one of the stars in the map to open an information viewer for example information
about Islamabad
July 23, 2012 at 9:54 am
bang
you can use method protect boolean onTap(int index){
return super.onTap(index);
}
// show index of item you select
July 31, 2012 at 5:23 pm
asharekh
i have developed an app for revers geocoding and i’m getting this error “over_query_limit”
what do you think the reason is?
October 9, 2012 at 11:55 am
kumar
hello, if some places which i touched are to be retained after closing the application also, what should i do(what mechanism should i use to store the places ) ? thank u so much in advance..
October 10, 2012 at 5:13 am
Mir
for storing places u can use SQLite DB, or u can save all the places in a simple text file comma separated. later u can parse the string in that text file and get each place to that u have accessed. storing values in XML is also a solution but u will need to parse it to get values from it.
November 21, 2012 at 12:55 pm
rup
how to calculate distance between two geopoint?where to save the latitude and longitude of that geopoints?
December 3, 2012 at 12:36 pm
Vishal
hats of to u sir
January 4, 2013 at 2:30 pm
Drawing icon on map where it is touched : Android Community - For Application Development
[…] Method and pass it the lat and long and you will get Image on the touched location if you need some example u can see here […]
March 3, 2013 at 5:25 pm
Drawing A Path or Line Between Two Locations » Muhammad Yusro
[…] Android Google Maps Tutorial Part 6, Getting The Location That Is Touched.(mirnauman.wordpress.com) […]
December 30, 2013 at 12:17 pm
Android touch | Firefly's space
[…] try to use itemized overlay to draw image and to draw at certain point override the OntouchEvent Method and pass it the lat and long and you will get Image on the touched location if you need some example u can see here […]
October 14, 2014 at 7:17 pm
srishti
can i get the total code for the google maps app in zip file
October 15, 2014 at 6:52 am
Mir
Dear Srishti,
sorry to say but its an old tutorial and i have changed several machines. I ll check my old backups, if i find the complete code, i ll upload it.
Thanks for reading my articles.