我在我的安卓应用程序中添加了PlaceAutocompleteFragment,当我搜索一个地方时,它运行得很好。我的问题是,如何让用户输入纬度和经度值,并在该坐标上放置标记?我只是想隐藏“没有结果找到”部分,让用户点击进入和获得的坐标。
下面是我到目前为止的情况。
<fragment
android:id="@+id/place_autocomplete_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
/>选择一个位置时的代码:
PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment)
getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);
autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
@Override
public void onPlaceSelected(Place place) {
String placeName = place.getName().toString();
LatLng latLng = place.getLatLng();
mMap.addMarker(new MarkerOptions().position(latLng).title(placeName));
mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
}
@Override
public void onError(Status status) {
Log.i(TAG, "An error occurred: " + status);
}
});

我想实现一个搜索栏,这很像谷歌地图。
发布于 2017-01-06 03:25:15
与其使用PlaceAutoCompleteFragment,不如使用PlaceAutocompleteIntent:
try {
Intent intent = new
PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_OVERLAY)
.build(getThis());
startActivityForResult(intent, RequestCodes.PLACE_AUTOCOMPLETE_REQUEST_CODE);
} catch (GooglePlayServicesRepairableException e) {
//Do something
} catch (GooglePlayServicesNotAvailableException e) {
//Do something
}这将发送一个意图,而不是直接显示一个片段。然后,通过onResult方法,您可以捕获故障,并在那里执行反向地理编码:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
switch(requestCode){
case RequestCodes.PLACE_AUTOCOMPLETE_REQUEST_CODE:
switch(resultCode){
case RESULT_OK:
Place place = PlaceAutocomplete.getPlace(this, data);
//Do something with the address you got.
break;
case RESULT_ERROR:
Status status = PlaceAutocomplete.getStatus(this, data);
//In this case, if you typed coordinates instead of an address,
//you can use them to perform a reverse geocoding as you want.
break;
case RESULT_CANCELED:
break;
}
break;
}
} catch (NullPointerException ex) {
ex.printStackTrace();
}
}发布于 2017-01-04 12:48:06
发布于 2017-01-06 12:22:07
@请按照下面的链接查找您的解决方案。
https://developers.google.com/places/android-api/autocomplete#add
在该链接中向下滚动到“以编程方式获取位置预测”部分。
您可以创建一个自定义搜索UI,作为自动完成小部件提供的UI的替代。要做到这一点,应用程序必须以编程的方式获得位置预测。通过调用GeoDataApi.getAutocompletePredictions(),应用程序可以从自动完成服务中获取预测的地名和/或地址列表。
https://stackoverflow.com/questions/41336584
复制相似问题