1 main.xml
<? 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"> <textview Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "@ string/Hello"/> <! -- Use a third-party package instead of this project package --> <COM. google. android. maps. mapview Android: Id = "@ + ID/mapview" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: apikey = "plugin"/> </linearlayout>
2 androidmanifest. xml
<? XML version = "1.0" encoding = "UTF-8"?> <Manifest xmlns: Android = "http://schemas.android.com/apk/res/android" package = "com. CJF. map "Android: versioncode =" 1 "Android: versionname =" 1.0 "> <uses-SDK Android: minsdkversion =" 10 "/> <uses-Permission Android: name = "android. permission. internet "/> <! -- Network is required --> <uses-Permission Android: Name = "android. Permission. access_fine_location"/> <! -- GPS --> <uses-Permission Android: Name = "android. Permission. access_coarse_location"/> <! -- Net --> "<application Android: icon =" @ drawable/ic_launcher "Android: Label =" @ string/app_name "> <uses-library Android: Name =" com. google. android. maps "/> <! -- You need to add a third-party library --> <activity Android: Name = ". mainactivity "Android: Label =" @ string/app_name "> <intent-filter> <action Android: Name =" android. intent. action. main "/> <category Android: Name =" android. intent. category. launcher "/> </intent-filter> </activity> </Application> </manifest>
3. mainactivity
Package COM. CJF. map; import Java. util. list; import COM. google. android. maps. geopoint; import COM. google. android. maps. mapactivity; import COM. google. android. maps. mapcontroller; import COM. google. android. maps. mapview; import COM. google. android. maps. mylocationoverlay; import COM. google. android. maps. overlay; import android. app. activity; import android. content. context; import android. graphics. bitmap; import android. Graphics. bitmapfactory; import android. graphics. canvas; import android. graphics. paint; import android. graphics. point; import android. location. location; import android. location. locationlistener; import android. location. locationmanager; import android. OS. bundle; import android. view. menu; import android. view. menuitem; import android. widget. toast;/*** inherits the mapactivity abstract class, which is also inherited from activity * locationmanager and needs to listen for changes to geographic location information This interface locationlistener is required for multiple times, so you can directly implement **/public class mainactivity extends mapactivity implements locationlistener {private mapview; private mapcontroller mmapcontroller; private geopoint mgeopoint; private locationmanager mlocationmanager; private location mlocation; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); MA Pview = (mapview) findviewbyid (R. id. mapview);/*** 1. Map loading */mapview. setbuiltinzoomcontrols (true); // supports mapview scaling. setclickable (true); // set this method to interact with the user mapview. setkeepscreenon (true); mapview. setlongclickable (true); mapview. setstreetview (true); // set it to street view mode/*** 2. Add the location setting function */mmapcontroller = mapview. getcontroller (); mmapcontroller. setzoom (18); // set the map zoom level/*** 3. For a given latitude and longitude, display Chengdu */updatemapshow (30.659259, 104. 065765);/*** 4. Add a mark and tag to the location on the map (note that it is not on the same layer as the map) */mapview. getoverlays (). add (New myoverlay ();/*** 5 to implement the locating function */mlocationmanager = (locationmanager) getsystemservice (context. location_service); // 1, obtain our location information mlocation = mlocationmanager. getlastknownlocation (locationmanager. gps_provider); // use GPS and the fine if (mlocation! = NULL) {// 2, update our location information updatemapshow (mlocation. getlatitude (), mlocation. getlongpolling ();} else {mlocation = mlocationmanager. getlastknownlocation (locationmanager. network_provider); // use the network, and the permission corase if (mlocation! = NULL) updatemapshow (mlocation. getlatitude (), mlocation. getlongpolling (); else toast. maketext (this, "geographic location information not available", 3 ). show ();}/*** 6, register location tracking --- network tracking/GPS tracking * parameter 1: Location provider * parameter 2: Minimum Update Time, the time is in milliseconds. * parameter 3: the minimum number of long-distance updates is one time. Unit: * parameter 4: register the listening event locationlistener */mlocationmanager. requestlocationupdates (locationmanager. network_provider, 3000, 5, this); mlocationmanager. requestlocationupdates (locationmanager. gps_provide R, 1000, 1, this);}/*** display * @ Param lat * @ Param LNG */private void updatemapshow (double Lat, double LNG) According to coordinate update) {double lati = lat * 1e6; double lngi = LNG * 1e6; mgeopoint = new geopoint (lati. intvalue (), lngi. intvalue (); mmapcontroller. animateto (mgeopoint); mmapcontroller. setcenter (mgeopoint); // the center point is in the upper left corner of the image. // mmapcontroller. setzoom (15);}/*** defines the internal class. You can add tags to the map. Here, add an image on the map to mark the current position * @ author Administrator **/Class myoverlay extends overlay {@ overridepublic Boolean draw (canvas, mapview, Boolean shadow, long when) {// obtain an image Bitmap bitmap = bitmapfactory. decoderesource (getresources (), R. drawable. ic_launcher); point out = NULL; // The returned value is still a pointout = mapview. getprojection (). topixels (mgeopoint, out); // you can convert the geographical coordinates to a specific position on the screen. Paint paint = new paint (); // you do not need to fill in or set if (OUT! = NULL) canvas. drawbitmap (bitmap, out. x, out. y, paint); // Add a label. // the return value can be true or not modified. // return Super. draw (canvas, mapview, shadow, when); Return true;}/*** print the geographic location information when you click it */@ overridepublic Boolean ONTAP (geopoint P, mapview) {toast. maketext (mainactivity. this, "you clicked:" + P. getlatitudee6 () + ";" + P. getlongitudee6 (), toast. length_long ). show (); return Super. ONTAP (p, mapview) ;}}/*** display route information */@ overridepro Tected Boolean isroutedisplayed () {// todo auto-generated method stubreturn false;}/*** add three menus, select three display modes for the map */@ overridepublic Boolean oncreateoptionsmenu (menu) {menu. add (1, 1, 1, "traffic map"); menu. add (1, 2, 3, "satellite map"); menu. add (1, 3, 3, "street view map"); return Super. oncreateoptionsmenu (menu) ;}@ overridepublic Boolean onoptionsitemselected (menuitem item) {Switch (item. getitemid () {Case 1: mapview. settraffic (tr UE); // because the three modes are three different layers, only one layer mapview can be displayed at a time. setsatellite (false); mapview. setstreetview (false); break; Case 2: mapview. settraffic (false); mapview. setsatellite (true); mapview. setstreetview (false); break; Case 3: mapview. settraffic (false); mapview. setsatellite (false); mapview. setstreetview (true); break;} return Super. onoptionsitemselected (item);} // ----------- method ---------/*** triggered when the location is changed in the following methods in the locationlistener Interface * /Public void onlocationchanged (location) {If (location! = NULL) updatemapshow (location. getlatitude (), location. getlongpolling (); toast. maketext (mainactivity. this, "your current location:" + location. getlatitude () + ";" + location. getlongpolling (), 3000 ). show ();} public void onproviderdisabled (string provider) {}/*** trigger */Public void onproviderenabled (string provider) {If (provider. equals (locationmanager. network_provider) {toast. maketext (mainactivity. this, "available for Mobile Networks", 3000);} else if (provider. equals (locationmanager. gps_provider) {toast. maketext (mainactivity. this, "available GPS", 3000) ;}} public void onstatuschanged (string provider, int status, bundle extras) {// todo auto-generated method stub} // remove the listener @ overrideprotected void onpause () {mlocationmanager. removeupdates (this); super. onpause () ;}@ overrideprotected void onresume () {mlocationmanager. requestlocationupdates (locationmanager. network_provider, 3000, 5, this); mlocationmanager. requestlocationupdates (locationmanager. gps_provider, 1000, 1, this); super. onresume ();}}