You are currently browsing the tag archive for the ‘Coordinates’ tag.

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.

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

}

}

In this tutorial we will make our way to get Coordinates from GPS. We will use the emulator, will learn how to use the emulator and how to use the DDMS and how we can send coordinates from DDMS to our emulator. We will start with a very basic application that will get coordinates from the GPS and will display it in a Toast. In the second part of this tutorial we will use those coordinates and will make our Google Maps animate to that location automatically. For using Google Maps Basics follow the link

https://mirnauman.wordpress.com/2012/01/30/using-google-maps-in-android-development-tutorial-part-1/

We will continue with the code in the above example, using the same API and AVD. First of all add the following permission in the AndroidManifest.xml file, if its not been already added.

<uses-permission android:name="android.permission.INTERNET"/>
 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

Secondly check the AVD that is it with GPS support or not. As we can see in the hardware section that “GPS Support” has a yes infront of it. Means GPS Support is installed.

Creating & Configuring New AVD

There will be no change in the “main.xml” file. Now open the “GoogleMapsActivity.java” file. Add the following to the import section of the file. At the top.

import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;

Create a LocationManager and LocationListner objects in our main “GoogleMapsActivity” Class.

LocationManager locMgr;
 MyLocationListener locLstnr;

Now Create a whole new class by the name of MyLocationListner inside the GoogleMapsActivity class.

 public class MyLocationListener implements LocationListener
 {
 @Override
 public void onLocationChanged(Location loc)
 {
 loc.getLatitude();
 loc.getLongitude();
 String Text = "My current location is: " +
 "Latitud = " + loc.getLatitude() +
 "Longitud = " + loc.getLongitude();
 Toast.makeText( getApplicationContext(), Text, Toast.LENGTH_SHORT).show();

 }

 @Override
 public void onProviderDisabled(String provider)
 {
 Toast.makeText( getApplicationContext(),
 "Gps Disabled",
 Toast.LENGTH_SHORT ).show();
 }

 @Override
 public void onProviderEnabled(String provider)
 {
 Toast.makeText( getApplicationContext(),
 "Gps Enabled",
 Toast.LENGTH_SHORT).show();
 }

 @Override
 public void onStatusChanged(String provider, int status, Bundle extras)
 {

 }

 }

Now in the GoogleMapsActivity class add the following lines of code to create the LocationManager object and MyLocationListner object.

locMgr = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
locLstnr = new MyLocationListener();
locMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locLstnr);

Check the picture below. In the image below will have used DDMS to send dummy coordinates to our emulator. We have to keep a few points in mind when sending dummy coordinates to emulator.

Toast displaying our dummy coordinates sent from DDMS to our emulator.

First of all emulator should be select in the Devices Tab in the DDMS. In the above picture my device is shown as emulator-5554. Secondly while the device selected. Open the Emulator Control Tab, Inside that tab open the Manual Tab and scroll down to location controls. Now click send and you will see a toast appear on our emulator displaying our message including coordinates that we have just sent from DDMS. With this step our first phase of the tutorial is complete. To animate the GoogleMap to the location of the Coordinates that we have received. Add the following code in the onLocationChanged method of our MyLocationListner Class

String coordinates[] = {""+loc.getLatitude(), ""+loc.getLongitude()};
 double lat = Double.parseDouble(coordinates[0]);
 double lng = Double.parseDouble(coordinates[1]);

 GeoPoint p = new GeoPoint(
 (int) (lat * 1E6),
 (int) (lng * 1E6));

mc.animateTo(p);
 mc.setZoom(7);
 mapView.invalidate();

Comment the code that displays the Toast and run the Project. Send dummy locations from DDMS and we will see that the Map will animate to the location of the coordinates that we have provided from DDMS.

Note:- Please leave your comments if this article was helpful.

Next tutorial on its way ( adding images to GoogleMaps, OverLays )

Blog Stats

  • 325,597 hits

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 233 other followers

%d bloggers like this: