Common methods used in GPS development and criteria for determining the development of Android location Providergps

Source: Internet
Author: User
Tags gety locale
Summary of common methods of GPS

Get Locationprovider

Java code Public voidGetlocationprovider () { Try{Criteria MCRITERIA01 = NewCriteria ();   Mcriteria01.setaccuracy (Criteria.accuracy_fine); Mcriteria01.setaltituderequired ( false); Mcriteria01.setbearingrequired ( false); Mcriteria01.setcostallowed ( true);   Mcriteria01.setpowerrequirement (Criteria.power_low); Strlocationprovider = Mlocationmanager01.getbestprovider (MCRITERIA01, true);   MLocation01 = Mlocationmanager01.getlastknownlocation (Strlocationprovider); } Catch(Exception e)   {Mtextview01.settext (e.tostring ());   E.printstacktrace (); }   }
public void Getlocationprovider () {try {criteria mCriteria01 = new criteria (); Mcriteria01.setaccuracy (Criteria.accurac Y_fine); Mcriteria01.setaltituderequired (FALSE); Mcriteria01.setbearingrequired (FALSE); Mcriteria01.setcostallowed (TRUE); Mcriteria01.setpowerrequirement (Criteria.power_low); Strlocationprovider = Mlocationmanager01.getbestprovider (MCriteria01, true); MLocation01 = Mlocationmanager01.getlastknownlocation (Strlocationprovider); catch (Exception e) {mtextview01.settext (e.tostring ()); E.printstacktrace ();}

gets the latitude and longitude, and returns the Geopoint object
Java code PrivateGeopoint getgeobylocation (Location Location) {geopoint GP = NULL; Try{ if(Location!= NULL)  { DoubleGeolatitude = Location.getlatitude () *1e6; DoubleGeolongitude = Location.getlongitude () *1e6; GP = NewGeopoint ( int) Geolatitude, ( int) geolongitude); }  } Catch(Exception e)  {E.printstacktrace (); } returnGp }
Private Geopoint getgeobylocation (Location Location) {geopoint GP = null; try {if (Location!= null) {double Geolatitud E = Location.getlatitude () *1e6; Double geolongitude = Location.getlongitude () *1e6; GP = new Geopoint ((int) geolatitude, (int) geolongitude); } catch (Exception e) {e.printstacktrace ();} return GP; }

convert latitude and longitude to actual screen coordinates
Java code point myscreencoords = NewPoint (); Geopoint Tmpgeopoint = NewGeopoint ( int) (Mlocation.getlatitude () *1e6), ( int) (Mlocation.getlongitude () *1e6)); Mapview.getprojection (). Topixels (Tmpgeopoint, myscreencoords);
Point myscreencoords = new Point (); Geopoint tmpgeopoint = new Geopoint (int) (Mlocation.getlatitude () *1e6), (int) (Mlocation.getlongitude () *1e6)); Mapview.getprojection (). Topixels (Tmpgeopoint, myscreencoords);


Click Mapview any point to get the coordinates
Java Code @Override Public BooleanOntouchevent (motionevent ev) { intActionType = Ev.getaction (); Switch(ActionType) { CaseMotionEvent.ACTION_UP:Projection proj = Mapview.getprojection (); Geopoint loc = Proj.frompixels ( int) Arg0.getx (), ( int) arg0.gety ());   String sirina=double.tostring (Loc.getlongitudee6 ()/1000000);     String dolzina=double.tostring (Loc.getlatitudee6 ()/1000000); } return false; }
@Override public boolean ontouchevent (motionevent ev) {int actiontype = ev.getaction (), switch (actiontype) {case Motion Event.ACTION_UP:Projection proj = Mapview.getprojection (); Geopoint loc = proj.frompixels (int) arg0.getx (), (int) arg0.gety ()); String sirina=double.tostring (Loc.getlongitudee6 ()/1000000); String dolzina=double.tostring (Loc.getlatitudee6 ()/1000000); return false; }



change of latitude and longitude to refresh the map
Java code Public voidRefreshmapview () {Geopoint p = NewGeopoint ( int) (dlat* 1E6), ( int) (dlng* 1E6)); Mmapview01.displayzoomcontrols ( true);   Mmapcontroller01.animateto (P);   Mmapcontroller01.setzoom (Intzoomlevel); }
public void Refreshmapview () {geopoint p = new Geopoint (int) (dlat* 1E6), (int) (dlng* 1E6)); Mmapview01.displayzoomcontro LS (true); Mmapcontroller01.animateto (P); Mmapcontroller01.setzoom (Intzoomlevel); }


according to the current latitude and longitude, to obtain some of the relevant address information
Java code//To determine the encoding according to the geographical environment//Note this locale is a Java.util.Locale package class that obtains the current system-Set language Geocoder GC = NewGeocoder (ex09_05. This, Locale.getdefault ()); DoubleGeolatitude = ( int) Gp.getlatitudee6 ()/1e6; DoubleGeolongitude = ( int) Gp.getlongitudee6 ()/1e6;    list<address> lstaddress = gc.getfromlocation (Geolatitude, Geolongitude, 1); StringBuilder SB = NewStringBuilder (); if(Lstaddress.size () > 0) {Address adslocation = lstaddress.get (0); for( intI=0;i<adslocation.getmaxaddresslineindex (); i++) {Sb.append (Adslocation.getaddressline (i)). Append ("\ n");  } sb.append (Adslocation.getlocality ()). Append ("\ n");  Sb.append (Adslocation.getpostalcode ()). Append ("\ n");  Sb.append (Adslocation.getcountryname ()); }
According to the geographical environment to determine the encoding//Note that this Locale is a Java.util.Locale package class, get the current system settings language Geocoder GC = new Geocoder (Ex09_05.this, Locale.getdefault ()); Double geolatitude = (int) gp.getlatitudee6 ()/1e6; Double geolongitude = (int) gp.getlongitudee6 ()/1e6; list<address> lstaddress = gc.getfromlocation (Geolatitude, Geolongitude, 1); StringBuilder sb = new StringBuilder (); if (lstaddress.size () > 0) {Address adslocation = lstaddress.get (0); for (int i=0;i< Adslocation.getmaxaddresslineindex (); i++) {Sb.append (Adslocation.getaddressline (i)). Append ("\ n");} sb.append ( Adslocation.getlocality ()). Append ("\ n"); Sb.append (Adslocation.getpostalcode ()). Append ("\ n"); Sb.append (Adslocation.getcountryname ()); }



obtain its Geopoint object according to the input address
Java code PrivateGeopoint getgeobyaddress (String strsearchaddress) {geopoint GP = NULL; Try{ if(strsearchaddress!= "") {Geocoder MGeocoder01 = NewGeocoder (ex09_07. This, Locale.getdefault ()); list<address> lstaddress = Mgeocoder01.getfromlocationname (strsearchaddress, 1); if(!lstaddress.isempty ()) {Address adslocation = lstaddress.get (0); DoubleGeolatitude = Adslocation.getlatitude () *1e6; DoubleGeolongitude = Adslocation.getlongitude () *1e6; GP = NewGeopoint ( int) Geolatitude, ( int) geolongitude); }   }   } Catch(Exception e)   {E.printstacktrace (); } returnGp }
Private Geopoint getgeobyaddress (String strsearchaddress) {geopoint GP = null; try {if (strsearchaddress!= "") {Geocoder MGeocoder01 = new Geocoder (Ex09_07.this, Locale.getdefault ()); list<address> lstaddress = Mgeocoder01.getfromlocationname (strsearchaddress, 1); if (!lstaddress.isempty ()) {Address adslocation = lstaddress.get (0); Double geolatitude = Adslocation.getlatitude () *1E6 ; Double geolongitude = Adslocation.getlongitude () *1e6; GP = new Geopoint ((int) geolatitude, (int) geolongitude); A catch (Exception e) {e.printstacktrace ();} return GP; }


Map Zoom in Zoom Out button
Java Code MBUTTON02 = (Button) Findviewbyid (R.id.mybutton2); Mbutton02.setonclicklistener ( NewButton.onclicklistener () { Public voidOnClick (View v) {intzoomlevel++; if(Intzoomlevel>mmapview01.getmaxzoomlevel ())   {intzoomlevel = Mmapview01.getmaxzoomlevel ();   } mmapcontroller01.setzoom (Intzoomlevel);     }   });   MButton03 = (Button) Findviewbyid (R.id.mybutton3); Mbutton03.setonclicklistener ( NewButton.onclicklistener () { Public voidOnClick (View v) {intzoomlevel--; if(intzoomlevel<1)   {intzoomlevel = 1;   } mmapcontroller01.setzoom (Intzoomlevel); }   });
MBUTTON02 = (Button) Findviewbyid (R.id.mybutton2); Mbutton02.setonclicklistener (New Button.onclicklistener () {public void OnClick (View v) {intzoomlevel++; if ( Intzoomlevel>mmapview01.getmaxzoomlevel ()) {intzoomlevel = Mmapview01.getmaxzoomlevel ();} Mmapcontroller01.setzoom (Intzoomlevel); } }); MButton03 = (Button) Findviewbyid (R.id.mybutton3); Mbutton03.setonclicklistener (New Button.onclicklistener () {public void OnClick (View v) {intzoomlevel--; if ( intzoomlevel<1) {intzoomlevel = 1;} mmapcontroller01.setzoom (Intzoomlevel); } });


Reproduced in the following article: http://marshal.easymorse.com/archives/2528
There are two Android location provider:

* LocationManager.GPS_PROVIDER:GPS, high precision, but slow and consumption of electricity, and may be due to weather reasons or obstacles to obtain satellite information, the other equipment may not have GPS modules;
* Locationmanager.network_provider: Through the network to obtain positioning information, low precision, less power consumption, access to information faster, do not rely on GPS modules.

For the universality of the program, we hope to choose location provider dynamically. To Android through the location API display address information made a specific change, you can see the use of GPS positioning, high precision:



The criteria is used here to automatically choose which location provider to use depending on the current device situation. See
Java code Locationmanager Locationmanager = (locationmanager) getsystemservice (Context.location_service); Criteria criteria = NewCriteria (); Criteria.setaccuracy (criteria.accuracy_fine);//set to maximum precision criteria.setaltituderequired ( false)//Does not require elevation information criteria.setbearingrequired ( false)//Does not require azimuth information criteria.setcostallowed ( true)//Whether it is permissible to pay criteria.setpowerrequirement (criteria.power_low);//The requirement for electricity is location = Locationmanager. Getlastknownlocation (Locationmanager.getbestprovider (criteria, true));
Locationmanager Locationmanager = (locationmanager) getsystemservice (Context.location_service); Criteria criteria = new criteria (); Criteria.setaccuracy (criteria.accuracy_fine);//set to Maximum precision criteria.setaltituderequired (false)/No elevation information required Criteria.setbearingrequired (false);//do not require azimuth information criteria.setcostallowed (TRUE);//whether to allow payment criteria.setpowerrequirement (criteria.power_low);//The demand for electricity location = Locationmanager. Getlastknownlocation (Locationmanager.getbestprovider ( Criteria, true));

The original wording is simple: Java code locationmanager Locationmanager = (locationmanager) getsystemservice (Context.location_service); Location=locationmanager.getlastknownlocation (locationmanager.network
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.