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

18

Google Maps Javascript API中的“我的位置”

这个问题已经半年之前就有人问过了。 Google Maps API v3是否更新了以使用在http://maps.google.com上找到的“My Location”按钮?

“我的位置”是介于街景和游戏手柄控件之间的控制。

如果Google Maps API没有提供“My Location”,那么我需要使用navigator.gelocation编写自己的HTML5地理定位功能,然后在Google Maps上创建自己的控件吗?

4个回答

59

不可以,但基于当前位置添加自己的标记很容易:

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
非常感谢!!但是,如果你使用它,你可能想要下载 link 上托管的图像并将其保存到本地服务器。如果不这样做,你始终依赖另一个服务器的连接加上你自己的。通过下载图片,一切都应该更顺畅。 - blastervla
4
公平地说,那是一个谷歌服务器。如果它崩溃了,地图API也将无法工作。 - josh3736
但是当用户移动时,如何重新绘制位置图标呢?在我的情况下,它每次都会创建一个新的。 - Dr. Freddy Dimethyltryptamine
您可以简写为 icon: 'path/to/icon.png' - Fanky

5
我们为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/

获得示例代码和文档。

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

它向地图添加了标准控件,一旦点击即会显示蓝色圆圈,其大小由可用的位置数据的精度确定。如果您不拖动地图,它将使您保持定位状态。

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


2
不知何故,GeolocationControl 对我来说无法显示(即使我使用文档中完全相同的代码)。该库的所有其他功能对我都有效。关于该按钮是否存在已知问题? - erikvdplas
1
在我的文件中包含以下代码后,地理位置图标没有显示:var geoloccontrol = new klokantech.GeolocationControl(map, mapMaxZoom); - Siddu hadapad
1
我将“var geoloccontrol = new klokantech.GeolocationControl(map, mapMaxZoom);”更改为“var geoloccontrol = new klokantech.GeolocationControl(map);”,并且它起作用了。基本上我删除了未初始化的可选参数opt_zoom。或者,您可以将“mapMaxZoom”替换为实际值,这将是地图在获取初始位置后切换到的缩放级别。现在,有人知道如何覆盖默认的“我的位置”按钮大小吗?与最近重新设计以具有较大 UI 按钮的新版 Google 地图 API 相比,它非常小。 - BartmanEH
当我在浏览器中查看地图时,一切都正常,但是当我使用Apache Cordova构建Android移动应用程序时,我没有得到相同的结果,为什么?问题在于,尽管我使用与浏览器上的代码完全相同的代码,但按钮不可见,是否有另一种方法可以在Apache Cordova移动应用程序中持续显示当前位置?谢谢。 - pancy1

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();
}); 

1
//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)
};

1
这个地理位置示例甚至没有展示任何Google地图代码。 - Greg Chabala

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接