利用HTML5開發Android(5)---HTML5地理位置服務在Android中的應用

來源:互聯網
上載者:User

標籤:android   style   blog   color   io   os   java   ar   strong   

Android中
Java代碼
 1 //啟用地理定位    2 webSettings.setGeolocationEnabled(true);    3 //設定定位的資料庫路徑    4 webSettings.setGeolocationDatabasePath(dir);    5    6 //配置許可權(同樣在WebChromeClient中實現)    7 public void onGeolocationPermissionsShowPrompt(String origin,     8                GeolocationPermissions.Callback callback) {    9     callback.invoke(origin, true, false);   10     super.onGeolocationPermissionsShowPrompt(origin, callback);   11 }  
在Manifest中添加許可權
Xml代碼
1 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />  2 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />  
HTML5中 通過navigator.geolocation對象擷取地理位置資訊
常用的navigator.geolocation對象有以下三種方法:

1 //擷取當前地理位置   2 navigator.geolocation.getCurrentPosition(success_callback_function, error_callback_function, position_options)   3 //持續擷取地理位置   4 navigator.geolocation.watchPosition(success_callback_function, error_callback_function, position_options)   5 //清除持續擷取地理位置事件   6 navigator.geolocation.clearWatch(watch_position_id)  
其中success_callback_function為成功之後處理的函數,error_callback_function為失敗之後返回的處理函數,參數position_options是配置項
 
在JS中的代碼
Js代碼
 1 //定位    2 function get_location() {    3     if (navigator.geolocation) {    4         navigator.geolocation.getCurrentPosition(show_map,handle_error,{enableHighAccuracy:false,maximumAge:1000,timeout:15000});    5     } else {    6         alert("Your browser does not support HTML5 geoLocation");    7     }    8 }    9        10 function show_map(position) {   11     var latitude = position.coords.latitude;   12     var longitude = position.coords.longitude;   13     var city = position.coords.city;   14     //telnet localhost 5554   15     //geo fix -82.411629 28.054553   16     //geo fix -121.45356 46.51119 4392   17     //geo nmea $GPGGA,001431.092,0118.2653,N,10351.1359,E,0,00,,-19.6,M,4.1,M,,0000*5B   18     document.getElementByIdx_x_x_x("Latitude").innerHTML="latitude:"+latitude;   19     document.getElementByIdx_x_x_x("Longitude").innerHTML="longitude:"+longitude;   20     document.getElementByIdx_x_x_x("City").innerHTML="city:"+city;   21 }   22        23 function handle_error(err) {   24     switch (err.code) {   25     case 1:   26         alert("permission denied");   27         break;   28     case 2:   29         alert("the network is down or the position satellites can‘t be contacted");   30         break;   31     case 3:   32         alert("time out");   33         break;   34     default:   35         alert("unknown error");   36         break;   37     }   38 }  
其中position對象包含很多資料 error代碼及選項 可以查看文檔

利用HTML5開發Android(5)---HTML5地理位置服務在Android中的應用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.