java - Get the exact latitude and longitude in Android -


i trying latitude , longitude of phone, , send location details email address. it's working, i'm not able exact longitude , latitude of current location. not want use map view, trying use gps not able use properly. code:

package com.example.mapq;  import android.location.criteria; import android.location.location; import android.location.locationlistener; import android.location.locationmanager; import android.os.bundle; import android.app.activity; import android.content.context; import android.content.intent; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.textview;  public class mapactivity extends activity implements locationlistener{ locationmanager lm; textview lt, ln; button b1; string provider; location l; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_map); ln=(textview)findviewbyid(r.id.lng); lt=(textview)findviewbyid(r.id.lat); b1=(button) findviewbyid(r.id.button1);  lm=(locationmanager)this.getsystemservice(context.location_service); criteria c=new criteria(); //criteria object select best service based on //accuracy, power consumption, response, bearing , monetary cost //set false use best service otherwise select default sim network //and give location based on sim network  //now first check satellite internet sim network location provider=lm.getbestprovider(c, false); //now have best provider //get location l=lm.getlastknownlocation(provider); if(l!=null) { //get latitude , longitude of location final double lng=l.getlongitude(); final double lat=l.getlatitude(); //display on text view ln.settext(""+lng); lt.settext(""+lat);  b1.setonclicklistener(new onclicklistener() {  @override public void onclick(view v) { intent emailintent = new intent(intent.action_send); emailintent.putextra(intent.extra_email, new string[]{"mallikarjuna8511820@gmail.com"}); emailintent.putextra(intent.extra_cc, new string[]{"mallikarjuna8511820@gmail.com"}); emailintent.putextra(intent.extra_subject, "subject"); emailintent.putextra(intent.extra_text, "body"); emailintent.putextra(android.content.intent.extra_text,                  "http://maps.google.com/maps?q=loc:" + lat +","+ lng); emailintent.settype("plain/text"); startactivity(intent.createchooser(emailintent, "choose email client..."));          }     }); } else { ln.settext("no provider"); lt.settext("no provider"); } } //if want location on changing place use below method //otherwise remove below methods , don't implement location listener @override public void onlocationchanged(location arg0) { double lng=l.getlongitude(); double lat=l.getlatitude(); ln.settext(""+lng); lt.settext(""+lat); }  @override public void onproviderdisabled(string arg0) { // todo auto-generated method stub } @override public void onproviderenabled(string arg0) { // todo auto-generated method stub }  @override public void onstatuschanged(string arg0, int arg1, bundle arg2) { // todo auto-generated method stub } } 

you need requestlocationupdates somewhere in code oncreate/resume like:

lm = (locationmanager) getsystemservice(location_service); lm.requestlocationupdates(locationmanager.gps_provider,                     mupdateperiod * 1000, mminresenddistance, this); 

where typical values might be:

private float mminresenddistance = 10.0f; private int mupdateperiod; // in seconds 

.


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -