Pages

Monday, April 11, 2011

GoogleMap - Latitude and Longitude

I have to display the map with respect to the place given.

In that tags the longitude and latitude of the location to be passed as parameters to get the exact location in Map.

I got many examples for displaying the Google map in different way & special effects. But not able to find how get the longitude and latitude of a location/place. At last found the way. Might help someone who searches like me.

Code: To get the Longitude & Latitude of the location

To have Google map in an application. We have to get a key from this link
http://code.google.com/apis/maps/signup.html



import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;


try {

String locationName="johannesburg"; \\Give any location\place name
String gmapKey="ABQIAAAAiqblAQNWNoua1VYnGtU1dBRdtL7gCpXlU8FuabFvwIMVSlhRL4gzsM_7WZ6hbScCw1YuLXHNjwA"; \\Give your google map key
URL url = new URL("http://maps.google.com/maps/geo?q="+locationName+"&output=csv&key="+gmapKey);
URLConnection urlConnection = url.openConnection();
BufferedReader in;
String inputLine;
String data =null;

in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
while ((inputLine = in.readLine()) != null){
data =inputLine;
System.out.println("Accuracy,Status,Latitude,Longitutude of " +locationName+" >> "+data);
}
in.close();

}
} catch (IOException e) {
e.printStackTrace();
}


output:

Accuracy,Status,Latitude,Longitutude of johannesburg >> 200,4,-26.2041028,28.0473051