How can I find the latitude and longitude from address?

前端 未结 9 1180
礼貌的吻别
礼貌的吻别 2020-11-22 10:27

I want to show the location of an address in Google Maps.

How do I get the latitude and longitude of an address using the Google Maps API?

9条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-22 10:58

    Ud_an's solution with updated API's

    Note: LatLng class is part of Google Play Services.

    Mandatory:

    
    
    
    

    Update: If you have target SDK 23 and above, make sure you take care of runtime permission for location.

    public LatLng getLocationFromAddress(Context context,String strAddress) {
    
        Geocoder coder = new Geocoder(context);
        List
    address; LatLng p1 = null; try { // May throw an IOException address = coder.getFromLocationName(strAddress, 5); if (address == null) { return null; } Address location = address.get(0); p1 = new LatLng(location.getLatitude(), location.getLongitude() ); } catch (IOException ex) { ex.printStackTrace(); } return p1; }

提交回复
热议问题