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

时间:2012-02-04 17:28:54

标签: html5 google-maps-api-3

"My Location" in Google Maps javascript API

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

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

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

4 个答案:

答案 0 :(得分:52)

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 :(得分:4)

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

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

在HTML标题中包含此JavaScript:

<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/软件自动生成的查看器开发的 - 它为地图叠加创建了图块,并根据图像和栅格地理数据创建了自定义图层。

答案 2 :(得分:3)

你必须自己做。这是一段代码,用于添加&#34;您的位置&#34;按钮。 的 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();
}); 

答案 3 :(得分: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)
};