• 技术文章 >web前端 >js教程

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

    2016-05-16 15:18:26原创1170

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

    如何展示呢?

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

    页面上,大致是这样:

    
    
    
    

    Directive部分如下:

    (function(){
    var mapGeoLocation = ['$window', function($window){
    var template = '

    正在查找地址...

    ' + '
    ', 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中如何使用谷歌地图把当前位置显示出来的相关知识,希望对大家有所帮助。

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    上一篇:Javascript实现单例模式_javascript技巧 下一篇:自己动手写 PHP MVC 框架(40节精讲/巨细/新人进阶必看)

    相关文章推荐

    • 深入解析JWT(JSON Web Token)的原理及用法• 聊聊Node.js中怎么用async函数• 聊聊Node项目中怎么操作MySQL• 一文聊聊Angular中的依赖注入• react native路由跳转怎么实现
    1/1

    PHP中文网