gpt4 book ai didi

javascript - 如何在html文本框中显示google maps api标记的经纬度

转载 作者:行者123 更新时间:2023-11-30 17:52:46 27 4
gpt4 key购买 nike

我在我的网站上添加了一个带有可拖动标记的谷歌地图。但是我想在谷歌地图下方的文本框中显示标记位置的纬度和经度,如本例所示:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="http://maps.google.com/maps? file=api&amp;v=2&amp;key=examplekey"
type="text/javascript">
</script>
</head>
<body>

</h2><table>
<tr>
<td>
<div id="map" style="WIDTH: 700px; HEIGHT: 500px"><div></td></tr><tr><td>
<form action="http://formtoemailremote.com/user_forms.php" method="post">
Lat/Long:..........<input type="text" name="grid" id="grid"><br>
</form>
</td></tr></table>
<p>
</p>
<script type="text/javascript">

var map
map = new GMap2(document.getElementById("map"));
map.addControl(new GLargeMapControl());
map.addControl(new GMapTypeControl());
//map.centerAndZoom(new GPoint(-2.3, 49.35), 8);
map.setCenter(new GLatLng(49.461,-2.58),12);
map.enableDoubleClickZoom();

var icon = new GIcon();
var point=new GLatLng(49.461,-2.58);
var marker = new GMarker(point, {icon:G_DEFAULT_ICON, draggable: true});
map.addOverlay(marker);
marker.enableDragging();
GEvent.addListener(marker, "drag", function(){
document.getElementById("grid").value=marker.getPoint().toUrlValue();});

</script>
</body>
</html>

我看到标记和文本框的连接是id值

GEvent.addListener(marker, "drag", function(){
document.getElementById("grid").value=marker.getPoint().toUrlValue();});

<form action="http://formtoemailremote.com/user_forms.php" method="post">
Lat/Long:..........<input type="text" name="grid" id="grid"><br>
</form>

但是当我将其复制到我自己的代码中时,它不起作用:

<!DOCTYPE html>
<html>
<head>

<!--LOADING THE GOOGLE MAP APPLICATON PROGRAMMING INTERFACE-->
<script src="http://maps.googleapis.com/maps/api/js?key=insertkey&sensor=false">
</script>

<script>
// DEFINING NEW VARIABLE "MYCENTER"
var myCenter=new google.maps.LatLng(49.716,-2.196);

function initialize()
{
// DEFINING MAP PROPERTIES NOTICE CENTER USES PREDEFINED "MYCENTER"
var mapProp = {
center:myCenter,
zoom:13,
mapTypeId:google.maps.MapTypeId.HYBRID
};

//DEFINING NEW VARIABLE "MAP," A GOOGLE MAP BASED ON "MAPPROP"
var map=new google.maps.Map(document.getElementById("googleMap")
,mapProp);

// DEFINING NEW VARIABLE "MARKER" A DRAGGABLE MARKER POSITIONED AT "MYCENTER"
var marker=new google.maps.Marker({
position:myCenter,
draggable:true,
});

// PLACES THE MARKER ON THE GOOGLE MAP
marker.setMap(map);
}

google.maps.event.addDomListener(window, 'load', initialize);

google.maps.event.addListener(marker, "drag", function(){
document.getElementById("grid").value=marker.getPoint().toUrlValue();});

</script>
</head>

<body>
<div id="googleMap" style="width:500px;height:380px;"></div>

<form>
Lat/Long:<input type= "text" name = "gridbox" id = "grid"><br>
</form>

</body>
</html>

我注意到事件代码在示例中是“GEvent”,而在我自己的代码中是“google.maps.event”,这让我觉得它们来自两个不同版本的 google maps api...

如果有人能向我展示正确的编码或为我指明正确的方向,我将不胜感激。

注意:我已经从示例和我自己的代码中删除了 API key 。您必须插入自己的才能看到它的外观。

感谢阅读!

最佳答案

好的,这是一个您可以复制/粘贴的工作示例。

记得包括一个正确的

<script type="text/javascript" src="http://maps.googleapis.co ...

用这个替换你的代码:

<script>
var myCenter=new google.maps.LatLng(49.716,-2.196);
var marker;

function initialize() {
var mapProp = {
center:myCenter,
zoom:13,
mapTypeId:google.maps.MapTypeId.HYBRID
};

var map = new google.maps.Map(document.getElementById("googleMap"),mapProp);

marker = new google.maps.Marker({
position:myCenter,
draggable:true,
});

marker.setMap(map);

google.maps.event.addListener(marker, "drag", function(){
document.getElementById("grid").value=marker.position.toUrlValue();
});
}

google.maps.event.addDomListener(window, 'load', initialize);
</script>

这就是它的全部。

enter image description here

关于javascript - 如何在html文本框中显示google maps api标记的经纬度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18640181/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com