在AngularJS中如何使用谷歌地圖把當(dāng)前位置顯示出來(lái)
--在html5中,為我們提供了navigator.geolocation.getCurrentPosition(f1, f2)函數(shù),f1是定位成功調(diào)用的函數(shù),f2是定位失敗調(diào)用的函數(shù),而且會(huì)把當(dāng)前的地理位置信息作為實(shí)參傳遞給f1和f2函數(shù)。f1函數(shù)調(diào)用谷歌地圖的API即可。
如何展示呢?
--需要一個(gè)提示信息和展示地圖的一個(gè)區(qū)域。
頁(yè)面上,大致是這樣:
<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); } //定位成功時(shí)調(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" }); } //定位失敗時(shí)調(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)前位置顯示出來(lái)的相關(guān)知識(shí),希望對(duì)大家有所幫助。
- 帶有定位當(dāng)前位置的百度地圖前端web api實(shí)例代碼
- iOS中定位當(dāng)前位置坐標(biāo)及轉(zhuǎn)換為火星坐標(biāo)的方法
- Android獲取當(dāng)前位置的經(jīng)緯度數(shù)據(jù)
- JS利用cookie記憶當(dāng)前位置的防刷新導(dǎo)航效果
- Android實(shí)現(xiàn)Service獲取當(dāng)前位置(GPS+基站)的方法
- thinkphp獲取欄目和文章當(dāng)前位置的方法
- thinkphp實(shí)現(xiàn)面包屑導(dǎo)航(當(dāng)前位置)例子分享
- 鼠標(biāo)移到導(dǎo)航當(dāng)前位置的LI變色處于選中狀態(tài)
- 講解iOS開(kāi)發(fā)中基本的定位功能實(shí)現(xiàn)
- iOS獲取到用戶當(dāng)前位置
相關(guān)文章
Angular.JS中的指令引用template與指令當(dāng)做屬性詳解
這篇文章主要介紹了Angular.JS中的指令引用template與指令當(dāng)做屬性的相關(guān)資料,文中介紹的非常詳細(xì),對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-03-03詳解angular用$sce服務(wù)來(lái)過(guò)濾HTML標(biāo)簽
這篇文章主要介紹了詳解angular用$sce服務(wù)來(lái)過(guò)濾HTML標(biāo)簽,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-04-04angular 用攔截器統(tǒng)一處理http請(qǐng)求和響應(yīng)的方法
下面小編就為大家?guī)?lái)一篇angular 用攔截器統(tǒng)一處理http請(qǐng)求和響應(yīng)的方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-06-06Angular.js ng-file-upload結(jié)合springMVC的使用教程
這篇文章主要給大家介紹了關(guān)于Angular.js文件上傳控件ng-file-upload結(jié)合springMVC的使用教程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-07-07用AngularJS的指令實(shí)現(xiàn)tabs切換效果
這篇文章介紹的是寫一個(gè)通過(guò)指令嵌套實(shí)現(xiàn)tabs功能的指令模塊,這也是我在一個(gè)項(xiàng)目中應(yīng)用到的,現(xiàn)在分享給大家,有需要的可以參考借鑒。2016-08-08Angular 輸入框?qū)崿F(xiàn)自定義驗(yàn)證功能
AngularJS 表單和控件可以驗(yàn)證輸入的數(shù)據(jù)。本文給大家介紹Angular 輸入框?qū)崿F(xiàn)自定義驗(yàn)證功能,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下2017-02-02使用Angular CLI進(jìn)行Build(構(gòu)建)和Serve詳解
這篇文章主要介紹了使用Angular CLI進(jìn)行Build(構(gòu)建)和Serve詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-03-03