Reputation: 692
My app uses the google maps api and the google places api. Everything works perfectly until I search for a certain place, when I search for a place and then select it from the results a dialog box (which should not show) shows up and prompts the user if they want to select that place or change the address. After clicking "select" the app removes the places UI and turns into a regular map, without ever going to the place selected. My code is below.
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();
}
}
Upvotes: 1
Views: 2035
Reputation: 43322
You need to use the data
Intent to get the location of the Place that the user selected, add a Marker to the map, and move the camera so that it's centered over the selected Place:
@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));
}
}
}
Upvotes: 1