[英]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
. 由于您没有发布整个堆栈跟踪信息,因此我无法真正知道哪个对象引发了
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. 我为
addresses
对象添加了一个foreach循环。 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
. 我看到您在
getFromLocation()
方法中放置了一个最大地址1,但是如果将其更改为更高的数字,则此代码仍应适用于所有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. 我还检查了
getMaxAddressLineIndex()
是否大于-1,以检查address
在getAddressLine(0)
是否实际具有值。 It is the index not count. 它不是索引。
getMaxAddressLineIndex()
will return -1 if it is empty. 如果为空,则
getMaxAddressLineIndex()
将返回-1。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.