【问题标题】:Android get lat&long from google addressAndroid 从谷歌地址获取 lat&long
【发布时间】:2014-02-10 11:48:41
【问题描述】:
【问题讨论】:
标签:
android
google-places-api
【解决方案1】:
使用方法
public List<Address> getFromLocationName (String locationName, int maxResults) 来自 Android Geocoder API,输入您想要的位置名称和最大结果数,您应该可以开始了。
例如。
Geocoder coder = new Geocoder(this);
try {
ArrayList<Address> adresses = (ArrayList<Address>) coder.getFromLocationName("Some Address", 10);
for(Address add : adresses){
double longitude = add.getLongitude();
double latitude = add.getLatitude();
}
} catch (IOException e) {
e.printStackTrace();
} catch(IllegalArgumentException e){
e.printStackTrace();
}
【解决方案2】:
如果有帮助,我最近用 Java 为 Google Places API 创建了一个库。
自动补全很简单:
GooglePlaces client = new GooglePlace("apiKey");
List<Prediction> predictions = client.getPlacePredictions("Empire");
for (Prediction prediction : predictions) {
String description = prediction.getDescription();
// etc etc
}
从地址中获取经纬度就像这样简单。
List<Place> places = client.getPlacesByQuery(address, GooglePlaces.MAXIMUM_RESULTS);
for (Place place : places) {
if (place.getAddress().equals(address)) {
double lat = place.getLatitude();
double lng = place.getLongitude();
}
}
https://github.com/windy1/google-places-api-java
【解决方案3】:
您可以简单地使用 google maps api 来获取经纬度
http://maps.google.com/maps/api/geocode/json?address=&sensor=false
在上面的链接中,您必须在“address=”旁边添加地址,您可以获取带有 lat 和 long 以及其他一些信息的 json 数据。
【解决方案4】:
试试 GeoDataClient。参考GeoDataClient和Place IDs and details。
geoDataClient.getPlaceById(autoCompletePredictionItem.getPlaceId())
.addOnCompleteListener(new OnCompleteListener<PlaceBufferResponse>() {
@Override
public void onComplete(@NonNull Task<PlaceBufferResponse> task) {
if (task.isSuccessful()) {
PlaceBufferResponse places = task.getResult();
Place myPlace = places.get(0);
Log.e(TAG, "Place found: " + myPlace.getLatLng().toString());
places.release();
} else {
Log.e(TAG, "Place not found.");
}
}
});
【解决方案5】:
根据更新的文档,GeoDAtaClient 已被弃用。新方法是使用这个:
// Define a Place ID. val placeId = "INSERT_PLACE_ID_HERE"
// Specify the fields to return. val placeFields = listOf(Place.Field.ID, Place.Field.NAME)
// Construct a request object, passing the place ID and fields array. val request = FetchPlaceRequest.newInstance(placeId, placeFields)
placesClient.fetchPlace(request)
.addOnSuccessListener { response: FetchPlaceResponse ->
val place = response.place
Log.i(PlaceDetailsActivity.TAG, "Place found: ${place.name}")
}.addOnFailureListener { exception: Exception ->
if (exception is ApiException) {
Log.e(TAG, "Place not found: ${exception.message}")
val statusCode = exception.statusCode
TODO("Handle error with given status code")
}
}
https://developers.google.com/maps/documentation/places/android-sdk/place-details#maps_places_get_place_by_id-kotlin