如何让用户在我的Google地图应用程序中指出他们的位置?

时间:2010-11-03 20:39:43

标签: google-maps

我想让用户在我们的谷歌地图应用程序中标记他们的位置,然后将其保存到我们的数据库,然后它可以在我们的谷歌地图应用程序中显示下一次。

2 个答案:

答案 0 :(得分:1)

如果你想保存用户点击的位置你可以使用“点击”监听器来获取点击的经纬度,用这样的代码然后使用Ajax样式调用将它发送到你的服务器,它可以是存储在数据库中。

var clickListener = GEvent.addListener(map,"click",
    function (overlay,latlng,overlaylatlng) {
        alert(latlng);
});

此代码适用于Google Maps API的v2。版本3应该有类似的东西。

答案 1 :(得分:1)

选项是首先尝试地理位置,然后让浏览器查明它们。如果浏览器不支持地理定位或发生错误,您可以手动添加其位置。

var geolocation = null; // holds the latlng object
navigator.geolocation.getCurrentPosition( geolocation_success_init, geolocation_error_init, {enableHighAccuracy: false, timeout: 10000} );

function geolocation_success_init( position ) {
    geolocation = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
    initialize_map();
}
function geolocation_error_init( error ){
    initialize_map();
}

为地理位置创建地图检查时

if ( geolocation ) {
    marker = new google.maps.Marker({
        position: geolocation,
        map:.map,
        title: "Your Location"
    });
}