简体   繁体   中英

Get latitude and longitude based on city, zip or streetname

In my current android application, I would like to get the geocordinates based on an entered city name, street name or zip code. How can I accomplish this?

Best Regards, Rony

Check out the method Geocoder.getFromLocationName(cityName, maxResults) .

Use it like this:

List<Address> addressList = geoCoder.getFromLocationName(cityName, 1);
Address address = addressList.get(0);
if(address.hasLatitude() && address.hasLongitude()){
    double selectedLat = address.getLatitude();
    double selectedLng = address.getLongitude();
}

Hi try the following code to get Geocode point from given address.

List<Address> foundGeocode = null;
/* find the addresses  by using getFromLocationName() method with the given address*/
foundGeocode = new Geocoder(this).getFromLocationName("address here", 1);
 foundGeocode.get(0).getLatitude(); //getting latitude
 foundGeocode.get(0).getLongitude();//getting longitude

Hi,

There's a very nice site called the World Gazetteer that has the data you need, in a neat, downloadable file (by city name)

http://www.world-gazetteer.com/home.htm

From the main page, click on the link that says:

other statistics.....various statistics: tables, maps and downloadable data

and from the page that comes up, click on the link that says:

popdata (1.4 MB)

Unzip the file, and you got it!

The database is free master database of world cities which includes latitude, longitude, and population data...etc.

Got this from: http://answers.google.com/answers/threadview?id=432778

Try this.

Geocoder geoCoder = new Geocoder(getBaseContext(), Locale.getDefault());
try {
    List<Address> addresses = geoCoder.getFromLocation(latitude , longitude, 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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM