在AngularJS中如何使用谷歌地圖把當(dāng)前位置顯示出來
--在html5中,為我們提供了navigator.geolocation.getCurrentPosition(f1, f2)函數(shù),f1是定位成功調(diào)用的函數(shù),f2是定位失敗調(diào)用的函數(shù),而且會把當(dāng)前的地理位置信息作為實參傳遞給f1和f2函數(shù)。f1函數(shù)調(diào)用谷歌地圖的API即可。
如何展示呢?
--需要一個提示信息和展示地圖的一個區(qū)域。
頁面上,大致是這樣:
<map-geo-location height="400" width="600"></map-geo-location> <script src="angular.js"></script> <script src="http://maps.google.com/maps/api/js?sensor=false"></script> <script src=="mapGeoLocation.js"></script>
Directive部分如下:
(function(){ var mapGeoLocation = ['$window', function($window){ var template = '<p><span id="status">正在查找地址...</span></p>' + '<br /><div id="map"></div>', mapContainer = null, status = null; function link(scope, elem, attrs){ //以Angular的方式獲取Angular元素 status = angular.element(document.getElementById('status')); mapContainer = angular.element(document.getElementById('map')); mapContainer.attr('style', 'height:' + scope.height + 'px;width:' + scope.width + 'px'); $window.navigator.geolocation.getCurrentPosition(mapLocation, geoError); } //定位成功時調(diào)用 function mapLocation(pos){ status.html('found your location! Longitude: ' + pos.coords.longitude + ' Latitude: ' + pos.coords.latitude); var latlng = new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude); var optons = { zoom:15, center: latlng, myTypeCOntrol: true, mapTypeId: google.maps.MapTypeId.ROADMAP }; var map = new google.maps.Map(mapContainer[0], options); var marker = new google.maps.Markser({ position: latlng, map: map, title: "Your location" }); } //定位失敗時調(diào)用 function geoError(error){ status.html('failed lookup ' + error.message); } return { restrict: 'EA', //默認(rèn) scope:{ height: '@', width:'@' }, link: link, template: template } }]; angular.module('direcitveModule',[]) .direcitve('mapGeoLocation', mapGeoLocation); }());
以上所述是小編給大家介紹的在AngularJS中如何使用谷歌地圖把當(dāng)前位置顯示出來的相關(guān)知識,希望對大家有所幫助。
- 帶有定位當(dāng)前位置的百度地圖前端web api實例代碼
- iOS中定位當(dāng)前位置坐標(biāo)及轉(zhuǎn)換為火星坐標(biāo)的方法
- Android獲取當(dāng)前位置的經(jīng)緯度數(shù)據(jù)
- JS利用cookie記憶當(dāng)前位置的防刷新導(dǎo)航效果
- Android實現(xiàn)Service獲取當(dāng)前位置(GPS+基站)的方法
- thinkphp獲取欄目和文章當(dāng)前位置的方法
- thinkphp實現(xiàn)面包屑導(dǎo)航(當(dāng)前位置)例子分享
- 鼠標(biāo)移到導(dǎo)航當(dāng)前位置的LI變色處于選中狀態(tài)
- 講解iOS開發(fā)中基本的定位功能實現(xiàn)
- iOS獲取到用戶當(dāng)前位置
相關(guān)文章
Angular.JS中的指令引用template與指令當(dāng)做屬性詳解
這篇文章主要介紹了Angular.JS中的指令引用template與指令當(dāng)做屬性的相關(guān)資料,文中介紹的非常詳細(xì),對大家具有一定的參考價值,需要的朋友們下面來一起看看吧。2017-03-03詳解angular用$sce服務(wù)來過濾HTML標(biāo)簽
這篇文章主要介紹了詳解angular用$sce服務(wù)來過濾HTML標(biāo)簽,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-04-04angular 用攔截器統(tǒng)一處理http請求和響應(yīng)的方法
下面小編就為大家?guī)硪黄猘ngular 用攔截器統(tǒng)一處理http請求和響應(yīng)的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06Angular.js ng-file-upload結(jié)合springMVC的使用教程
這篇文章主要給大家介紹了關(guān)于Angular.js文件上傳控件ng-file-upload結(jié)合springMVC的使用教程,文中通過示例代碼介紹的非常詳細(xì),對大家具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧。2017-07-07Angular 輸入框?qū)崿F(xiàn)自定義驗證功能
AngularJS 表單和控件可以驗證輸入的數(shù)據(jù)。本文給大家介紹Angular 輸入框?qū)崿F(xiàn)自定義驗證功能,非常不錯,具有參考借鑒價值,需要的朋友參考下2017-02-02使用Angular CLI進(jìn)行Build(構(gòu)建)和Serve詳解
這篇文章主要介紹了使用Angular CLI進(jìn)行Build(構(gòu)建)和Serve詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-03-03