通过 Google Maps API V3 检索可拖动图钉的纬度和经度
我会解释一下。我设法在地图上有一个可拖动的图钉。我想检索该点的坐标并将其放入两个字段中:纬度和经度。这些坐标稍后将通过 PHP 发送到 SQL 表。 这是我打算做的一个示例,但它不是几个图钉,而是一个图钉可拖动。问题是:我什至无法显示初始点的坐标。当然,当用户移动图钉时,我希望字段中的坐标也发生变化。 我希望我说清楚了。我做错了什么?我应该使用地理编码服务吗?
这是 JS:
<script type="text/javascript">
var map;
function initialize() {
var myLatlng = new google.maps.LatLng(40.713956,-74.006653);
var myOptions = {
zoom: 8,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
var marker = new google.maps.Marker({
draggable: true,
position: myLatlng,
map: map,
title: "Your location"
});
google.maps.event.addListener(marker,'click',function(overlay,point){
document.getElementById("latbox").value = lat();
document.getElementById("lngbox").value = lng();
});
}
</script>
和 HTML:
<html>
<body onload="initialize()">
<div id="map_canvas" style="width:50%; height:50%"></div>
<div id="latlong">
<p>Latitude: <input size="20" type="text" id="latbox" name="lat" ></p>
<p>Longitude: <input size="20" type="text" id="lngbox" name="lng" ></p>
</div>
</body>
</html>
I will explain. I managed to have a draggable pin on a map. I want to retrieve the coordinates of this point and put them into two fields: Latitude and Longitude. These coordinates will later be send to a SQL table via PHP.
Here is an example of what I intend to do, but instead of several pins, it's just one and it's draggable. The problem is: I'm not even able to display the coordinates of the initial point. And of course when the user moves the pin, I want the coordinates to change as well in the fields.
I hope I made myself clear. What did I do wrong? Should I use the Geocoding service?
Here goes the JS:
<script type="text/javascript">
var map;
function initialize() {
var myLatlng = new google.maps.LatLng(40.713956,-74.006653);
var myOptions = {
zoom: 8,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
var marker = new google.maps.Marker({
draggable: true,
position: myLatlng,
map: map,
title: "Your location"
});
google.maps.event.addListener(marker,'click',function(overlay,point){
document.getElementById("latbox").value = lat();
document.getElementById("lngbox").value = lng();
});
}
</script>
And the HTML:
<html>
<body onload="initialize()">
<div id="map_canvas" style="width:50%; height:50%"></div>
<div id="latlong">
<p>Latitude: <input size="20" type="text" id="latbox" name="lat" ></p>
<p>Longitude: <input size="20" type="text" id="lngbox" name="lng" ></p>
</div>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
这些工作中的任何一个
您也可以考虑使用 Dragend 事件
Either of these work
You might also consider using the dragend event also
查看 Google Maps API 参考中的官方代码示例:
http://gmaps-samples-v3.googlecode .com/svn/trunk/draggable-markers/draggable-markers.html
Look at the official code sample from Google Maps API reference:
http://gmaps-samples-v3.googlecode.com/svn/trunk/draggable-markers/draggable-markers.html
实际工作的代码如下:
如果地图可以在图钉掉落后重新居中,那就更好了。我想可以用
map.setCenter()
来完成,但我不确定应该把它放在哪里。我尝试将其放在这段代码之前和之后,但它不起作用。The code that is actually working is the following:
It would be better if the map could be re-centered once the pin is dropped. I guess it can be done with
map.setCenter()
but I'm not sure where I should put it. I tried to put it right before and right after this piece of code but it won't work.Google 地图 V3 示例。以下是用户删除单个图钉、用新图钉替换删除的图钉、自定义图钉图像、在 DIV 内的表单字段中填充纬度/经度值的图钉的工作示例。
Google Maps V3 Example. Here's a working example of a user dropping a single pin, replacing a dropped pin with new pin, custom pin images, pins that populate lat/long values in a FORM FIELD within a DIV.
检查这个 fiddle
在下面的代码中用 事件 您想要的。在您的情况下“点击”
Check this fiddle
In the following code replace dragend with the event you want. In your case 'click'
对我来说效果很好..谢谢..
worked well for me.. Thanks..
示例运行 JsFiddle
Example Run JsFiddle