如何从经纬度获取地址 android google map v2

How to get address from latitude and longitude android google map v2

我试图从 Android Google Maps v2 中的纬度和经度获取地址,但它出错了。

这是我的代码:

case PLACES_DETAILS :
                final HashMap<String, String> hm = result.get(0);

                final double latitude = Double.parseDouble(hm.get("lat"));
                final double longitude = Double.parseDouble(hm.get("lng"));

                SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
                googleMap = fm.getMap();

                final LatLng point = new LatLng(latitude, longitude);
                CameraUpdate cameraPosition = CameraUpdateFactory.newLatLngZoom(point, 10);
                CameraUpdate cameraZoom = CameraUpdateFactory.zoomBy(5);

                googleMap.moveCamera(cameraPosition);
                googleMap.animateCamera(cameraZoom);
                googleMap.getCameraPosition();
                //Log.e("Lat", String.valueOf(googleMap.getCameraPosition().target.latitude));
                //Log.e("Long", String.valueOf(googleMap.getCameraPosition().target.latitude));
                //googleMap.setOnMyLocationChangeListener(myLocationChangeListener);

                googleMap.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
                    @Override
                    public void onCameraChange(CameraPosition cameraPosition) {
                        Log.e("Lat", String.valueOf(googleMap.getCameraPosition().target.latitude));
                        Log.e("Long", String.valueOf(googleMap.getCameraPosition().target.longitude));
                    }
                });

使用以下代码片段:

try { 
Geocoder geo = new Geocoder(youractivityclassname.this.getApplicationContext(), Locale.getDefault());
List<Address> addresses = geo.getFromLocation(latitude, longitude, 1);
if (addresses.isEmpty()) {
    yourtextfieldname.setText("Waiting for Location"); 
} 
else { 
    if (addresses.size() > 0) {
        yourtextfieldname.setText(addresses.get(0).getFeatureName() + ", " + addresses.get(0).getLocality() +", " + addresses.get(0).getAdminArea() + ", " + addresses.get(0).getCountryName());
        //Toast.makeText(getApplicationContext(), "Address:- " + addresses.get(0).getFeatureName() + addresses.get(0).getAdminArea() + addresses.get(0).getLocality(), Toast.LENGTH_LONG).show(); 
    } 
} 
}    
   catch (Exception e) {
e.printStackTrace(); // getFromLocation() may sometimes fail
} 

参考: https://mobiforge.com/design-development/using-google-maps-android

我给你我的代码,我在 2 个月前使用了这段代码。我现在无法测试这段代码,但它当时有效。你可能需要修改一些行。 使用此功能

 public void getAddressByLatLong(){
latitude = bundle.getDouble("Lat");
        longitude = bundle.getDouble("Long");
        LatLng pos = new LatLng(latitude, longitude);

        try {
            if (googleMap == null) {
                googleMap = ((MapFragment) getFragmentManager()
                        .findFragmentById(R.id.map)).getMap();
            }
            googleMap.setMapType(googleMap.MAP_TYPE_HYBRID);

            LocationManager location_manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            LocationListener listner = new MyLocationListner();
            location_manager.requestLocationUpdates(
                    LocationManager.NETWORK_PROVIDER, 3000, 5000, listner);

                    } catch (Exception e) {
            e.printStackTrace();
        }
                    }

创建 class

public class MyLocationListner implements LocationListener {

@SuppressLint("NewApi")
@SuppressWarnings("static-access")
@Override
public void onLocationChanged(Location arg0) {


        Toast.makeText(getApplicationContext(),
                "In on Location Changed", Toast.LENGTH_SHORT).show();
        // TODO Auto-generated method stub
        getLatitude = "" + arg0.getLatitude();
        getLongitude = "" + arg0.getLongitude();

        // lati.setText(getLatitude + "," + getLongitude);

        latitude = arg0.getLatitude();
        longitude = arg0.getLongitude();



    try {

        geocoder = new Geocoder(DisplayActivity.this, Locale.ENGLISH);
        addresses = geocoder.getFromLocation(latitude, longitude, 1);

        if (geocoder.isPresent()) {
            Toast.makeText(getApplicationContext(), "geocoder present",
                    Toast.LENGTH_SHORT).show();

            Address returnAddress = addresses.get(0);

            String localityString = returnAddress.getLocality();
            String city = returnAddress.getCountryName();
            String region_code = returnAddress.getCountryCode();
            String zipcode = returnAddress.getPostalCode();
            str = new StringBuilder();
            str.append(localityString + " ");
            str.append(city + " ");
            str.append(region_code + " ");
            str.append(zipcode + " ");

            // Add = str.toString();
            // longi.setText(str);

            CameraPosition cameraPosition = new CameraPosition.Builder()
                    .target(new LatLng(latitude, longitude)).zoom(2)
                    .build();

            googleMap.animateCamera(CameraUpdateFactory
                    .newCameraPosition(cameraPosition));
            MarkerOptions marker = new MarkerOptions().position(
                    new LatLng(latitude, longitude)).title(
                    str.toString());
            marker.icon(BitmapDescriptorFactory
                    .fromResource(R.drawable.mario));

            googleMap.addMarker(marker);
            Toast.makeText(getApplicationContext(), str,
                    Toast.LENGTH_SHORT).show();
        } else
            Toast.makeText(getApplicationContext(),
                    "geocoder not present", Toast.LENGTH_SHORT).show();

    } catch (IOException e) {
        Toast.makeText(getApplicationContext(), "Exception",
                Toast.LENGTH_SHORT).show();
    }
}

@Override
public void onProviderDisabled(String arg0) {
}

@Override
public void onProviderEnabled(String arg0) {
}

@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
}

}

这应该为您提供经纬度地址

Geocoder geocoder;
    List<Address> addresses;
    geocoder = new Geocoder(mContext, Locale.getDefault());
    try {
        String sPlace;
        addresses = geocoder.getFromLocation(mLat, mLong, 1);
        String address = addresses.get(0).getAddressLine(0);
        String city = addresses.get(0).getAddressLine(1);
        String country = addresses.get(0).getAddressLine(2);

        String[] splitAddress = address.split(",");
        sPlace = splitAddress[0] + "\n";
        if(city != null && !city.isEmpty()) {
            String[] splitCity = city.split(",");
            sPlace += splitCity[0];
        }

    } catch (IOException e) {
        e.printStackTrace();
    }