You are currently browsing the category archive for the ‘Android Google Maps API Ver 1.0’ category.

Preface:

One of readers asked me to write about getting the names of the location that I touch on the map if it is possible to get names from coordinates. That query triggered me to write this article. The answer is yes we can get location names if we have coordinates, the process is called Geocoding.

Geocoding is the process in which we translate latitude and longitude sets to text based address or string location names. Like if we have the coordinates 33695043,73050000 and we want to know which location is it. We will use geocoding and will find out that its Islamabad, the capital of Pakistan. Same is the case with Reverse Geocoding, if we have the name Islamabad, we can Reverse Geocode it to fine the coordinates. To continue with this article I have used the code from my last tutorial i.e, https://mirnauman.wordpress.com/2012/04/26/android-google-maps-tutorial-part-7-drawing-a-path-or-line-between-two-locations/ but u guys don’t need to use the code from that tutorial. You can use ur current google maps app and just insert the OnTouchEvent method the YourOverlay Class that extends Overlay Class

A lil boring theory:

We will use the Geocoder Class. This class is used for Geocoding and Reverse Geocoding. Now in Reverse Geocoding things can be a lil tricky coz the returned latitude, longitude values depend on the string address details that a user has provided. It may be just a city name, a city name with a street name or a city name with street name and name of a building or specific location. The Geocoder class requires a backend service that is not included in the core android framework. The Geocoder query methods will return an empty list if there no backend service in the platform. The major public methods of this class are getFromLocation() and getFromLocationName(). The earlier one will transform lat,long values to string location names and the later one will get the coordinates from string location names. both the methods will return an array of address i.e, List< Addresses >.

Enough theory, now some real stuff

Add the following code to the class that extends Overlay Class


@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();

 Geocoder geoCoder = new Geocoder(getBaseContext(), Locale.getDefault());
 try {
 List<Address> addresses = geoCoder.getFromLocation(p.getLatitudeE6() / 1E6, p.getLongitudeE6() / 1E6, 1);

 String strCompleteAddress= "";
 if (addresses.size() > 0)
 {
 for (int i=0; i<addresses.get(0).getMaxAddressLineIndex();i++)
strCompleteAddress+= addresses.get(0).getAddressLine(i) + "\n";
 }
 Log.i("MyLocTAG => ", strCompleteAddress);
 Toast.makeText(getBaseContext(), strCompleteAddress, Toast.LENGTH_LONG).show();
 }
 catch (IOException e) {
 Log.i("MyLocTAG => ", "this is the exception part");
 e.printStackTrace();
 }
 return true;
 }
 else
 return false;
 }

The first Toast will show only the latitude and longitude in number for the location that is touched. Then we created an object of the Geocoder class.


Geocoder geoCoder = new Geocoder(getBaseContext(), Locale.getDefault());

Than we called the getFromLocation method, provided it with the latitude and longitude values and the number of search results that will be returned.


List<Address> addresses = geoCoder.getFromLocation(p.getLatitudeE6()  / 1E6,  p.getLongitudeE6() / 1E6, 1);

After this we concatenate each value in the Addresses array list to get a single string with the complete location address.


for (int i=0; i<addresses.get(0).getMaxAddressLineIndex();i++)
 strCompleteAddress += addresses.get(0).getAddressLine(i) + "\n";

The last part displays a Toast that will show the string location name.


Toast.makeText(getBaseContext(), strCompleteAddress, Toast.LENGTH_LONG).show();

Geocoding snapshot showing the complete string location name that is touched on the map

Same is the case with Reverse Geocoding, if we have the string location name we can get the coordinates to that location by using Reverse Geocoding. The sample code for Reverse Geocoding is attached below.


Geocoder geoCoder = new Geocoder(this, Locale.getDefault());
 try {
 List<Address> addresses = geoCoder.getFromLocationName("Bhattai Rd Islamabad", 5);
 String strCompleteAddress = "";
 if (addresses.size() > 0) {
 p = new GeoPoint(
 (int) (addresses.get(0).getLatitude() * 1E6),
 (int) (addresses.get(0).getLongitude() * 1E6));
 mc.animateTo(p);
 mapView.invalidate();
 }
 } catch (IOException e) {
 e.printStackTrace();
 }

Mir

I have been trying this for a month and a lot of my readers asked to write a tutorial that makes it easy to draw a line on Google Maps for Android between two locations. I have gone through a lot of material. Most of the articles on this exact issue are not exactly addressing this specific issue. So their code is way too complex for this simple task. I have tried to make this as simple as possible.

Assumption About The Reader:

At this point I assume that reader of this article have already configured everything and Google Maps are displayed in their emulator properly. For those who don’t have Google Maps properly displayed in their emulator, they can read my earlier articles. Links provided at the end of this article.

Objective:

Draw a line that between two points on Google Maps lets say City “A” and City “B”. Repeat…

View original post 464 more words

Mir

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:-

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

View original post 38 more words

Mir

Dear readers am back after a long time trying to complete what i started a few months back. This article is part 5 of the previous series but it doesn’t depend on the code of the previous articles, still they can be referenced to get a better understanding of configuring Google Maps for Android and learning the basic development of Google Maps for Android. The development environment stays the same as of the previous articles. No change in any of the configurations etc.

Please see the links for previous articles for basics of Google Maps for Android.

  1. https://mirnauman.wordpress.com/2012/01/30/using-google-maps-in-android-development-tutorial-part-1/
  2. https://mirnauman.wordpress.com/2012/02/07/using-gps-in-android-and-animating-google-maps-to-the-current-gps-location-android-tutorial-part-2/
  3. https://mirnauman.wordpress.com/2012/02/13/adding-image-to-googlemaps-using-map-overlays-android-tutorial-part-3/
  4. https://mirnauman.wordpress.com/2012/02/14/android-google-maps-tutorial-part-4-adding-menu-some-additional-functionality-like-zooming-changing-map-view-animating-to-gps-current-location-using-menu-button/

Objective of this article is to add multiple images to our Google Map Application for Android. I have taken a scenario where I have a list of coordinates  of different cities of Pakistan and I want display an image on each city. Check the final output of this article so that you have an idea of what we…

View original post 354 more words

Mir

To get going with this tutorial one needs to go through all the previous 3 parts because we are using the same code from the previous tutorials and taking it a few steps further.

  1. https://mirnauman.wordpress.com/2012/01/30/using-google-maps-in-android-development-tutorial-part-1/
  2. https://mirnauman.wordpress.com/2012/02/07/using-gps-in-android-and-animating-google-maps-to-the-current-gps-location-android-tutorial-part-2/
  3. https://mirnauman.wordpress.com/2012/02/13/adding-image-to-googlemaps-using-map-overlays-android-tutorial-part-3/

Our main emphasis in this tutorial will be on adding a Menu to our Android GoogleMaps project and perform different actions using the buttons of that menu. So wat we will do is to create a menu with 3 buttons. Just like the one shown in the image below.

  1. My Current Location Button
  2. Map Satellite View Button
  3. Map Normal View Button

Before we start we will copy 3 images(mylocation.png,satelliteview.png,normalview.png) in the drawables folder in the res folder. After this we will create a new xml file in the layout folder. We will name our file as “menu.xml”. The code for menu.xml is given below.

Save the menu.xml file and open GooglemapsActivity.java file. Now add the…

View original post 514 more words

Mir

Objective of this tutorial is to place an image to the current location of the device, means displaying an image at the current coordinates from the GPS of the device. We can have a better idea from the image displayed below of what we are trying to achieve.

To start with this tutorial we will continue with the examples  from the previous tutorial and to have the exact output one must go through the previous tutorials in this series i.e,

First of all we will create a PNG image with transparent background or download it from Google or search it in the android samples folders. When we have our desired image just like the one shown below.

Copy this PNG and browse to your “res” folder of the GoogleMaps project. My path for the “res” folder is “C:\Users\Mir Nauman Tahir\workspace\googlemaps\res\” . In “res” folder we will have 3 more…

View original post 462 more words

Mir

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.

Secondly check the AVD that is it with GPS support or not. As we can see in the hardware section that “GPS Support” has…

View original post 353 more words

Mir

To use Google Maps in Android using MapView Control we need Google Maps API Key. To get the key we have to go through the following steps.

  1. Locate debug.keystore file on your system.
  2. Locate keytool.exe file on your system
  3. Generate MD5 fingerprint.
  4. Use that MD5 fingerprint to get the Google Maps API Key

The debug.keystore file can be found at “c:\users\mir nauman tahir\.android\debug.keystore”

Replace “mir nauman tahir” with your current user name.

Now navigate to keytool.exe file. “c:\program files\java\jdk1.7.0_02\bin\keytool.exe”

Replace the jdk version with the version installed on your system.

Now execute the following command.

C:\Program Files\Java\jdk1.7.0_02\bin>keytool.exe  -list -alias androiddebugkey -keystore “c:\users\mir nauman tahir\.android\debug.keystore” -storepass android -keypass android

If prompted for a password, try “changeit” or just press enter key to enter an empty password. This command will return SHA1 fingerprint. jdk1.7 or higher will return SHA1 by default. Remember the bold and italic text is the path to the debug.keystore…

View original post 187 more words

I have been trying this for a month and a lot of my readers asked to write a tutorial that makes it easy to draw a line on Google Maps for Android between two locations. I have gone through a lot of material. Most of the articles on this exact issue are not exactly addressing this specific issue. So their code is way too complex for this simple task. I have tried to make this as simple as possible.

Assumption About The Reader:

At this point I assume that reader of this article have already configured everything and Google Maps are displayed in their emulator properly. For those who don’t have Google Maps properly displayed in their emulator, they can read my earlier articles. Links provided at the end of this article.

Objective:

Draw a line that between two points on Google Maps lets say City “A” and City “B”. Repeat the process to draw a line between City “B” and City “C”.

What we need:

Coordinates of 3 cities or any 3 locations that we want to connect.

  1. Abbottabad :   34159000,73220000
  2. Islamabad :      33695043,73050000
  3. Rawalpindi :    33615043, 73050000

I have taken relative coordinates of 3 Pakistani cities starting from Abbottabad (where my family lives), moving to Islamabad (Capital of Pakistan). From Islamabad moving to Rawalpindi.

Coding (the part everyone’s been waiting for):

Create an overlay class lets say by the name of MyOverlay inside our main activity class named “GoogleMapsDrawPath” that extends MapActivity


class MyOverlay extends Overlay{

public MyOverlay(){

}

public void draw(Canvas canvas, MapView mapv, boolean shadow){
 super.draw(canvas, mapv, shadow);

//Configuring the paint brush

Paint mPaint = new Paint();
 mPaint.setDither(true);
 mPaint.setColor(Color.RED);
 mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
 mPaint.setStrokeJoin(Paint.Join.ROUND);
 mPaint.setStrokeCap(Paint.Cap.ROUND);
 mPaint.setStrokeWidth(4);

 GeoPoint gP1 = new GeoPoint(34159000,73220000);//starting point Abbottabad
 GeoPoint gP2 = new GeoPoint(33695043,73050000);//End point Islamabad

 GeoPoint gP4 = new GeoPoint(33695043, 73050000);//Start point Islamabad
 GeoPoint gP3 = new GeoPoint(33615043, 73050000);//End Point Rawalpindi

Point p1 = new Point();
 Point p2 = new Point();
 Path path1 = new Path();

 Point p3 = new Point();
 Point p4 = new Point();
 Path path2 = new Path();
 projection.toPixels(gP2, p3);
 projection.toPixels(gP1, p4);

path1.moveTo(p4.x, p4.y);//Moving to Abbottabad location
 path1.lineTo(p3.x,p3.y);//Path till Islamabad

 projection.toPixels(gP3, p1);
 projection.toPixels(gP4, p2);

path2.moveTo(p2.x, p2.y);//Moving to Islamabad location
 path2.lineTo(p1.x,p1.y);//Path to Rawalpindi

 canvas.drawPath(path1, mPaint);//Actually drawing the path from Abbottabad to Islamabad
 canvas.drawPath(path2, mPaint);//Actually drawing the path from Islamabad to Rawalpindi

 }

}

Explanation:

The code is pretty self explanatory as I have commented the parts that required explanation. First we configure the paint brush. To draw a line between two points we need to have coordinates of  at least two locations but as I am connecting 3 locations, I have taken 4 GeoPoints , repeating the coordinates of  Islamabad. I have done this to demonstrate that each time we draw a line we will need two points. After that I have created 4 Points and 2 Paths. Than setting the Projection. In simple words associating the GeoPoints  to Points.  After this setting the Paths like moving it to a starting location and defining the end point of the Path. Lastly call the canvas.drawPath(path1, mPaint) method providing it the path and the brush to actually draw the line on the map.

Configuring the Main Activity Class:

Add the following code in the onCreate method of GoogleMapsDrawPath class.


mapView = (MapView) findViewById(R.id.mapview1);//Creating an instance of MapView
 mapView.setBuiltInZoomControls(true);//Enabling the built-in Zoom Controls

 gP = new GeoPoint(33695043, 73000000);//Creating a GeoPoint

 mc = mapView.getController();
 mc.setCenter(gP);
 mc.setZoom(9);//Initializing the MapController and setting the map to center at the
 //defined GeoPoint

mapOverlays = mapView.getOverlays();
 projection = mapView.getProjection();

 myoverlay = new MyOverlay();
 mapOverlays.add(myoverlay);

Explanation:

Create instance of the mapView, enable the built-in zoom controls. Create GeoPoints and move the map to center at the created GeoPoint. Lastly add overlays from the myOverlay class to the map.

Path starting from Abbottabad moving towards Islamabad

Path Reaching Islamabad and than the second Path Starting from Islamabad to Rawalpindi

Complete path starting from Abbottabad to Rawalpindi. It looks like one single line but actually it consists of two line. The second one starting from the point where the first one is ending.

Some Extras that need explanation:

I feel the need of explaining a few more things that even if we don’t understand the code will work. But the readers have the right to know why a certain method, object, class etc is used.

  • GeoPoint :An immutable class representing a pair of latitude and longitude, stored as integer numbers of microdegrees
  • Projection :A Projection serves to translate between the coordinate system of x/y on-screen pixel coordinates and that of latitude/longitude points on the surface of the earth.
  • Point :Converts the given GeoPoint to onscreen pixel coordinates, relative to the top-left of the MapView that provided this Projection

 

Complete Code for GoogleMapsDrawPath Application:


package com.android.googlemapsdrawpath;

import java.util.List;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.os.Bundle;

public class GoogleMapsDrawPath extends MapActivity {
 /** Called when the activity is first created. */
 private List mapOverlays;
 private Projection projection;
 private MapController mc;
 private MapView mapView;
 private GeoPoint gP;
 //private GeoPoint gP2;
 private MyOverlay myoverlay;

@Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);

 mapView = (MapView) findViewById(R.id.mapview1);//Creating an instance of MapView
 mapView.setBuiltInZoomControls(true);//Enabling the built-in Zoom Controls

 gP = new GeoPoint(33695043, 73000000);//Creating a GeoPoint

 mc = mapView.getController();
 mc.setCenter(gP);
 mc.setZoom(9);//Initializing the MapController and setting the map to center at the
 //defined GeoPoint

mapOverlays = mapView.getOverlays();
 projection = mapView.getProjection();

 myoverlay = new MyOverlay();
 mapOverlays.add(myoverlay);

 }

@Override
 protected boolean isRouteDisplayed() {
 // TODO Auto-generated method stub
 return false;
 }

 class MyOverlay extends Overlay{

public MyOverlay(){

}

public void draw(Canvas canvas, MapView mapv, boolean shadow){
 super.draw(canvas, mapv, shadow);
 //Configuring the paint brush
 Paint mPaint = new Paint();
 mPaint.setDither(true);
 mPaint.setColor(Color.RED);
 mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
 mPaint.setStrokeJoin(Paint.Join.ROUND);
 mPaint.setStrokeCap(Paint.Cap.ROUND);
 mPaint.setStrokeWidth(4);


 GeoPoint gP1 = new GeoPoint(34159000,73220000);//starting point Abbottabad
 GeoPoint gP2 = new GeoPoint(33695043,73050000);//End point Islamabad

 GeoPoint gP4 = new GeoPoint(33695043, 73050000);//Start point Islamabad
 GeoPoint gP3 = new GeoPoint(33615043, 73050000);//End Point Rawalpindi

Point p1 = new Point();
 Point p2 = new Point();
 Path path1 = new Path();

 Point p3 = new Point();
 Point p4 = new Point();
 Path path2 = new Path();
 projection.toPixels(gP2, p3);
 projection.toPixels(gP1, p4);

path1.moveTo(p4.x, p4.y);//Moving to Abbottabad location
 path1.lineTo(p3.x,p3.y);//Path till Islamabad

 projection.toPixels(gP3, p1);
 projection.toPixels(gP4, p2);

path2.moveTo(p2.x, p2.y);//Moving to Islamabad location
 path2.lineTo(p1.x,p1.y);//Path to Rawalpindi

 canvas.drawPath(path1, mPaint);//Actually drawing the path from Abbottabad to Islamabad
 canvas.drawPath(path2, mPaint);//Actually drawing the path from Islamabad to Rawalpindi

 }

}
}

 

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

}

}

Blog Stats

  • 328,669 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: