使用json而不是Geocoder从谷歌地图获取位置地址
Posted
技术标签:
【中文标题】使用json而不是Geocoder从谷歌地图获取位置地址【英文标题】:Getting address of location from google map using json instead of Geocoder 【发布时间】:2014-06-01 06:42:02 【问题描述】:我正在谷歌地图上开发一个应用程序。我是使用谷歌地图 API V2 的新手。我想单击地图上的位置并获取该位置的地址,我使用 Geocoder,它适用于我,但在不同版本的 android 上它不起作用,所以我想使用 json 任何人帮助我.. Mainactivity.java
@Override
public void onMapLongClick(LatLng point)
lat = point.latitude;
lng = point.longitude;
try
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocation(lat, lng, 1);
StringBuilder sb = new StringBuilder();
if (addresses.size() > 0)
Address address = addresses.get(0);
sb.append(address.getCountryName()).append(", ");
sb.append(address.getLocality()).append(", ");
sb.append(address.getSubLocality()).append("\n");
sb.append(address.getFeatureName()).append(", ");
sb.append(address.getPostalCode()).append(", ");
sb.append(address.getAdminArea()).append(", ");
sb.append(address.getPhone()).append("");
addressString = sb.toString();
mTapTextView.setText(addressString);
catch (IOException e)
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.googlemaps"
android:versionCode="1"
android:versionName="1.0" >
<permission
android:name="com.example.googlemaps.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.example.googlemaps.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- Required to show current location -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!-- Required OpenGL ES 2.0. for Maps V2 -->
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.googlemaps.Main"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyC9yx6mNQGAIQv9GNAPIMXRYaxWhWwMJ2c" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<activity
android:name="com.example.googlemaps.ShowDirection"
android:label="@string/title_activity_show_direction"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
</activity>
</application>
</manifest>
【问题讨论】:
你拿什么来获取json?? 不知道 json Achilles 【参考方案1】:使用Google GeocodingAPI
反向地理编码可以通过在HTTP GET方法中提供APIKEY、经纬度坐标来实现,可以直接得到json响应格式的结果
https://maps.googleapis.com/maps/api/geocode/json?latlng=
【讨论】:
我给了 API KEY,但是我如何使用 json 方法来获取特定位置的地址 你有没有提供地址的经纬度坐标 是的,我可以通过 onMapLongclick 方法获取 lat 和 lng,但是我怎样才能代表提供该 lat 和 lng 来获取该位置的地址【参考方案2】:试试下面的代码:
public static JSONObject getLocationInfo(double lat, double lng)
HttpGet httpGet = new HttpGet("http://maps.googleapis.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;
public static String getLoc(double lat, double lng)
JSONObject jsonObj = getLocationInfo(lat, lng);
Log.i("JSON string =>", jsonObj.toString());
String currentLocation = "testing";
String street_address = null;
String postal_code = null;
try
String status = jsonObj.getString("status").toString();
Log.i("status", status);
if(status.equalsIgnoreCase("OK"))
JSONArray results = jsonObj.getJSONArray("results");
int i = 0;
Log.i("i", i+ "," + results.length() ); //TODO delete this
do
JSONObject r = results.getJSONObject(i);
JSONArray typesArray = r.getJSONArray("types");
String types = typesArray.getString(0);
if(types.equalsIgnoreCase("street_address"))
street_address = r.getString("formatted_address").split(",")[0];
Log.i("street_address", street_address);
else if(types.equalsIgnoreCase("postal_code"))
postal_code = r.getString("formatted_address");
Log.i("postal_code", postal_code);
if(street_address!=null && postal_code!=null)
currentLocation = street_address + "," + postal_code;
Log.i("Current Location =>", currentLocation); //Delete this
i = results.length();
i++;
while(i<results.length());
Log.i("JSON Geo Locatoin =>", currentLocation);
return currentLocation;
catch (JSONException e)
Log.e("testing","Failed to load JSON");
e.printStackTrace();
return null;
和访问:
String currentLocation = getLoc(lat, lng);
并在清单文件中添加以下权限:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
【讨论】:
JSONArray georesult = json.getJSONArray("results");在这行给我错误 复制该网址并粘贴到浏览器中并测试它是否运行。 "error_message" : "请求中指定的 'sensor' 参数必须设置为 'true' 或 'false'。", "results" : [], "status" : " REQUEST_DENIED" 在您的请求中添加“&sensor=false”或“=true” 我更改了我的代码。这样您就可以正确引用它了。现在检查一下。并在那里传递您的纬度和经度以上是关于使用json而不是Geocoder从谷歌地图获取位置地址的主要内容,如果未能解决你的问题,请参考以下文章