Android 映射框:如何从LatLng获取地址字符串?
使用Mapbox Maps SDK for Android 8.2.1,如何使用Android 映射框:如何从LatLng获取地址字符串?,android,reverse-geocoding,mapbox-android,Android,Reverse Geocoding,Mapbox Android,使用Mapbox Maps SDK for Android 8.2.1,如何使用LatLng对象获取地址 我试着遵循,但我不知道如何得到结果并将其转换为字符串。我可以看到result是一个CarmenFeature列表,但我不知道如何使用它 我怎样才能做到这一点?下面我尝试使用pointpointFromLatLng和我希望存储地址的字符串,chosenLocationString。谢谢 if(chosenLatLng!=null){ MapboxGeocoding MapboxGeocodin
LatLng
对象获取地址
我试着遵循,但我不知道如何得到结果并将其转换为字符串。我可以看到result
是一个CarmenFeature
列表,但我不知道如何使用它
我怎样才能做到这一点?下面我尝试使用pointpointFromLatLng
和我希望存储地址的字符串,chosenLocationString
。谢谢
if(chosenLatLng!=null){
MapboxGeocoding MapboxGeocoding=MapboxGeocoding.builder()
.accessToken(“我的访问令牌”)
.query(pointFromLatLng)
.build();
mapboxGeocoding.enqueueCall(新回调(){
@凌驾
公共void onResponse(调用、响应){
列表结果=response.body().features();
chosenLocationString=结果。;
如果(results.size()>0){
//记录第一个结果点。
Point firstResultPoint=results.get(0.center();
d(标记“onResponse:+firstResultPoint.toString());
}否则{
//找不到您的请求的结果。
Log.d(标记“onResponse:No result found”);
}
}
@凌驾
失败时公共无效(调用、可丢弃){
printStackTrace();
}
});
Intent returnIntent=新Intent();
returnIntent.putExtra(“位置”,chosenLocationString);
设置结果(Activity.RESULT_OK,returnIntent);
SetEventLocationActivity.this.finish();
}
更新: 这是我当前的实现,它提供了一个空的
特性。address()
<代码>makeGeocodeSearch(chosenLatLng)在onCreate中调用
onCreate():
@覆盖
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u set\u event\u location);
设置位置按钮=(文本视图)findViewById(R.id.set位置按钮);
//单击设置位置按钮后,设置街道地址文本视图,关闭活动
set_location_按钮。setOnClickListener(新视图。OnClickListener(){
@凌驾
公共void onClick(视图v){
if(chosenLatLng!=null){
//这里叫地理编码
makeGeocodeSearch(chosenLatLng);
Intent returnIntent=新Intent();
returnIntent.putExtra(“位置”,chosenLocationString);
设置结果(Activity.RESULT_OK,returnIntent);
SetEventLocationActivity.this.finish();
}否则{
Toast.makeText(getApplicationContext(),
“点击搜索按钮输入事件位置”,
吐司。长度(短)。show();
}
}
});
}
onActivityResult():
@覆盖
受保护的void onActivityResult(int请求代码、int结果代码、意图数据){
super.onActivityResult(请求代码、结果代码、数据);
if(resultCode==Activity.RESULT\u OK&&requestCode==REQUEST\u CODE\u AUTOCOMPLETE){
//检索所选位置的CarmenFeature
CarmenFeature selectedCarmenFeature=PlaceAutocomplete.getPlace(数据);
//创建新功能集合,并使用上面选择的CarmenFeature向其中添加新功能。
//然后检索并更新指定用于显示选定位置的符号层图标的源
if(mapboxMap!=null){
Style Style=mapboxMap.getStyle();
如果(样式!=null){
GeoJsonSource=style.getSourceAs(geojsonSourceLayerId);
如果(源!=null){
source.setGeoJson(FeatureCollection.fromFeatures(
新特性[]{Feature.fromJson(selectedCarmenFeature.toJson())});
}
chosenLatLng=新LatLng(((点)已选择CarmenFeature.geometry()).latitude(),
((点)选择的CarmenFeature.geometry()).longitude());
//将地图摄影机移动到选定位置
mapboxMap.animateCamera(CameraUpdateFactory.newCameraPosition(
新建CameraPosition.Builder()
.target(新车床(((点)已选择CarmenFeature.geometry()).latitude(),
((点)选定的CarmenFeature.geometry()).longitude())
.zoom(14)
.build()),4000);
}
}
}
}
makeGeocodeSearch():
私有void makeGeocodeSearch(LatLng LatLng){
试一试{
//构建Mapbox地理编码请求
MapboxGeocoding客户端=MapboxGeocoding.builder()
.accessToken()
.query(Point.fromLngLat(latLng.getLongitude(),latLng.getLatitude()))
.geocodingTypes(GeocodingCriteria.TYPE\u位置)
.build();
client.enqueueCall(新回调(){
@凌驾
公共void onResponse(调用,
回应(回应){
if(response.body()!=null){
列表结果=response.body().features();
如果(results.size()>0){
//从成功的地理编码响应中获取第一个功能
CarmenFeature feature=results.get(0);
//从CarmenFeature获取地址字符串
private void makeGeocodeSearch(final LatLng latLng) {
try {
// Build a Mapbox geocoding request
MapboxGeocoding client = MapboxGeocoding.builder()
.accessToken(ACCESS_TOKEN)
.query(Point.fromLngLat(latLng.getLongitude(), latLng.getLatitude()))
.geocodingTypes(GeocodingCriteria.TYPE_PLACE)
.build();
client.enqueueCall(new Callback<GeocodingResponse>() {
@Override
public void onResponse(Call<GeocodingResponse> call,
Response<GeocodingResponse> response) {
if (response.body() != null) {
List<CarmenFeature> results = response.body().features();
if (results.size() > 0) {
// Get the first Feature from the successful geocoding response
CarmenFeature feature = results.get(0);
// Get the address string from the CarmenFeature
String carmenFeatureAddress = feature.address();
} else {
Toast.makeText(GeocodingActivity.this, "no results in geocoding request",Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void onFailure(Call<GeocodingResponse> call, Throwable throwable) {
Log.e(TAG,"Geocoding Failure: " + throwable.getMessage());
}
});
} catch (ServicesException servicesException) {
Logd.e(TAG,"Error geocoding: " + servicesException.toString());
servicesException.printStackTrace();
}
}
// Get the first Feature from the successful geocoding response
CarmenFeature feature = results.get(0);
// Get the address string from the CarmenFeature
String carmenFeatureAddress = feature.address();