Get latitude and longitude based on city, zip or s

2019-02-05 08:57发布

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

4条回答
淡お忘
2楼-- · 2019-02-05 09:32

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();
}
查看更多
够拽才男人
3楼-- · 2019-02-05 09:34

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();
}
查看更多
贼婆χ
4楼-- · 2019-02-05 09:35

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

查看更多
我欲成王,谁敢阻挡
5楼-- · 2019-02-05 09:44

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
查看更多
登录 后发表回答