android - Passing current location to Marker -
i writing application opens maps app , able add marker
object @ current location when opens. application correctly opens maps app , shows blue dot current location, having difficulty using current location in order create marker
.
here source code:
import com.google.android.gms.common.connectionresult; import com.google.android.gms.common.googleplayservicesclient; import com.google.android.gms.common.googleplayservicesutil; import com.google.android.gms.location.locationclient; import com.google.android.gms.location.locationlistener; import com.google.android.gms.location.locationrequest; import com.google.android.gms.maps.cameraupdate; import com.google.android.gms.maps.cameraupdatefactory; import com.google.android.gms.maps.googlemap; import com.google.android.gms.maps.model.latlng; import com.google.android.gms.maps.model.markeroptions; import com.google.android.gms.maps.mapfragment; import com.google.android.gms.maps.supportmapfragment; import android.location.location; import android.location.locationmanager; import android.os.bundle; import android.provider.settings; import android.app.activity; import android.app.dialog; import android.content.intent; import android.support.v4.app.fragmentactivity; import android.util.log; import android.widget.toast; public class placemarker extends fragmentactivity implements googleplayservicesclient.connectioncallbacks, googleplayservicesclient.onconnectionfailedlistener, locationlistener { private final static int connection_failure_resolution_request = 9000; private locationclient mlocationclient = null; private locationrequest mlocationrequest = null; private googlemap mmap; private static final int update_interval_in_seconds = 5; private static final int milliseconds_per_second = 1000; private static final long update_interval = milliseconds_per_second * update_interval_in_seconds; private static final int fastest_interval_in_seconds = 1; private static final long fastest_interval = milliseconds_per_second * fastest_interval_in_seconds; private locationmanager locationmanager; private location location = null; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_place_marker); mmap = ((supportmapfragment) getsupportfragmentmanager().findfragmentbyid(r.id.map)).getmap(); mmap.setmaptype(googlemap.map_type_normal); mmap.setmylocationenabled(true); mlocationclient = new locationclient(this, this, this); if (servicesconnected()) { mlocationrequest = locationrequest.create(); mlocationrequest.setpriority(locationrequest.priority_high_accuracy); mlocationrequest.setinterval(update_interval); mlocationrequest.setfastestinterval(fastest_interval); double mlatitude = getcurrentlocation().getlatitude(); double mlongitude = getcurrentlocation().getlongitude(); mmap.addmarker(new markeroptions() .position(new latlng(mlatitude, mlongitude)) .title("title test") .snippet("snippet test")); } else { toast.maketext(this, "position unavailable", toast.length_short).show(); } } @override protected void onstart() { super.onstart(); mlocationclient.connect(); } @override protected void onstop() { if (mlocationclient.isconnected()) { mlocationclient.removelocationupdates(this); } mlocationclient.disconnect(); super.onstop(); } private location getcurrentlocation() { location location = mlocationclient.getlastlocation(); if (location != null) { return location; } else { toast.maketext(this, "current location unavailable", toast.length_short).show(); checkforgpsandpromptopen(); return null; } } private void checkforgpsandpromptopen() { boolean enabled = locationmanager.isproviderenabled(locationmanager.gps_provider); if (!enabled) { intent intent = new intent(settings.action_location_source_settings); startactivity(intent); } } // handle results returned fragmentactivity google play services @override protected void onactivityresult(int requestcode, int resultcode, intent data) { // decide based on original request code switch (requestcode) { case connection_failure_resolution_request : // if result code activity.result_ok, try connect again switch (resultcode) { case activity.result_ok : // try request again break; } } } private boolean servicesconnected() { // check google play services available int resultcode = googleplayservicesutil.isgoogleplayservicesavailable(this); // if google play services available if (connectionresult.success == resultcode) { // in debug mode, log status log.d("location updates", "google play services available."); // continue return true; // google play services not available reason } else { // error code // error dialog google play services dialog errordialog = googleplayservicesutil.geterrordialog(resultcode, this, connection_failure_resolution_request); // if google play services can provide error dialog if (errordialog != null) { errordialog.show(); } return false; } } /* * called location services when request connect client finishes successfully. * @ point, can request current location or start periodic updates */ @override public void onconnected(bundle databundle) { // display connection status toast.maketext(this, "connected", toast.length_short).show(); mlocationclient.requestlocationupdates(mlocationrequest, this); location = getcurrentlocation(); taketolocation(convertlocationtolatlong(location)); } /* * called location services if connection location client drops because of error. */ @override public void ondisconnected() { // display connection status toast.maketext(this, "disconnected. please re-connect.", toast.length_short).show(); } public void onlocationchanged(location location) { // report ui location updated // string msg = "updated location: " + double.tostring(location.getlatitude()) + "," + double.tostring(location.getlongitude()); // toast.maketext(this, msg, toast.length_short).show(); } /* * called location services if attempt location services fails. */ @override public void onconnectionfailed(connectionresult connectionresult) { } private void taketolocation(latlng newlocation) { if (newlocation != null) { cameraupdate update = cameraupdatefactory.newlatlngzoom(newlocation, 16); mmap.animatecamera(update); } else { toast.maketext(this, "position unavailable", toast.length_short).show(); } } private latlng convertlocationtolatlong(location location) { latlng currentlatlong = new latlng(location.getlatitude(), location.getlongitude()); return currentlatlong; } }
the logcat error shows there illegalstateexception: not connected
caused line 61:
double mlatitude = getcurrentlocation().getlatitude();
i guess if have third party crash catching sdk used in project if getcurrentlocation() returns null in following lines:
double mlatitude = getcurrentlocation().getlatitude(); double mlongitude = getcurrentlocation().getlongitude();
it happen sdk catches npe fails send report. cuz method getcurrentlocation() return null:
toast.maketext(this, "current location unavailable", toast.length_short).show(); checkforgpsandpromptopen(); return null;
Comments
Post a Comment