How can I find the latitude and longitude from address?

前端 未结 9 1082
礼貌的吻别
礼貌的吻别 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:53
    Geocoder coder = new Geocoder(this);
            List<Address> addresses;
            try {
                addresses = coder.getFromLocationName(address, 5);
                if (addresses == null) {
                }
                Address location = addresses.get(0);
                double lat = location.getLatitude();
                double lng = location.getLongitude();
                Log.i("Lat",""+lat);
                Log.i("Lng",""+lng);
                LatLng latLng = new LatLng(lat,lng);
                MarkerOptions markerOptions = new MarkerOptions();
                markerOptions.position(latLng);
                googleMap.addMarker(markerOptions);
                googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng,12));
            } catch (IOException e) {
                e.printStackTrace();
            }
    
    0 讨论(0)
  • 2020-11-22 10:57
    public void goToLocationFromAddress(String strAddress) {
        //Create coder with Activity context - this
        Geocoder coder = new Geocoder(this);
        List<Address> address;
    
        try {
            //Get latLng from String
            address = coder.getFromLocationName(strAddress, 5);
    
            //check for null
            if (address != null) {
    
                //Lets take first possibility from the all possibilities.
                try {
                    Address location = address.get(0);
                    LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
    
                    //Animate and Zoon on that map location
                    mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
                    mMap.animateCamera(CameraUpdateFactory.zoomTo(15));
                } catch (IndexOutOfBoundsException er) {
                    Toast.makeText(this, "Location isn't available", Toast.LENGTH_SHORT).show();
                }
    
            }
    
    
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    0 讨论(0)
  • 2020-11-22 10:58

    Ud_an's solution with updated API's

    Note: LatLng class is part of Google Play Services.

    Mandatory:

    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    
    <uses-permission android:name="android.permission.INTERNET"/>
    

    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> 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;
    }
    
    0 讨论(0)
  • 2020-11-22 11:03
    public LatLang getLatLangFromAddress(String strAddress){
        Geocoder coder = new Geocoder(this, Locale.getDefault());
        List<Address> address;
        try {
            address = coder.getFromLocationName(strAddress,5);
            if (address == null) {
                    return new LatLang(-10000, -10000);
                }
                Address location = address.get(0);
                return new LatLang(location.getLatitude(), location.getLongitude());
            } catch (IOException e) {
                return new LatLang(-10000, -10000);
            }
        }            
    

    LatLang is a pojo class in this case.

    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> permission is not required.

    0 讨论(0)
  • 2020-11-22 11:05

    The following code will work for google apiv2:

    public void convertAddress() {
        if (address != null && !address.isEmpty()) {
            try {
                List<Address> addressList = geoCoder.getFromLocationName(address, 1);
                if (addressList != null && addressList.size() > 0) {
                    double lat = addressList.get(0).getLatitude();
                    double lng = addressList.get(0).getLongitude();
                }
            } catch (Exception e) {
                e.printStackTrace();
            } // end catch
        } // end if
    } // end convertAddress
    

    Where address is the String (123 Testing Rd City State zip) you want to convert to LatLng.

    0 讨论(0)
  • 2020-11-22 11:15

    An answer to Kandha problem above :

    It throws the "java.io.IOException service not available" i already gave those permission and include the library...i can get map view...it throws that IOException at geocoder...

    I just added a catch IOException after the try and it solved the problem

        catch(IOException ioEx){
            return null;
        }
    
    0 讨论(0)
提交回复
热议问题