Question

In my app I am using an if/else statement that, onMapLongClick, will update the location of the marker, if one exists, or create a marker if one does not exist. I wanted to use SearchView with Google Places API, so I utilized a tutorial I found reading through posts on Stack Overflow. It utilizes search results from Google Places API to create a marker. Nice! However, it is using its own addMarker. So, I am hoping someone would be so kind as to get me going in the right direction so that I am able to tie the results being generated from Google Places API in with my method for adding a marker. (Teach the man to fish, not give the man a fish.)

My method:

@Override
public void onMapLongClick(LatLng point) {
 if(marker != null){ //if marker exists (not null or whatever)
     marker.setPosition(point);
 }
 else{
    marker = map.addMarker(new MarkerOptions()
    .position(point)
    .title("Your Destination")
    .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher))
    .draggable(true));
}
    if(marker!=null){
     CameraPosition cameraPosition = new CameraPosition.Builder()
     .target(point)
     .zoom(10)
     .build();
     map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}

Tutorial method for Google-Places-API results:

private void showLocations(Cursor c){
    MarkerOptions markerOptions = null;
    LatLng position = null;
    map.clear();
    while(c.moveToNext()){
        markerOptions = new MarkerOptions();
        position = new LatLng(Double.parseDouble(c.getString(1)),Double.parseDouble(c.getString(2)));
        markerOptions.position(position);
        markerOptions.title(c.getString(0));
        map.addMarker(markerOptions);
    }
    if(position!=null){
        CameraUpdate cameraPosition = CameraUpdateFactory.newLatLng(position);
        map.animateCamera(cameraPosition);
    }
}
Was it helpful?

Solution

There might be a better way to do it, but after tinkering with it a bit, I was able to recycle the code for my method into the tutorial method, and it looks like this:

private void showLocations(Cursor c){

    LatLng position = null;
    while(c.moveToNext()){
        position = new LatLng(Double.parseDouble(c.getString(1)),Double.parseDouble(c.getString(2)));
        if(marker != null){ //if marker exists (not null)
         marker.setPosition(position);
     }
     else{
        marker = map.addMarker(new MarkerOptions()
        .position(position)
        .title("Your Destination")
        .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher))
        .draggable(true));
    }
    if(position!=null){
        CameraUpdate cameraPosition = CameraUpdateFactory.newLatLng(position);
        map.animateCamera(cameraPosition);
    }
    }
}

Now, not matter whether the user long clicks on a location on the map, or selects one from given search results, there will only ever appear one user-set marker on the map. Wahoo!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top