4

我使用谷歌地图引擎创建了一条公交路线。我想添加一个指针(标记),它将在地图上显示用户的当前位置我该怎么做?我没有看到这样的选择。

此外,我正在以 KML 格式导出文件,然后在我的网页上阅读它,但它不会在地图上显示用户的当前位置,而只会显示路线。

https://mapsengine.google.com/map/edit?mid=z0-DS05yFIOY.kaEp4978lVdI

4

1 回答 1

0

首先,Google Maps Engine (GME) 主要是一个数据/服务层。相反,用户的位置将来自客户端/表示层,在这种情况下是您的浏览器、移动设备或使用外部 GPS 装备的更复杂的设置。所以最终,在你的地图中移动一个用户位置图标并不是你要用 GME 完成的事情。相反,您需要将客户直接可用的技术与 Google Maps API 中公开的功能配对。

我将在这里向您展示一个基于浏览器的解决方案,它实现了HTML5 Geolocation API这是另一个讨论和示例。如果您还有其他问题,您会发现 Google 很容易。如果您正在为移动设备开发本机应用程序,则需要研究与您的框架/SDK 相关的对应位置/GPS API。

希望这会有所帮助。

使用 HTML5 地理位置在 Google 地图中显示用户的位置变化

// Make sure you scope these variables properly, I'm showing them
// at the global/application level..
var _MobilePosition;
var _LAST_GPS_ERROR;


// A simple icon/marker to represent the user's location in the map..
var _MobileIcon = {
    path: 'M 0, 0 m -5, 0 a 5, 5 0 1, 0 10, 0 a 5, 5 0 1, 0 -10, 0',
    fillColor: '#00FF00',
    fillOpacity: 0.8,
    scale: 1,
    strokeColor: '#00FF00',
    strokeWeight: 0
};

var _MobileMarker = new google.maps.Marker({
    icon: _MobileIcon
});    


// WPID = "Watch Position ID".
//
// This is where you start handling the HTML5 geolocation API. Every so many 
// seconds it fires and forwards GPS data to your geo_success or geo_error 
// functions..
//
var _WPID = navigator.geolocation.watchPosition(
    geo_success, 
    geo_error, 
    {
        enableHighAccuracy:true, 
        timeout:5000,
        maximumAge:1000
    }
);


// Handle successful GPS positioning by moving a user position marker around
// in the map..
//
function geo_success(pos)
{
    // This condition prevents moving the marker before the maps library is
    // loaded. Depending how you use this, you may not need this condition.
    //
    if(typeof google.maps.LatLng !== 'undefined')
    {
        var lat = Number(pos.coords.latitude);
        var lng = Number(pos.coords.longitude);

        _MobilePosition = new google.maps.LatLng(lat,lng);

        _MobileMarker.setVisible(true);
        _MobileMarker.setPosition(_MobilePosition);
        _MobileMarker.setMap(_map);

        // Optional: pan the map to the user's position.
        // If you implement this, it's a good idea to give the user
        // some means of disabling this functionality..
        //
        if( typeof _MobilePosition !== 'undefined' )
        {
            _map.panTo(_MobilePosition);
        }
    }
}


// Catch any errors thrown by the geolocation API. I'm not doing anything
// useful with it in this example, just catching it..
// 
function geo_error(error)
{
    _LAST_GPS_ERROR = error;
};
于 2014-12-18T17:28:44.753 回答