Javascript 谷歌地图API地理定位&x2B;Lat/Lng
我想提供地理定位和可编辑地图标记,通过谷歌地图API向用户返回lat/lng。我已经成功地找到了如何通过所述方法进行地理定位的部分:Javascript 谷歌地图API地理定位&x2B;Lat/Lng,javascript,google-maps,google-maps-api-3,geolocation,geopositioning,Javascript,Google Maps,Google Maps Api 3,Geolocation,Geopositioning,我想提供地理定位和可编辑地图标记,通过谷歌地图API向用户返回lat/lng。我已经成功地找到了如何通过所述方法进行地理定位的部分: 功能成功(职位){ var mapcanvas=document.createElement('div'); mapcanvas.id='mapcontainer'; mapcanvas.style.height='400px'; mapcanvas.style.width='600px'; document.querySelector('article').a
功能成功(职位){
var mapcanvas=document.createElement('div');
mapcanvas.id='mapcontainer';
mapcanvas.style.height='400px';
mapcanvas.style.width='600px';
document.querySelector('article').appendChild(mapcanvas);
coords=new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
变量选项={
缩放:15,
中心:coords,
mapTypeControl:false,
导航控制选项:{
样式:google.maps.NavigationControlStyle.SMALL
},
mapTypeId:google.maps.mapTypeId.ROADMAP
};
var map=new google.maps.map(document.getElementById(“mapcontainer”),选项);
var marker=new google.maps.marker({
职位:coords,
地图:地图,
标题:“你在这里!”
});
}
if(导航器.地理位置){
navigator.geolocation.getCurrentPosition(成功);
}否则{
错误(“不支持地理位置”);
}
但是,我也希望能够允许用户移动地图,通过地图标记选择另一个位置,并捕获他们选择的lat/lng。该方法实现了我想要的功能,但我不确定如何让两个脚本一起工作以添加我需要的功能。我正试图创造一些像尤伯在这方面所做的事情,如果这有助于解释目标的话
我怎样才能制作一张以地理位置为中心的谷歌地图,但谁的地图标记可以移动并记录最终标记的lat/lng?谢谢你的想法 首先要在标记选项中将标记的
draggable
属性设置为true
然后在标记上侦听dragend
事件-您可以使用google.maps.event.addListener(标记'dragend',回调)进行设置代码>
然后在回调
函数中,使用getPosition()
方法获取标记的位置
如@geocodezip所述,请参阅以获取更多参考。您尝试了什么?你看过报纸了吗?也许你试着让这个标记可以拖动?然后调查了这么多问题?
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script>
<script>
function success(position) {
var mapcanvas = document.createElement('div');
mapcanvas.id = 'mapcontainer';
mapcanvas.style.height = '400px';
mapcanvas.style.width = '600px';
document.querySelector('article').appendChild(mapcanvas);
coords = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
var options = {
zoom: 15,
center: coords,
mapTypeControl: false,
navigationControlOptions: {
style: google.maps.NavigationControlStyle.SMALL
},
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("mapcontainer"), options);
var marker = new google.maps.Marker({
position: coords,
map: map,
title:"You are here!"
});
}
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(success);
} else {
error('Geo Location is not supported');
}
</script>