Get latitude and longitude based on city, zip or streetname

后端 未结 4 2101
悲&欢浪女
悲&欢浪女 2020-12-30 17:12

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 Rega

相关标签:
4条回答
  • 2020-12-30 17:44

    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();
    }
    
    0 讨论(0)
  • 2020-12-30 17:46

    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
    
    0 讨论(0)
  • 2020-12-30 17:47

    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

    0 讨论(0)
  • 2020-12-30 17:48

    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();
    }
    
    0 讨论(0)
提交回复
热议问题