android - Consolidating Methods For Adding Marker To Map -


in app using if/else statement that, onmaplongclick, update location of marker, if 1 exists, or create marker if 1 not exist. wanted use searchview google places api, utilized tutorial found reading through posts on stack overflow. utilizes search results google places api create marker. nice! however, using own addmarker. so, hoping kind me going in right direction able tie results being generated google places api in method adding marker. (teach man fish, not give man 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 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);     } } 

there might better way it, after tinkering bit, able recycle code method tutorial method, , looks 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 user long clicks on location on map, or selects 1 given search results, there ever appear 1 user-set marker on map. wahoo!


Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -