我的应用使用谷歌地图api和谷歌地方api。一切都很完美,直到我搜索某个地方,当我搜索一个地方,然后从结果中选择一个对话框(不应显示)时,会显示并提示用户是否要选择该地点或更改地址。单击“选择”后,应用程序将删除场所UI并转换为常规地图,而无需前往所选位置。我的代码如下。
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
private static final int PLACE_PICKER_REQUEST = 1000;
private GoogleApiClient mClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
int PLACE_PICKER_REQUEST = 1;
PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();
try {
startActivityForResult(builder.build(this), PLACE_PICKER_REQUEST);
} catch (GooglePlayServicesRepairableException e) {
e.printStackTrace();
} catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
}
mClient = new GoogleApiClient
.Builder(this)
.addApi(Places.GEO_DATA_API)
.addApi(Places.PLACE_DETECTION_API)
.build();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PLACE_PICKER_REQUEST) {
if (resultCode == RESULT_OK) {
Place place = PlacePicker.getPlace(data, this);
StringBuilder stBuilder = new StringBuilder();
String placename = String.format("%s", place.getName());
String latitude = String.valueOf(place.getLatLng().latitude);
String longitude = String.valueOf(place.getLatLng().longitude);
String address = String.format("%s", place.getAddress());
stBuilder.append("Name: ");
stBuilder.append(placename);
stBuilder.append("\n");
stBuilder.append("Latitude: ");
stBuilder.append(latitude);
stBuilder.append("\n");
stBuilder.append("Logitude: ");
stBuilder.append(longitude);
stBuilder.append("\n");
stBuilder.append("Address: ");
stBuilder.append(address);
}
}
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
@Override
protected void onStart() {
super.onStart();
mClient.connect();
}
@Override
protected void onStop() {
mClient.disconnect();
super.onStop();
}
}
答案 0 :(得分:1)
您需要使用data
意图来获取用户所选地点的位置,将标记添加到地图中,然后移动相机以使其位于所选地点的中心位置:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PLACE_PICKER_REQUEST) {
if (resultCode == RESULT_OK) {
Place place = PlacePicker.getPlace(data, this);
String placeName = String.format("Place: %s", place.getName());
String placeAddress = String.format("Address: %s", place.getAddress());
LatLng toLatLng = place.getLatLng();
// Add Marker
marker = mMap.addMarker(new MarkerOptions().position(toLatLng)
.title(placeName).snippet(placeAddress)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA)));
// Move Camera to selected place
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng,11));
}
}
}