How do i add a marker on maps in android programming -


so reason when try adding marker geopoint, marker fails show @ given location. tried researching problem reason code seems follow researched on. can guide me in right direction?

import android.content.context; import android.location.criteria; import android.location.geocoder; import android.location.location; import android.location.locationmanager; import android.support.v4.app.fragmentactivity; import android.os.bundle; import android.util.log; import android.widget.toast; import com.google.android.gms.maps.cameraupdatefactory; import com.google.android.gms.maps.googlemap; import com.google.android.gms.maps.supportmapfragment; import com.google.android.gms.maps.model.cameraposition; import com.google.android.gms.maps.model.latlng; import com.google.android.gms.maps.model.markeroptions;  import java.text.decimalformat;  public class mapsactivity extends fragmentactivity {  private googlemap mmap; // might null if google play services apk not available.  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_maps);     setupmapifneeded(); }  @override protected void onresume() {     super.onresume();     setupmapifneeded(); }  /**  * sets map if possible (i.e., google play services apk correctly  * installed) , map has not been instantiated.. ensure ever  * call {@link #setupmap()} once when {@link #mmap} not null.  * <p/>  * if isn't installed {@link supportmapfragment} (and  * {@link com.google.android.gms.maps.mapview mapview}) show prompt user  * install/update google play services apk on device.  * <p/>  * user can return fragmentactivity after following prompt , correctly  * installing/updating/enabling google play services. since fragmentactivity may not  * have been destroyed during process (it  * stopped or paused), {@link #oncreate(bundle)} may not called again should call  * method in {@link #onresume()} guarantee called.  */ private void setupmapifneeded() {     // null check confirm have not instantiated map.     if (mmap == null) {         // try obtain map supportmapfragment.         mmap = ((supportmapfragment) getsupportfragmentmanager().findfragmentbyid(r.id.map))                 .getmap();         // check if successful in obtaining map.         if (mmap != null) {             setupmap();         }     } }  /**  * can add markers or lines, add listeners or move camera. in case,  * add marker near africa.  * <p/>  * should called once , when sure {@link #mmap} not null.  */  private void setupmap() {      mmap.addmarker(new markeroptions().position(new latlng(0, 0)).title("marker").snippet("snippet"));      // enable mylocation layer of google map     mmap.setmylocationenabled(true);      // locationmanager object system service location_service     locationmanager locationmanager = (locationmanager) getsystemservice(context.location_service);      // create criteria object retrieve provider     criteria criteria = new criteria();      // name of best provider     string provider = locationmanager.getbestprovider(criteria, true);      // current location     location mylocation = locationmanager.getlastknownlocation(provider);      // set map type     mmap.setmaptype(googlemap.map_type_normal);      // latitude of current location      location location = locationmanager.getlastknownlocation(locationmanager.gps_provider);      if (location != null) {         double longitude = location.getlongitude();         double latitude = location.getlatitude();           // longitude of current location          // create latlng object current location         latlng latlng = new latlng(latitude, longitude);          // show current location in google map         mmap.movecamera(cameraupdatefactory.newlatlng(latlng));          // zoom in google map         //latlng mycoordinates = new latlng(latitude, longitude);         //cameraupdate yourlocation = cameraupdatefactory.newlatlngzoom(mycoordinates, 20);         //mmap.animatecamera(yourlocation);         mmap.animatecamera(cameraupdatefactory.zoomto(20));         mmap.addmarker(new markeroptions().position(new latlng(latitude, longitude)).title("you here!").snippet("consider located"));           latlng tolatlng = new latlng(40.6937, 73.9859);          mmap.addmarker(new markeroptions().position( new latlng(40.6937, 73.9859)).title("you location here!").snippet("consider located"));      } }            public void onfinish() {             // code here after map rendered         }     } 

the main issue mention:

the marker fails show @ given location

it looks main reason getlastknownlocation() returning null, never gets code sets marker @ current position.

instead of calling method, can set location listener, explicitly request new location.

i cleaned code bit, , got working. note removed use of getlastknownlocation(), , moved logic had in there onlocationchanged() callback.

i added functionality remove previous "current location" marker, , add new 1 every time location changes.

also note 1 marker showing @ equator near africa due line, commented out:

mmap.addmarker(new markeroptions().position(new latlng(0, 0)).title("marker").snippet("snippet")); 

here full updated class code:

import android.content.context; import android.location.location; import android.location.locationlistener; import android.location.locationmanager; import android.support.v4.app.fragmentactivity; import android.os.bundle; import com.google.android.gms.maps.cameraupdatefactory; import com.google.android.gms.maps.googlemap; import com.google.android.gms.maps.supportmapfragment; import com.google.android.gms.maps.model.bitmapdescriptorfactory; import com.google.android.gms.maps.model.latlng; import com.google.android.gms.maps.model.marker; import com.google.android.gms.maps.model.markeroptions; import android.location.criteria;  public class mapsactivity extends fragmentactivity implements locationlistener {      private googlemap mmap; // might null if google play services apk not available.     locationmanager locationmanager;     marker marker;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_maps);         setupmapifneeded();     }      @override     protected void onresume() {         super.onresume();         setupmapifneeded();     }      @override     protected void onpause() {         super.onpause();          //remove location listener         if (locationmanager != null){             locationmanager.removeupdates(this);         }      }      private void setupmapifneeded() {         // null check confirm have not instantiated map.         if (mmap == null) {             // try obtain map supportmapfragment.             mmap = ((supportmapfragment) getsupportfragmentmanager().findfragmentbyid(r.id.map))                     .getmap();             // check if successful in obtaining map.             if (mmap != null) {                 setupmap();             }         }     }      private void setupmap() {          //this marker @ equator near africa         //mmap.addmarker(new markeroptions().position(new latlng(0, 0)).title("marker").snippet("snippet"));          // enable mylocation layer of google map         mmap.setmylocationenabled(true);         mmap.setmaptype(googlemap.map_type_hybrid);         mmap.getuisettings().setzoomcontrolsenabled(true);         mmap.getuisettings().setmylocationbuttonenabled(true);         mmap.getuisettings().setcompassenabled(true);         mmap.getuisettings().setrotategesturesenabled(true);         mmap.getuisettings().setzoomgesturesenabled(true);          // locationmanager object system service location_service         locationmanager = (locationmanager) getsystemservice(context.location_service);          if (locationmanager.isproviderenabled(locationmanager.network_provider)) {             locationmanager.requestlocationupdates(locationmanager.network_provider, 1000, 0, this);         }          if (locationmanager.isproviderenabled(locationmanager.gps_provider)) {             locationmanager.requestlocationupdates(locationmanager.gps_provider, 1000, 0, this);         }       }        public void onfinish() {         // code here after map rendered     }      @override     public void onlocationchanged(location location) {          double longitude = location.getlongitude();         double latitude = location.getlatitude();          // create latlng object current location         latlng latlng = new latlng(latitude, longitude);          // show current location in google map         mmap.movecamera(cameraupdatefactory.newlatlng(latlng));         mmap.animatecamera(cameraupdatefactory.zoomto(15));          if (marker != null) {             marker.remove();         }         marker = mmap.addmarker(new markeroptions().position(new latlng(latitude, longitude))                 .title("you here!").snippet("consider located")                 .icon(bitmapdescriptorfactory.defaultmarker(bitmapdescriptorfactory.hue_magenta)));      }      @override     public void onstatuschanged(string provider, int status, bundle extras) {      }      @override     public void onproviderenabled(string provider) {      }      @override     public void onproviderdisabled(string provider) {      } } 

result:

map image

edit:

you picked interesting spot put hard-coded marker.

i replaced marker code this:

    latlng tolatlng = new latlng(40.6937, 73.9859);      // create latlng object current location     latlng latlng = new latlng(40.6937, 73.9859);      // show current location in google map     mmap.movecamera(cameraupdatefactory.newlatlng(latlng));     mmap.animatecamera(cameraupdatefactory.zoomto(15));      if (marker != null) {         marker.remove();     }     marker = mmap.addmarker(new markeroptions().position(new latlng(40.6937, 73.9859))             .title("you here!").snippet("consider located")             .icon(bitmapdescriptorfactory.defaultmarker(bitmapdescriptorfactory.hue_magenta))); 

and placed marker @ given location. note there strange effect going on due different satellite images.

initial result:

initial

zoomed out see are!

zoomed out


Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -