2012-02-04 153 views
14

"My Location" in Google Maps javascript API显示我的谷歌地图API V3

这个问题被问过的位置在一年半前。 Google Maps API v3是否已更新为使用http://maps.google.com上的“我的位置”按钮?

我的位置是街景视图和游戏手柄控件之间的控件。

如果Google Maps API不提供我的位置,那么我是否需要使用navigator.gelocation编写自己的HTML5地理位置功能,然后在Google地图上创建我自己的控件?

回答

46

No,但基于目前的位置添加自己的标志很简单:

var myloc = new google.maps.Marker({ 
    clickable: false, 
    icon: new google.maps.MarkerImage('//maps.gstatic.com/mapfiles/mobile/mobileimgs2.png', 
                new google.maps.Size(22,22), 
                new google.maps.Point(0,18), 
                new google.maps.Point(11,11)), 
    shadow: null, 
    zIndex: 999, 
    map: // your google.maps.Map object 
}); 

if (navigator.geolocation) navigator.geolocation.getCurrentPosition(function(pos) { 
    var me = new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude); 
    myloc.setPosition(me); 
}, function(error) { 
    // ... 
}); 
+1

感谢。我最终创建了一个自定义控件并添加了一个点击监听器。当侦听器触发它时,几乎完成了键入的内容:将标记添加到用户的位置。 – hobbes3 2012-02-05 06:12:08

+0

谢谢你亲切的先生 – EmptyCup 2016-01-01 19:09:32

+0

非常感谢你!不过,如果您使用此功能,则可能需要下载[link](maps.gstatic.com/mapfiles/mobile/mobileimgs2.png)托管的图像,并将其保存在本地服务器中。如果没有这样做,你总是依赖于另一台服务器的连接加上你的连接。通过下载图像,一切应该更平滑 – blastervla 2017-03-15 14:59:33

2
//copy and paste this in your script section. 
if (navigator.geolocation) { 
    navigator.geolocation.getCurrentPosition(success, error); 
} else { 
    alert('location not supported'); 
} 

//callbacks 
function error(msg) { 
    alert('error in geolocation'); 
} 

function success(position) { 
    var lats = position.coords.latitude; 
    var lngs = position.coords.longitude; 
    alert(lats); 
    alert(lngs) 
}; 
4

我们已经做出了这样的组件,用于谷歌地图API第3版。任何人都可以在自定义项目中使用添加显示只用一行代码当前地理位置的控制:包括在HTML头这个JavaScript后

var geoloccontrol = new klokantech.GeolocationControl(map, mapMaxZoom); 

<script src="https://cdn.klokantech.com/maptilerlayer/v1/index.js"></script> 

参见:

http://www.maptiler.com/maptilerlayer/

获取示例代码和文档。

Show My Location Control on Google Maps API v3

它增加了标准控制的地图 - 一旦挖 - 它显示在你的位置的蓝色圆圈从现有位置数据的精度得出尺寸。如果你不拖动地图,它会让你一旦移动就定位。

该控件是为由http://www.maptiler.com/软件自动生成的查看器开发的,该软件为地图叠加层和由图像和栅格地理数据制作的自定义图层创建拼贴。

+1

不知何故GeolocationControl不会显示给我(即使我使用完全相同的代码从文档)。该库的所有其他功能对我来说都有效。有关于该按钮的已知问题? – erikvdplas 2017-05-03 10:23:41

+0

包含'\t 后不会显示地理位置图标var geoloccontrol = new klokantech.GeolocationControl(map,mapMaxZoom); 'in my file – 2018-01-04 06:17:36

3

你必须自己做。这是一段添加“您的位置”按钮的代码。 HTML

<div id="map">Map will be here</div> 

CSS

#map {width:100%;height: 400px;} 

JS

var map; 
var faisalabad = {lat:31.4181, lng:73.0776}; 

function addYourLocationButton(map, marker) 
{ 
    var controlDiv = document.createElement('div'); 

    var firstChild = document.createElement('button'); 
    firstChild.style.backgroundColor = '#fff'; 
    firstChild.style.border = 'none'; 
    firstChild.style.outline = 'none'; 
    firstChild.style.width = '28px'; 
    firstChild.style.height = '28px'; 
    firstChild.style.borderRadius = '2px'; 
    firstChild.style.boxShadow = '0 1px 4px rgba(0,0,0,0.3)'; 
    firstChild.style.cursor = 'pointer'; 
    firstChild.style.marginRight = '10px'; 
    firstChild.style.padding = '0px'; 
    firstChild.title = 'Your Location'; 
    controlDiv.appendChild(firstChild); 

    var secondChild = document.createElement('div'); 
    secondChild.style.margin = '5px'; 
    secondChild.style.width = '18px'; 
    secondChild.style.height = '18px'; 
    secondChild.style.backgroundImage = 'url(https://maps.gstatic.com/tactile/mylocation/mylocation-sprite-1x.png)'; 
    secondChild.style.backgroundSize = '180px 18px'; 
    secondChild.style.backgroundPosition = '0px 0px'; 
    secondChild.style.backgroundRepeat = 'no-repeat'; 
    secondChild.id = 'you_location_img'; 
    firstChild.appendChild(secondChild); 

    google.maps.event.addListener(map, 'dragend', function() { 
     $('#you_location_img').css('background-position', '0px 0px'); 
    }); 

    firstChild.addEventListener('click', function() { 
     var imgX = '0'; 
     var animationInterval = setInterval(function(){ 
      if(imgX == '-18') imgX = '0'; 
      else imgX = '-18'; 
      $('#you_location_img').css('background-position', imgX+'px 0px'); 
     }, 500); 
     if(navigator.geolocation) { 
      navigator.geolocation.getCurrentPosition(function(position) { 
       var latlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude); 
       marker.setPosition(latlng); 
       map.setCenter(latlng); 
       clearInterval(animationInterval); 
       $('#you_location_img').css('background-position', '-144px 0px'); 
      }); 
     } 
     else{ 
      clearInterval(animationInterval); 
      $('#you_location_img').css('background-position', '0px 0px'); 
     } 
    }); 

    controlDiv.index = 1; 
    map.controls[google.maps.ControlPosition.RIGHT_BOTTOM].push(controlDiv); 
} 

function initMap() { 
    map = new google.maps.Map(document.getElementById('map'), { 
     zoom: 15, 
     center: faisalabad 
    }); 
    var myMarker = new google.maps.Marker({ 
     map: map, 
     animation: google.maps.Animation.DROP, 
     position: faisalabad 
    }); 
    addYourLocationButton(map, myMarker); 
} 

$(document).ready(function(e) { 
    initMap(); 
});