国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

在AngularJS中如何使用谷歌地圖把當前位置顯示出來

2019-11-20 10:42:22
字體:
來源:轉載
供稿:網友

--在html5中,為我們提供了navigator.geolocation.getCurrentPosition(f1, f2)函數,f1是定位成功調用的函數,f2是定位失敗調用的函數,而且會把當前的地理位置信息作為實參傳遞給f1和f2函數。f1函數調用谷歌地圖的API即可。

如何展示呢?

--需要一個提示信息和展示地圖的一個區域。

頁面上,大致是這樣:

<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);}//定位成功時調用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"});}//定位失敗時調用function geoError(error){status.html('failed lookup ' + error.message);}return {restrict: 'EA', //默認scope:{height: '@',width:'@'},link: link,template: template}}];angular.module('direcitveModule',[]).direcitve('mapGeoLocation', mapGeoLocation);}());

以上所述是小編給大家介紹的在AngularJS中如何使用谷歌地圖把當前位置顯示出來的相關知識,希望對大家有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 房产| 电白县| 余干县| 花垣县| 黄大仙区| 苍溪县| 南宁市| 凤凰县| 玛沁县| 金川县| 都兰县| 襄城县| 德惠市| 喜德县| 交城县| 澳门| 阜阳市| 门头沟区| 孟村| 宾川县| 宝兴县| 兴义市| 兰考县| 淮南市| 富裕县| 霍山县| 烟台市| 新化县| 平顺县| 小金县| 宁都县| 福鼎市| 江永县| 宜君县| 徐汇区| 霍城县| 宁国市| 平塘县| 安岳县| 威信县| 安塞县|