1

In my application am setting a default value to latitude and longitude. When i type the cityname in edittext i need to move that point to that place where i need

harish
  • 1,705
  • 5
  • 22
  • 36
  • please check this link http://stackoverflow.com/questions/3574644/how-can-i-find-the-latitude-and-longitude-from-address – vipin Apr 04 '12 at 09:54

4 Answers4

3

Use the below code to get the lat and long of city name

String location=cityName;
String inputLine = "";
String result = ""
location=location.replaceAll(" ", "%20");
String myUrl="http://maps.google.com/maps/geo?q="+location+"&output=csv";
try{
 URL url=new URL(myUrl);
 URLConnection urlConnection=url.openConnection();
 BufferedReader in = new BufferedReader(new 
 InputStreamReader(urlConnection.getInputStream()));
  while ((inputLine = in.readLine()) != null) {
  result=inputLine;
  }
  String lat = result.substring(6, result.lastIndexOf(","));
  String longi = result.substring(result.lastIndexOf(",") + 1);
 }
 catch(Exception e){
 e.printStackTrace();
 }
Nishant
  • 31,137
  • 4
  • 36
  • 50
  • 1
    As of 11/14/2017 says "404. That’s an error. The requested URL /maps/geo was not found on this server. That’s all we know. " However, "https://stackoverflow.com/questions/45534047/get-latitude-and-longitude-from-a-specific-city-android" still works. – steven smith Nov 14 '17 at 22:49
1

My suggestion is to use Google GeoCoding REST API , not Geocoder.

Because I once used it in my country,China,I have a error below:

"java.io.IOException: Service not Available"

after search , It`s seems that

"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. Use the isPresent() method to determine whether a Geocoder implementation    exists."

So I turn to Google GeoCoding REST API , It`s easy , just request like this:

http://maps.googleapis.com/maps/api/geocode/json?address=NewYork&sensor=false

You will get a json that has geo location in it. And You can also get Address from locaiton through it:

http://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&sensor=false

If you can read Chinese , I have a blog about this,check this out: http://wangchao.de/android%E5%88%A9%E7%94%A8google-geocoding-api%E6%9B%BF%E4%BB%A3geocoder-%E8%A7%A3%E5%86%B3%E5%9C%B0%E5%9D%80%E8%AF%AD%E8%A8%80%E9%97%AE%E9%A2%98

Wangchao0721
  • 909
  • 1
  • 9
  • 23
  • You might have tested it in Emulator when you get that error `Service not Available` cause Emulator doesn't have support for it..You Should have tried that in Device. – MKJParekh Apr 04 '12 at 09:49
  • I test it on my device,that`s really happened,there is a issue about it on google code,check this out:http://code.google.com/p/android/issues/detail?id=8816 – Wangchao0721 Apr 04 '12 at 12:34
  • I wasn't aware of this issue for chinese devs – Julian Suarez Apr 05 '12 at 09:08
0

Use the Geocoder class.

Geocode gc = new Geocoder();
List<Address> ads = gc.getFromLocationName(cityName,maxResults);

From the docs :

A class for handling geocoding and reverse geocoding. Geocoding is the process of transforming a street address or other description of a location into a (latitude, longitude) coordinate. Reverse geocoding is the process of transforming a (latitude, longitude) coordinate into a (partial) address. The amount of detail in a reverse geocoded location description may vary, for example one might contain the full street address of the closest building, while another might contain only a city name and postal code. 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. Use the isPresent() method to determine whether a Geocoder implementation exists.

Julian Suarez
  • 4,359
  • 4
  • 21
  • 38
0

Try to get the GeoPoint of cityname

GeoPoint startGP = new GeoPoint(
                (int) (Double.parseDouble(cityname.getText()) * 1E6));

Then u can able to get the latitude and longitude from GeoPoint by using below code.

 Double.toString((double)startGP.getLatitudeE6() / 1E6),
 Double.toString((double)dest.getLongitudeE6() / 1E6)
Brent Worden
  • 9,586
  • 7
  • 50
  • 50
Anand M Joseph
  • 567
  • 4
  • 6