Android Google Map
1. Before using the android Google map API, you must check whether the Google map application is installed in the system. The detection method is as follows:
protected boolean checkGoogleMap(){lean isInstallGMap = false; List<PackageInfo>packs = getPackageManager().getInstalledPackages(0);for (int i = 0; i < packs.size(); i++) {PackageInfo p = packs.get(i);if (p.versionName == null) { // system packages continue;}if ("com.google.android.apps.maps".equals(p.packageName)) { isInstallGMap = true; break;}}return isInstallGMap;} |
2. When detecting that the Google map application is not installed in the system, you can switch to Google map for web access, as shown below:
Intent it = new intent (intent. action_view, Uri. parse ("http://ditu.google.cn/maps? Hl = ZH & MRT = loc & Q = "+ weiduextra +", "+ jingduextra +"); startactivity (it); Note: use this method in androidmanifest. add the network access permission <uses-Permission Android: Name = "android. permission. internet "> </uses-Permission> |
3. When the system detects that the Google map application has been installed, we can use the Google map API as follows:
1) Method 1:
Intent it = new intent (intent. action_view, Uri. parse ("Geo:" + weiduextra + "," + jingduextra); startactivity (it); Note: use this method in androidmanifest. <uses-Permission Android: Name = "android. permission. access_coarse_location "/> <uses-Permission Android: Name =" android. permission. internet "/> |
2) Method 2:
You can create a subclass of mapactivity and display mapview on it. You can use mapcontroller to control the displayed coordinates, map mode, and field height, which is very easy to process.
Java code:
Public class maptest extends mapactivity {private mapview; private mapcontroller MC; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. mapview); mapview = (mapview) findviewbyid (R. id. map); mapview. settraffic (true); MC = mapview. getcontroller (); geopoint Gp = new geopoint (INT) (30.659259*1000000), (INT) (104.065762*1 000000); // geographical coordinate MC. animateto (GP); MC. setzoom (12) ;}@ override protected Boolean isroutedisplayed () {return false ;}} public class maptest extends mapactivity {private mapview; private mapcontroller MC; @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. mapview); mapview = (mapview) findviewbyid (R. id. map); mapview. settraffic (True); MC = mapview. getcontroller (); geopoint Gp = new geopoint (INT) (30.659259*1000000), (INT) (104.065762*1000000); // geographic coordinate MC. animateto (GP); MC. setzoom (12) ;}@ overrideprotected Boolean isroutedisplayed () {return false ;}} mapview. the XML content is as follows: XML Code <? XML version = "1.0" encoding = "UTF-8"?> <Relativelayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"> <COM. google. android. maps. mapview Android: Id = "@ + ID/Map" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: enabled = "true" Android: clickable = "true" Android: apikey = "0mhnpl2ns9xpkx6pkwjriv2wj-mehsh71yyx_sq"/> </relativelayout> <? XML version = "1.0" encoding = "UTF-8"?> <Relativelayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"> <COM. google. android. maps. mapview Android: Id = "@ + ID/Map" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: enabled = "true" Android: clickable = "true" Android: apikey = "0mhnpl2ns9xpkx6pkwjriv2wj-mehsh71yyx_sq"/> </relativelayout> |
Note:
A. to use this method, you must add the corresponding access permission to androidmanifest. xml.
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.INTERNET" />; |
B. You need to apply for an apikey.