Android 地理编码器找不到地址,google 地图可以找到
Android geocoder can not find address,google maps can find
当我点击搜索时它可以找到
Akbank Sanat istanbul
但是当我使用这段代码时,它找不到:
private LatLng getCoors(String loc, String desc){
Log.v(TAG, "getCoors ");
loc = loc +" Istanbul";
if (Geocoder.isPresent()) {
Log.v(TAG, "getCoors geocoder present");
geocoder = new Geocoder(mContext, Locale.getDefault());
Log.v(TAG, "location: "+loc);
try {
From_geocode = geocoder.getFromLocationName(loc, 1);
Log.v(TAG, "fromgeocode: "+From_geocode);
} catch (IOException e) {
e.printStackTrace();
Log.v(TAG, "catched error - from geocode");//need to do something
}
if (!From_geocode.isEmpty()) {
Log.v(TAG, " getcoors fromgeocod enot not empty ");
coors = new LatLng(From_geocode.get(0).getLatitude(), From_geocode.get(0).getLongitude());
Log.v(TAG, "LATITUTE=====" + coors.latitude + " LONGITUTE=====" + coors.longitude);
return coors;
}//second if end
else{
Log.v(TAG, " hata getcoors fromgeocode empty ");
}
}//first if end
else{
Log.v(TAG, " hata getcoors geocoder not preset ");
}
return null;
}//getcoors end
当我使用相同的地址时,
else{
Log.v(TAG, " hata getcoors fromgeocode empty ");
}
它在这里。所以,这意味着它无法获取 googlemaps 可以获取的地址。
我做错了什么?有没有不使用 json 甚至不连接的简单方法?
显然,地理编码器在单个 运行 中准确计算位置方面存在问题。尝试重复询问位置 5-10 次,直到收到回复。
或者,为了更准确地从字符串中获取地址,请尝试执行以下操作:
public JSONObject getLocationInfo() {
HttpGet httpGet = new HttpGet("http://maps.google.com/maps/api/geocode/json?latlng="+lat+","+lng+"&sensor=true");
HttpClient client = new DefaultHttpClient();
HttpResponse response;
StringBuilder stringBuilder = new StringBuilder();
try {
response = client.execute(httpGet);
HttpEntity entity = response.getEntity();
InputStream stream = entity.getContent();
int b;
while ((b = stream.read()) != -1) {
stringBuilder.append((char) b);
}
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
JSONObject jsonObject = new JSONObject();
try {
jsonObject = new JSONObject(stringBuilder.toString());
} catch (JSONException e) {
e.printStackTrace();
}
return jsonObject;
}
并这样称呼它:
JSONObject ret = getLocationInfo();
JSONObject location;
String location_string;
try {
location = ret.getJSONArray("results").getJSONObject(0);
location_string = location.getString("formatted_address");
Log.d("test", "formattted address:" + location_string);
} catch (JSONException e1) {
e1.printStackTrace();
}
此代码来自 Shobhit Puri,可以找到原始问题 here。
当我点击搜索时它可以找到
Akbank Sanat istanbul
但是当我使用这段代码时,它找不到:
private LatLng getCoors(String loc, String desc){
Log.v(TAG, "getCoors ");
loc = loc +" Istanbul";
if (Geocoder.isPresent()) {
Log.v(TAG, "getCoors geocoder present");
geocoder = new Geocoder(mContext, Locale.getDefault());
Log.v(TAG, "location: "+loc);
try {
From_geocode = geocoder.getFromLocationName(loc, 1);
Log.v(TAG, "fromgeocode: "+From_geocode);
} catch (IOException e) {
e.printStackTrace();
Log.v(TAG, "catched error - from geocode");//need to do something
}
if (!From_geocode.isEmpty()) {
Log.v(TAG, " getcoors fromgeocod enot not empty ");
coors = new LatLng(From_geocode.get(0).getLatitude(), From_geocode.get(0).getLongitude());
Log.v(TAG, "LATITUTE=====" + coors.latitude + " LONGITUTE=====" + coors.longitude);
return coors;
}//second if end
else{
Log.v(TAG, " hata getcoors fromgeocode empty ");
}
}//first if end
else{
Log.v(TAG, " hata getcoors geocoder not preset ");
}
return null;
}//getcoors end
当我使用相同的地址时,
else{
Log.v(TAG, " hata getcoors fromgeocode empty ");
}
它在这里。所以,这意味着它无法获取 googlemaps 可以获取的地址。
我做错了什么?有没有不使用 json 甚至不连接的简单方法?
显然,地理编码器在单个 运行 中准确计算位置方面存在问题。尝试重复询问位置 5-10 次,直到收到回复。
或者,为了更准确地从字符串中获取地址,请尝试执行以下操作:
public JSONObject getLocationInfo() {
HttpGet httpGet = new HttpGet("http://maps.google.com/maps/api/geocode/json?latlng="+lat+","+lng+"&sensor=true");
HttpClient client = new DefaultHttpClient();
HttpResponse response;
StringBuilder stringBuilder = new StringBuilder();
try {
response = client.execute(httpGet);
HttpEntity entity = response.getEntity();
InputStream stream = entity.getContent();
int b;
while ((b = stream.read()) != -1) {
stringBuilder.append((char) b);
}
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
JSONObject jsonObject = new JSONObject();
try {
jsonObject = new JSONObject(stringBuilder.toString());
} catch (JSONException e) {
e.printStackTrace();
}
return jsonObject;
}
并这样称呼它:
JSONObject ret = getLocationInfo();
JSONObject location;
String location_string;
try {
location = ret.getJSONArray("results").getJSONObject(0);
location_string = location.getString("formatted_address");
Log.d("test", "formattted address:" + location_string);
} catch (JSONException e1) {
e1.printStackTrace();
}
此代码来自 Shobhit Puri,可以找到原始问题 here。