[英]how to get latitude longitude from function and send it to new google.maps.LatLng( latitude ,longitude ) javascript
[英]Pass Latitude and Longitude to a javascript function and get a view from the google map
我正在使用天气数据来实现一个PHP网站。 我想知道如何通过JavaScript onClick
事件从HTML元素传递纬度和经度值。 我已使用以下代码获取Google地图:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<style type="text/css">
html { height: 100% }
body { height: 100%; margin: 0; padding: 0 }
#map_canvas { height: 100% }
</style>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?key*****************&sensor=true"></script>
<script type="text/javascript">
function initialize() {
var myOptions = {
center: new google.maps.LatLng(-34.397, 150.644),
zoom: 8,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"),myOptions);
}
</script>
</head>
<body>
<p onClick="initialize()">Search</p>
<div id="map_canvas" style="width:100%; height:100%"></div>
</body>
</html>
在上面的代码中,纬度和经度坐标位于JavaScript函数内部。 但是,我想要这样的HTML段落标记:
<p onClick=".....">6.234235, 34.234345</p>
我想将上述值作为URl传递给:
https://www.google.lk/maps/@[lat],[long]
有谁知道如何将这样的URL传递给上面的函数?
一种可能的方法是从<p>
节点收集坐标并将其解析为Google Maps Initialize函数。
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<style type="text/css">
html { height: 100% }
body { height: 100%; margin: 0; padding: 0 }
#map_canvas { height: 100% }
</style>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?key*****************&sensor=true"></script>
<script type="text/javascript">
function initialize() {
var coords = document.getElementById('coords').innerHTML.split(",");
var lat = coords[0];
var lng = coords[1];
var myOptions = {
center: new google.maps.LatLng(lat, lng),
zoom: 8,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"),myOptions);
var myMarkerLatlng = new google.maps.LatLng(lat,lng);
var marker = new google.maps.Marker({
position: myMarkerLatlng,
map: map,
title: 'Hello World!'
});
}
</script>
</head>
<body>
<p onClick="initialize()">Search</p>
<p id="coords" onClick="initialize()">6.234235, 34.234345</p>
<div id="map_canvas" style="width:100%; height:100%"></div>
</body>
</html>
这有点晚了,但是我在寻找自己的答案时偶然发现了这一点。 我根据在这里学到的知识找到了解决方案:
在标记中:
<div id="map" data-lat="<?php echo $locationInfo[0]['lat']; ?>" data-lon="<?php echo $locationInfo[0]['lng']; ?>"></div>
用Javascript:
var themap = document.getElementById('map');
//console.log('lat: ' +themap.dataset.lat);
//console.log('lon: ' +themap.dataset.lon);
var myOptions = {
center: new google.maps.LatLng(themap.dataset.lat, themap.dataset.lon),
zoom: 8,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
我尚未在您的代码中对其进行测试,但这对我有用。
要从p标记中获取值以及一个ID,并将其包含在您的initialize()函数中:
var latLng = document.getElementById('yourId').innerHTML.split[', '];
var myOptions = {
center: new google.maps.LatLng(latLng[0], latLng[1]),
zoom: 8,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
如果您有多个p标记,用户可以单击它们在不同位置的位置,请设置一个事件侦听器,并在事件对象而不是document.getElementById()上调用getSource()。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.