The*_*ash 35 android google-maps-android-api-2
描述:我正在使用Google maps API V2.我已Android Reverse Geocoding在触摸位置实施.
问题:它抛出异常
try {
addresses = geocoder.getFromLocation(latitude, longitude,1);}
catch (IOException e)
{
e.printStackTrace();
if(AppConstants.DEBUG)Log.v(AppConstants.DEBUG_TAG," e.printStackTrace(): "+e.getMessage());
}
Run Code Online (Sandbox Code Playgroud)
我收到latitude并且longitude价值正确,但我无法理解为什么它会抛出exception,我也做了谷歌搜索,但它无能为力.
任何人都可以详细解释一下吗?
San*_*r V 84
如此Android问题38009中所述 - Geocoder抛出异常:IOException:服务不可用 重新启动设备将解决问题
Ani*_*M H 16
如果您不想重启设备,请使用此功能
public JSONObject getLocationFormGoogle(String placesName) {
HttpGet httpGet = new HttpGet("http://maps.google.com/maps/api/geocode/json?address=" +placesName+"&ka&sensor=false");
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;
}
public LatLng getLatLng(JSONObject jsonObject) {
Double lon = new Double(0);
Double lat = new Double(0);
try {
lon = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
.getJSONObject("geometry").getJSONObject("location")
.getDouble("lng");
lat = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
.getJSONObject("geometry").getJSONObject("location")
.getDouble("lat");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return new LatLng(lat,lon);
}
LatLng Source =getLatLng(getLocationFormGoogle(placesName));
Run Code Online (Sandbox Code Playgroud)
小智 10
我修改了@Ani解决方案以从lat long参数中获取城市名称:
public static String getLocationCityName( double lat, double lon ){
JSONObject result = getLocationFormGoogle(lat + "," + lon );
return getCityAddress(result);
}
protected static JSONObject getLocationFormGoogle(String placesName) {
String apiRequest = "https://maps.googleapis.com/maps/api/geocode/json?latlng=" + placesName; //+ "&ka&sensor=false"
HttpGet httpGet = new HttpGet(apiRequest);
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;
}
protected static String getCityAddress( JSONObject result ){
if( result.has("results") ){
try {
JSONArray array = result.getJSONArray("results");
if( array.length() > 0 ){
JSONObject place = array.getJSONObject(0);
JSONArray components = place.getJSONArray("address_components");
for( int i = 0 ; i < components.length() ; i++ ){
JSONObject component = components.getJSONObject(i);
JSONArray types = component.getJSONArray("types");
for( int j = 0 ; j < types.length() ; j ++ ){
if( types.getString(j).equals("locality") ){
return component.getString("long_name");
}
}
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
return null;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
26249 次 |
| 最近记录: |