我正构建一个应用程序,使用lat long向Toast显示用户当前地址。我使用Geocoder将lat long转换成地址,但情况是,在某些设备上地址工作正常但在某些设备中它崩溃了。然后我在那些设备上调试应用程序崩溃,我发现在地理编码器行后它会跳转到捕获(异常),而不执行其他行。 那么,有没有其他方法可以通过lat获取位置。
还有另一个问题是同一个问题here,但这确实没有提供太多解决方案,所以我再次提出问题。
我的Geocoder区块是
if (mLastLocation != null) {
double latitude = mLastLocation.getLatitude();
double longitude = mLastLocation.getLongitude();
try {
geocoder = new Geocoder(this, Locale.getDefault());
addresses = geocoder.getFromLocation(latitude,longitude,1);
if (addresses != null && addresses.size() > 0){
String address = addresses.get(0).getAddressLine(0);
String city = addresses.get(0).getLocality();
String state = addresses.get(0).getAdminArea();
String country = addresses.get(0).getCountryName();
String postalCode = addresses.get(0).getPostalCode();
Toast.makeText(getApplicationContext(), "Your address is: " +address+ " "+city+ " " + state+ "\n" +country+ " "+ postalCode, Toast.LENGTH_LONG).show();
}
}catch (Exception e){
e.printStackTrace();
}
答案 0 :(得分:2)
您只需点击谷歌地图网络服务即可传递纬度和经度。它只是一个GET-Method网络服务。
http://maps.googleapis.com/maps/api/geocode/json?latlng=32,75&sensor=true
将lat和long替换为您自己的lat& long。
答案 1 :(得分:0)
您可以使用此Google API获取与纬度和经度相关的完整信息。
http://maps.googleapis.com/maps/api/geocode/json?latlng=32,75&sensor=true
这里32是纬度,75是经度。
答案 2 :(得分:0)
String city = getLocationCityName(lat,lang);
public static String getLocationCityName(double lat, double lon) {
JSONObject result = getLocationFormGoogle(lat + "," + lon);
String city = getCityAddress(result);
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();
org.apache.http.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")) {//city
String city = component.getString("long_name");
Log.d("city", city);
return component.getString("long_name");
}
if (types.getString(j).equals("postal_code")) {//pin
return component.getString("long_name");
}
if (types.getString(j).equals("country")) {//country
return component.getString("long_name");
}
if (types.getString(j).equals("administrative_area_level_1")) {//state
return component.getString("long_name");
}
if (types.getString(j).equals("administrative_area_level_2")) {
return component.getString("long_name");
}
}
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
return null;
}