To get going with this tutorial I am using the code from my previous tutorial http://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.

Latitude and Longitude of the location that is touched on the Map.

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;
 }

}

}

About these ads