如何用Google Place API从经纬度获取地址?打车APP开发技术问询
把LatLng转换为具体地址的实现方案
嘿,我之前在开发打车类APP的时候刚好处理过这个需求,给你分享两种靠谱的实现方式,都是基于Google的服务来做反向地理编码(把经纬度转成地址):
方式一:用Android内置的Geocoder类(简单快捷)
Android系统自带了Geocoder类,可以直接完成经纬度到地址的转换,集成起来很方便:
步骤1:添加网络权限
首先确保你的AndroidManifest.xml里已经添加了互联网权限:
<uses-permission android:name="android.permission.INTERNET" />
步骤2:在地图回调里添加转换逻辑
把下面的代码集成到你现有的onCameraIdle方法里,或者单独抽成一个工具方法:
// 假设你的Context是当前Activity,可根据实际情况调整 private void convertLatLngToAddress(LatLng pinLocation) { Geocoder geocoder = new Geocoder(this, Locale.getDefault()); try { // 获取匹配的地址列表,参数3是返回结果的最大数量 List<Address> addresses = geocoder.getFromLocation( pinLocation.latitude, pinLocation.longitude, 1 ); if (addresses != null && !addresses.isEmpty()) { Address address = addresses.get(0); // 方式1:直接获取完整的格式化地址 String fullAddress = address.getAddressLine(0); Log.d("Address", "完整地址:" + fullAddress); // 方式2:单独提取地址的各个组成部分(按需使用) String streetName = address.getThoroughfare(); // 街道名称 String city = address.getLocality(); // 城市 String country = address.getCountryName(); // 国家 String postalCode = address.getPostalCode(); // 邮编 // 这里可以把地址更新到你的UI控件,比如显示上车点的TextView } else { Log.w("Address", "没有找到匹配的地址"); } } catch (IOException e) { e.printStackTrace(); Toast.makeText(this, "地址解析失败,请检查网络", Toast.LENGTH_SHORT).show(); } }
然后在你的onCameraIdle里调用这个方法:
mMap.setOnCameraIdleListener(new GoogleMap.OnCameraIdleListener() { @Override public void onCameraIdle() { pinLocation = mMap.getCameraPosition().target; setPickupLocationPrefs(pinLocation); // 调用地址转换方法 convertLatLngToAddress(pinLocation); } });
注意点
- 部分设备可能不支持
Geocoder服务,建议使用前加判断:if (Geocoder.isPresent()) { ... } - 该方法可能返回null或空列表,一定要做非空判断
方式二:调用Google Maps Geocoding API(更稳定准确)
如果内置Geocoder的结果不够准确或某些地区不支持,可以直接调用官方的Geocoding API,这种方式更可靠,但需要申请API Key并处理网络请求:
步骤1:准备API Key
- 登录Google云控制台,创建项目
- 启用Geocoding API服务
- 生成API Key,并限制使用范围(比如仅允许你的APP包名调用)
步骤2:添加依赖和权限
- 同样需要
INTERNET权限 - 用OkHttp做网络请求,先在
build.gradle添加依赖:
implementation 'com.squareup.okhttp3:okhttp:4.11.0'
步骤3:发送网络请求并解析结果
private void convertLatLngToAddressViaAPI(LatLng pinLocation) { OkHttpClient client = new OkHttpClient(); String apiKey = "你的API_KEY"; // 替换成自己的API Key String url = String.format( "https://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f&key=%s", pinLocation.latitude, pinLocation.longitude, apiKey ); Request request = new Request.Builder().url(url).build(); client.newCall(request).enqueue(new Callback() { @Override public void onFailure(@NotNull Call call, @NotNull IOException e) { e.printStackTrace(); runOnUiThread(() -> { Toast.makeText(MainActivity.this, "地址请求失败", Toast.LENGTH_SHORT).show(); }); } @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { if (response.isSuccessful() && response.body() != null) { String jsonResponse = response.body().string(); try { JSONObject jsonObject = new JSONObject(jsonResponse); JSONArray results = jsonObject.getJSONArray("results"); if (results.length() > 0) { JSONObject firstResult = results.getJSONObject(0); // 获取格式化后的完整地址 String formattedAddress = firstResult.getString("formatted_address"); // 回到主线程更新UI runOnUiThread(() -> { Log.d("API Address", formattedAddress); // 更新你的地址显示控件 }); } } catch (JSONException e) { e.printStackTrace(); } } } }); }
注意点
- 网络请求必须在子线程执行,不能放在主线程
- 注意API的配额限制,免费额度有限,超出后会收费
- 在
AndroidManifest.xml里添加API Key配置:
<meta-data android:name="com.google.android.geo.API_KEY" android:value="你的API_KEY" />
两种方式各有优劣,追求快速集成优先用Geocoder;需要更稳定的地址解析结果就用官方API。
内容的提问来源于stack exchange,提问作者Abdalltif Basher
相关产品推荐
相关产品推荐

