How can I find the latitude and longitude from address?

前端 未结 9 1185
礼貌的吻别
礼貌的吻别 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:57

    public void goToLocationFromAddress(String strAddress) {
        //Create coder with Activity context - this
        Geocoder coder = new Geocoder(this);
        List
    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(); } }

提交回复
热议问题