Android Google Maps API v2找不到地址位置
我正在使用Google Maps Android API的Android Google Maps API v2找不到地址位置,android,google-maps,google-maps-api-2,google-geocoder,Android,Google Maps,Google Maps Api 2,Google Geocoder,我正在使用Google Maps Android API的GeoCoder类从给定的地址字符串中检索特定属性的坐标。但是android应用程序找不到该房产的详细信息。现在,当我将完全相同的字符串传递给时,它可以很容易地找到该房产的屋顶位置。它也适用于移动浏览器 发生了什么事?为什么谷歌地图在我的应用程序中找不到该属性?下面是我的代码: LatLng propertyLocation = null; try { propertyLocation = getLocat
GeoCoder
类从给定的地址字符串中检索特定属性的坐标。但是android应用程序找不到该房产的详细信息。现在,当我将完全相同的字符串传递给时,它可以很容易地找到该房产的屋顶位置。它也适用于移动浏览器
发生了什么事?为什么谷歌地图在我的应用程序中找不到该属性?下面是我的代码:
LatLng propertyLocation = null;
try {
propertyLocation = getLocationFromAddress("Property Address");
} catch (IOException e) {
e.printStackTrace();
}
MarkerOptions mOptions = new MarkerOptions();
mOptions.position(propertyLocation).title("Property Location").snippet(propertyAddress)
.draggable(true);
propertyMarker = googleMap.addMarker(mOptions);
propertyMarker.showInfoWindow();
googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(propertyLocation, 15), 3000,
null);
googleMap.setMyLocationEnabled(true);
这是我的getLocationFromAddress()代码>方法:
private LatLng getLocationFromAddress(String subjectPropertyAddress) throws IOException {
Log.i("Subject Property", subjectPropertyAddress);
if (Geocoder.isPresent()) {
Geocoder addressCoder = new Geocoder(LocationActivity.this);
List<Address> addresses = addressCoder.getFromLocationName(subjectPropertyAddress, 2);
if (addresses == null || addresses.isEmpty()) {
Log.i("Addresses", "Could not locate address:");
return null;
} else {
Address location = addresses.get(0);
return new LatLng(location.getLatitude(), location.getLongitude());
}
} else {
Log.i("Geo-Coder", "Not Present");
}
return null;
}
private LatLng getLocationFromAddress(字符串subjectPropertyAddress)引发IOException{
Log.i(“标的财产”,标的财产地址);
if(Geocoder.isPresent()){
地理编码器addressCoder=新地理编码器(LocationActivity.this);
列表地址=addressCoder.getFromLocationName(subjectPropertyAddress,2);
if(addresses==null | | addresses.isEmpty()){
Log.i(“地址”,“找不到地址:”);
返回null;
}否则{
地址位置=地址。获取(0);
返回新的LatLng(location.getLatitude(),location.getLongitude());
}
}否则{
Log.i(“地理编码器”,“不存在”);
}
返回null;
}
您在Asyntask类中尝试过吗?也许,这段代码可以帮助你
private class GeocodeTask extends AsyncTask<String, Void, List<Address>> {
@Override
protected List<Address> doInBackground(String... locationName) {
Geocoder geocoder = new Geocoder(ctx);
List<Address> addresses = null;
try {
// Getting a maximum of 3 Address that matches the input text
addresses = geocoder.getFromLocationName(locationName[0], 3);
} catch (IOException e) {
e.printStackTrace();
}
return addresses;
}
@Override
protected void onPostExecute(List<Address> addresses) {
if(addresses==null || addresses.size()==0){
Toast.makeText(getActivity(), "No Location found", Toast.LENGTH_SHORT).show();
}else {
googleMap.clear();
for(int i=0 ; i < addresses.size(); i++){
Address address = (Address) addresses.get(i);
latLng = new LatLng(address.getLatitude(), address.getLongitude());
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 13f));
googleMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
}
}
}
}
嘿,我试过你的解决方案,但没用。也许,这是地理编码器的问题?在xml布局中使用哪种地图视图?这是:“com.google.android.gms.maps.MapFragment”Danh DC,是的,它是com.google.android.gms.maps.MapFragment
。并且您的应用程序加载地图成功,只是GeoCoder的函数搜索位置有问题?因为GeoCoder找不到属性位置,它返回null,这会导致应用程序崩溃。但是是的,否则映射将成功加载,不会出现任何问题。
new GeocodeTask().execute("Property Address");