Android positioning and map development instances

Source: Internet
Author: User
Tags reverse geocoding

In Android development, maps and positioning are an indispensable part of many software programs. These special features also bring a lot of convenience to people.

First, we will introduce the main categories in the map package:

Mapcontroller : Main Controls map movement, scaling, and GPS Coordinates as center, control Mapview In View Component, Management Overlay , Provide View . Use multiple map modes (MAP mode (some cities can update traffic conditions in real time), satellite mode, street view mode) to view Google
Map . Common Methods: animateto (geopoint point) setcenter (geopoint point) setzoom (INT zoomlevel.

Mapview: A view used to display a map. It is derived from Android. View. viewgroup. When mapview gets the focus, you can control the movement and scaling of the map. Maps can be displayed in different forms, such as street view mode and satellite mode. The setsatellite (Boolean) settraffic (Boolean) and setstreetview (Boolean) methods are used.

overlay :
Yes mapview top layer, ondraw interface, which is customized in mapview shows some of your own items. mapview by mapview. getoverlays () pair overlay .

Projection:MapviewMediumGPSCoordinate and device coordinate conversion (GeopointAndPoint).

Locate the main categories in the system package:

Locationmanager: This class provides the ability to access the location service and to obtain the best location provider. In addition, the near alert function can also be implemented using this class.

Locationprovider: this class is the abstract class of the positioning provider. The location provider can periodically report the geographical location of devices.

Locationlistener: Provides the callback function when the location information changes. You must register the listener object in the locating manager in advance.

Criteria: This class enables the application to select an appropriate location provider by setting attributes in the locationprovider.

Geocoder: class used to process geocode and reverse geocode. Geocoding refers to converting an address or other description into a longitude or latitude. Reverse geocoding refers to converting a longitude or latitude to an address or a description language, which contains two constructors, the coordinates of the longitude and latitude must be input. The getfromlocation method can obtain an array of addresses.

 

The following describes how to develop a map locating instance. Before developing a map, you mustGet android
MapAPI
KeyThere is a lot of information on the Internet, so I will not repeat it here.

First, you must set the corresponding permissions and maps library in manifest. xml:

<Application Android: icon = "@ drawable/ic_launcher" Android: Label = "@ string/app_name"> <activity Android: Label = "@ string/app_name" Android: name = ". mymapactivity "> <intent-filter> <action Android: Name =" android. intent. action. main "/> <category Android: Name =" android. intent. category. launcher "/> </intent-filter> </activity><Uses-library Android: Name = "com. Google. Android. Maps"/></Application><Uses-Permission Android: Name = "android. permission. internet "/> <uses-Permission Android: Name =" android. permission. access_fine_location "/> <uses-Permission Android: Name =" android. permission. access_coarse_location "/>

Do not forget the red ones above.

Main. XML in layout:

 
<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: Orientation = "vertical"> <COM. google. android. maps. mapview Android: Id = "@ + ID/mapview" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: apikey = "plugin"/> </linearlayout>

Below is the coreCodeImportant:

Public class mymapactivity extends mapactivity {/** called when the activity is first created. */private mapcontroller; private mapview; private myoverlay; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); locationmanager = (locationmanager) getsystemservice (context. location_ser Vice); mapview = (mapview) This. findviewbyid (R. id. mapview); // sets the traffic mode mapview. settraffic (true); // sets the satellite mode mapview. setsatellite (false); // sets the street view mode mapview. setstreetview (false); // sets the zoom control mapview. setbuiltinzoomcontrols (true); mapview. setclickable (true); mapview. setenabled (true); // obtain mapcontroller instance mapcontroller = mapview. getcontroller (); mapcontroller. setzoom (15); myoverlay = new myoverlay (); List <overlay> ov Erlays = mapview. getoverlays (); overlays. add (myoverlay); Criteria = new criteria (); criteria. setaccuracy (criteria. accuracy_fine); criteria. setaltituderequired (false); criteria. setbearingrequired (false); criteria. setcostallowed (false); criteria. setpowerrequirement (criteria. power_low); // criteria string provider = locationmanager. getbestprovider (criteria, true); // get location locat Ion = locationmanager. getlastknownlocation (provider); updatewithlocation (location); // register a periodic update, locationmanager once every 3 seconds. requestlocationupdates (provider, 3000, 0, locationlistener) ;}@ override public Boolean oncreateoptionsmenu (menu) {// todo auto-generated method stub menu. add (0, 1, 1, "traffic mode"); menu. add (, 2, "satellite mode"); menu. add (, 3, "Street View Mode"); return Super. oncreateoptionsmenu (menu);} @ override P Ublic Boolean onoptionsitemselected (menuitem item) {// todo auto-generated method stub super. onoptionsitemselected (item); Switch (item. getitemid () {Case 1: // traffic mode mapview. settraffic (true); mapview. setsatellite (false); mapview. setstreetview (false); break; Case 2: // satellite mode mapview. setsatellite (true); mapview. setstreetview (false); mapview. settraffic (false); break; Case 3: // Street View Mode mapview. setstreetview (true); mapview. Settraffic (false); mapview. setsatellite (false); break; default: mapview. settraffic (true); mapview. setsatellite (false); mapview. setstreetview (false); break;} return true;} private void updatewithlocation (location) {If (location! = NULL) {// set the coordinates of myoverlay for the drawing class. setlocation (location); geopoint = new geopoint (INT) (location. getlatitude () * 1e6), (INT) (location. getlongpolling () * 1e6); // locate the specified coordinate mapcontroller. animateto (geopoint); mapcontroller. setzoom (15) ;}} private final locationlistener = new locationlistener () {@ overridepublic void onstatuschanged (string provider, int status, bundle extras) {// todo auto-Ge Nerated method stub} @ overridepublic void onproviderenabled (string provider) {// todo auto-generated method stub} @ overridepublic void onproviderdisabled (string provider) {// todo auto-generated method stub} // start this function when the coordinates change @ overridepublic void onlocationchanged (location) {// todo auto-generated method stubupdatewithlocation (location) ;}}; class myoverlay extends overlay {private location Locati On; Public void setlocation (location) {This. location = Location ;}@ override public Boolean draw (canvas, mapview, Boolean shadow, long when) {// todo auto-generated method stub super. draw (canvas, mapview, shadow); paint = new paint (); point myscreen = new point (); // change the longitude and latitude to the coordinates of the actual screen. Geopoint = new geopoint (INT) (location. getlatitude () * 1e6), (INT) (location. getlongpolling () * 1e6); mapview. getprojection (). topixels (geopoint, myscreen); paint. setstrokewidth (1); paint. setargb (255,255, 0, 0); paint. setstyle (paint. style. stroke); bitmap BMP = bitmapfactory. decoderesource (getresources (), R. drawable. mypicture); // draw the image to the corresponding position. Canvas. drawbitmap (BMP, myscreen. x, myscreen. y, paint); canvas. drawtext ("Heaven has no path", myscreen. x, myscreen. y, paint); Return true ;}@ overrideprotected Boolean isroutedisplayed () {// todo auto-generated method stubreturn false;} @ overridepublic Boolean onkeydown (INT keycode, keyevent event) {// todo auto-generated method stubif (keycode = keyevent. keycode_back) {alertdialog. builder = new alertdi Alog. Builder (this); builder. setmessage ("are you sure you want to exit? "). Setcancelable (false ). setpositivebutton ("OK", new dialoginterface. onclicklistener () {public void onclick (dialoginterface dialog, int ID) {mymapactivity. this. finish (); android. OS. process. killprocess (Android. OS. process. mypid (); android. OS. process. killprocess (Android. OS. process. mytid (); android. OS. process. killprocess (Android. OS. process. myuid ());}}). setnegativebutton ("return", new dialoginterface. onclicklistener () {public void onclick (dialoginterface dialog, int ID) {dialog. cancel () ;}}); alertdialog Alert = builder. create (); alert. show (); Return true;} return Super. onkeydown (keycode, event );}}

Next, let's take a look at the effect after running:

You can zoom in and out:

However, you can use the menu key to switch between different modes:

The above is switched to the satellite mode. Because a map consumes a lot of network resources, if the network is slow, it will wait for a long time.

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.