简体   繁体   中英

android google maps center of the camera location name

hi guys i have a marker in the center of my map camera and i want to show where information of the place marker is right now i write this so far but its not working and here is the error

 @Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    Toast.makeText(getContext(), "Map is Ready", Toast.LENGTH_SHORT).show();
        getDeviceLocation();

    mMap.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
        @Override
        public void onCameraChange(CameraPosition cameraPosition) {
            Geocoder geocoder = new Geocoder(getContext(), Locale.getDefault());

            try {
                List<Address> addresses = geocoder.getFromLocation(cameraPosition.target.latitude,cameraPosition.target.longitude, 1);
                Address obj = addresses.get(0);
                String add = obj.getAddressLine(0);
                Toast.makeText(getContext(), add, Toast.LENGTH_SHORT).show();
                Log.e("IGA", "Address" + add);


            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                Toast.makeText(getContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
            }
        }
    });


    }

this is the error and its comes from List

java.lang.IndexOutOfBoundsException: Index: 0, Size: 0

Since you didn't post the entire stack trace I can't really know which object is throwing the IndexOutOfBoundsException . But this sample code should get you pointed in the right direction.

@Override
public void onCameraChange(CameraPosition cameraPosition) {
    Geocoder geocoder = new Geocoder(getContext(), Locale.getDefault());

    try {
        List<Address> addresses = geocoder.getFromLocation(cameraPosition.target.latitude,cameraPosition.target.longitude, 1);
        for(Address address : addresses){
            if(address.getMaxAddressLineIndex() > -1){
                String add = address.getAddressLine(0);
                Toast.makeText(getContext(), add, Toast.LENGTH_SHORT).show();
                Log.e("IGA", "Address" + add);
            }
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        Toast.makeText(getContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
    }
}

I've add a foreach loop for the addresses object. I see that you have put a max Address of 1 in your getFromLocation() method, but if you change it to a higher number then this code should still work for all Addresses .

I've also put a check to see if the address actually has a value at getAddressLine(0) by checking getMaxAddressLineIndex() is bigger than -1. It is the index not count. getMaxAddressLineIndex() will return -1 if it is empty.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM