ANDROID 开发:谷歌地图开发入门(3)--- google map api v3 入门教程辅助

原创:http://blog.csdn.net/springyama/article/details/8518459

google map api v3是不用密钥的,其是将网页加载到ANDROID的WEBVIEW控件中来实现。以下链接是官方开发指南,但是由于某些原因,照搬指南可能会不成功。

https://developers.google.com/maps/documentation/javascript/tutorial?hl=zh-CN

 

以下是我经过实践的步骤:

一、新建一个ANDROID 项目,目录应该如下(其中“images”文件夹为我后来添加的),

 

 接着在assets文件夹下新建一个HTML文件“map_v3.html”,内容如下:

 

[html]  view plain copy
  1. <html>  
  2. <head>  
  3. <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />  
  4. <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>  
  5. <title>this is title.</title>  
  6.   
  7. <script type="text/javascript"  
  8.     src="http://maps.google.com/maps/api/js?sensor=true">  
  9. </script>  
  10. <script type="text/javascript">  
  11.   var map;  
  12.   function initialize() {  
  13.     var latitude = 0;  
  14.     var longitude = 0;  
  15.     if (window.android){  
  16.       latitude = window.android.getLatitude();  
  17.       longitude = window.android.getLongitude();  
  18.     }  
  19.       
  20.     var myLatlng = new google.maps.LatLng(latitude,longitude);  
  21.     var myOptions = {  
  22.       zoom: 8,  
  23.       center: myLatlng,  
  24.       mapTypeId: google.maps.MapTypeId.ROADMAP  
  25.     }  
  26.     map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);  
  27.       
  28.     var marker = new google.maps.Marker({  
  29.       position: myLatlng,   
  30.       map: map,    
  31.       title:"Hello World,I am here!"  
  32.   });     
  33.     // To add the marker to the map, call setMap();  
  34.    //marker.setMap(map);    
  35.   
  36.   }  
  37.   function centerAt(latitude, longitude){  
  38.     myLatlng = new google.maps.LatLng(latitude,longitude);  
  39.     map.panTo(myLatlng);  
  40.       
  41.   }  
  42.   
  43. </script>  
  44.   
  45. </head>  
  46. <body style="margin:0px; padding:0px;" onload="initialize()">  
  47.   <div id="map_canvas" style="width:100%; height:100%"></div>  
  48. </body>  
  49. </html>  


二、在“layout”文件夹下新建一个XML文件“map_v3.xml”,在其中放置一个WebView控件,具体代码如下:

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.   xmlns:android="http://schemas.android.com/apk/res/android"  
  4.   android:layout_width="match_parent"  
  5.   android:layout_height="match_parent">  
  6.   <WebView  
  7.   android:layout_width="match_parent"  
  8.   android:layout_height="match_parent"  
  9.   android:id="@+id/webview01"  
  10.   ></WebView>  
  11. </LinearLayout>  


三、在"src"文件夹下新建一个“WebMapActivityJSInterface.java“文件,代码如下:

 

[java]  view plain copy
  1.    
  2.   
  3. package test.com;  
  4.   
  5. import android.app.Activity;  
  6. import android.content.Context;  
  7. import android.content.pm.ActivityInfo;  
  8. import android.location.Location;  
  9. import android.location.LocationListener;  
  10. import android.location.LocationManager;  
  11. import android.location.Criteria;  
  12. import android.os.Bundle;  
  13. import android.webkit.WebView;  
  14. import android.webkit.WebViewClient;  
  15.   
  16. public class WebMapActivityJSInterface extends Activity implements LocationListener {  
  17. //  private static final String MAP_URL = "http://gmaps-samples.googlecode.com/svn/trunk/articles-android-webmap/simple-android-map.html";  
  18.   private static final String MAP_URL = "file:///android_asset/map_v3.html";  
  19.   private WebView webView;  
  20.   private Location mostRecentLocation;  
  21.   
  22.   @Override  
  23.   /** Called when the activity is first created. */  
  24.   public void onCreate(Bundle savedInstanceState) {  
  25.     super.onCreate(savedInstanceState);  
  26.     setContentView(R.layout.map_v3);  
  27.     getLocation();  
  28.     setupWebView();  
  29.     this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);  
  30.   
  31.   }  
  32.   /** Sets up the WebView object and loads the URL of the page **/  
  33.   private void setupWebView(){  
  34.   
  35.     webView = (WebView) findViewById(R.id.webview01);  
  36.     webView.getSettings().setJavaScriptEnabled(true);  
  37.     webView.setWebViewClient(new WebViewClient());  
  38.     webView.loadUrl(MAP_URL);    
  39.   
  40.     /** Allows JavaScript calls to access application resources **/  
  41.   //  webView.addJavascriptInterface(new JavaScriptInterface(), "android");  
  42.   
  43.   }  
  44.   
  45.   /** Sets up the interface for getting access to Latitude and Longitude data from device **/  
  46.   private class JavaScriptInterface {  
  47.     public double getLatitude(){  
  48.       return mostRecentLocation.getLatitude();  
  49.     }   
  50.     public double getLongitude(){  
  51.       return mostRecentLocation.getLongitude();  
  52.     }  
  53.   
  54.   }  
  55.   
  56.   
  57.   /** The Location Manager manages location providers. This code searches 
  58.         for the best provider of data (GPS, WiFi/cell phone tower lookup, 
  59.         some other mechanism) and finds the last known location. 
  60.    **/  
  61.   private void getLocation() {        
  62.     LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);  
  63.     Criteria criteria = new Criteria();  
  64.     criteria.setAccuracy(Criteria.ACCURACY_FINE);  
  65.     String provider = locationManager.getBestProvider(criteria,true);  
  66.   
  67.     //In order to make sure the device is getting location, request updates.        locationManager.requestLocationUpdates(provider, 1, 0, this);  
  68.     mostRecentLocation = locationManager.getLastKnownLocation(provider);  
  69.   }  
  70.   
  71.   /** Sets the mostRecentLocation object to the current location of the device **/  
  72.   @Override  
  73.   public void onLocationChanged(Location location) {  
  74.     mostRecentLocation = location;  
  75.   }  
  76.   
  77.   /** The following methods are only necessary because WebMapActivity implements LocationListener **/   
  78.   @Override  
  79.   public void onProviderDisabled(String provider) {  
  80.   }  
  81.   
  82.   @Override  
  83.   public void onProviderEnabled(String provider) {  
  84.   }  
  85.   
  86.   @Override  
  87.   public void onStatusChanged(String provider, int status, Bundle extras) {  
  88.   }  
  89.   
  90. }  

四、在"AndroidManifest.xml"文件中注册MapV3.java文件,  <activity
  android:name=".WebMapActivityJSInterface">
  </activity>

到此,恭喜你,大功告成!运行项目后,如果网络没有问题,可以看到一个地图,图标所示位置即为你手机当前的位置。

PS:

1、有时由于网络原因,可能会看不到地图,这时不妨换换网络,如从WIFI换成3G,等等;

2、建议把这个项目结合谷歌开发的官方指南来学习,官方指南中文网址为:https://developers.google.com/maps/documentation/javascript/tutorial?hl=zh-CN

3、在HTML文件中,“<!DOCTYPE html>”这一行代码最好不要用--对于入门者来说。因为可能有些浏览器不支持HTML5。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值