星空网 > 软件开发 > Java

在AngularJS中使用谷歌地图把当前位置显示出来

 

如何使用谷歌地图把当前位置显示出来呢?

--在html5中,为我们提供了navigator.geolocation.getCurrentPosition(f1, f2)函数,f1是定位成功调用的函数,f2是定位失败调用的函数,而且会把当前的地理位置信息作为实参传递给f1和f2函数。f1函数调用谷歌地图的API即可。



如何展示呢?

--需要一个提示信息和展示地图的一个区域。



页面上,大致是这样:



<map-geo-location height="400" width="600"></map-geo-location>

<script src='/images/loading.gif' data-original="angular.js"></script>
<script src='/images/loading.gif' data-original="http://maps.google.com/maps/api/js?sensor=false"></script>
<script src='/images/loading.gif' data-original=="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中使用谷歌地图把当前位置显示出来

关键词:JS

JS
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

Finish Line:https://www.goluckyvip.com/tag/20431.html
Finnomena:https://www.goluckyvip.com/tag/20432.html
Fire Tablets广告:https://www.goluckyvip.com/tag/20433.html
FireShot:https://www.goluckyvip.com/tag/20434.html
firmabs:https://www.goluckyvip.com/tag/20435.html
FirstCry:https://www.goluckyvip.com/tag/20436.html
价格翻了8倍,居然还有人买,秘诀在这里。:https://www.kjdsnews.com/a/1836642.html
欧洲B2B电商市场有待挖掘!销售总额将达1.7万亿欧元:https://www.kjdsnews.com/a/1836643.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流