如何在Google地图中添加我的位置按钮?

pro*_*san 34 javascript google-maps

我想知道是否可以添加默认控件选项我的位置按钮.

用我的位置按钮映射

有没有办法让它成为默认值,或者我需要创建具有地理位置的按钮然后触发该按钮上的click事件以便将用户导航到当前位置?

mi3*_*zal 45

正如@Praveen所说,你必须自己做.这是一段添加"您的位置"按钮的代码.JS小提琴链接

HTML

<div id="map">Map will be here</div>
Run Code Online (Sandbox Code Playgroud)

CSS

#map {width:100%;height: 400px;}
Run Code Online (Sandbox Code Playgroud)

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();
}); 
Run Code Online (Sandbox Code Playgroud)


Pra*_*een 21

谷歌地图本身是他们的谷歌地图API的定制实施.所以你必须自己做.

AFIK 谷歌地图API v3没有为"显示我的位置"提供任何默认控制,但实现你自己的很简单;

  1. API v3提供了 向地图添加自定义控件的功能,因此您可以轻松添加图标并编写事件处理程序,如下所述.
  2. 使用"HTML5地理位置",它会返回您当前位置的位置.
  3. 因此,使用lat/lng,当您单击图标处理它以放置标记.


Jon*_*nny 21

我稍微修改了mi3afzal的小提琴,以放弃jQuery依赖并添加视网膜支持.此外,我建议使用center_changed事件而不是dragend因为中心可以以编程方式更改,例如添加标记和扩展边界时.

https://jsfiddle.net/ogsvzacz/6/.

var map,
    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 = '0';
    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-2x.png)';
    secondChild.style.backgroundSize = '180px 18px';
    secondChild.style.backgroundPosition = '0 0';
    secondChild.style.backgroundRepeat = 'no-repeat';
    firstChild.appendChild(secondChild);

    google.maps.event.addListener(map, 'center_changed', function () {
        secondChild.style['background-position'] = '0 0';
    });

    firstChild.addEventListener('click', function () {
        var imgX = 0,
            animationInterval = setInterval(function () {
                imgX = -imgX - 18 ;
                secondChild.style['background-position'] = imgX+'px 0';
            }, 500);

        if(navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(function(position) {
                var latlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
                map.setCenter(latlng);
                clearInterval(animationInterval);
                secondChild.style['background-position'] = '-144px 0';
            });
        } else {
            clearInterval(animationInterval);
            secondChild.style['background-position'] = '0 0';
        }
    });

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


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);
Run Code Online (Sandbox Code Playgroud)


Klo*_*ies 16

我们为Google Maps API v3制作了这样一个组件.任何人都可以在自定义项目中使用一行代码添加一个显示当前地理位置的控件:

var geoloccontrol = new klokantech.GeolocationControl(map, mapMaxZoom);
Run Code Online (Sandbox Code Playgroud)

在HTML标题中包含此JavaScript之后:

<script src="https://cdn.klokantech.com/maptilerlayer/v1/index.js"></script>
Run Code Online (Sandbox Code Playgroud)

看到:

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

有关示例代码和文档.

在Google Maps API v3上显示我的位置控件

它将标准控件添加到地图中 - 一旦点击 - 它会显示您所在位置周围的蓝色圆圈,其大小来自可用位置数据的精度.如果你不拖动地图,它会让你在移动后保持定位.

此控件是为http://www.maptiler.com/软件自动生成的查看器开发的 - 它为地图叠加创建了图块,并根据图像和栅格地理数据创建了自定义图层.

注意:此答案是我们在Google Maps API v3上显示我的位置的回复的重新发布