![](/img/trans.png)
[英]Retrieving latitude and longitude from mysql, passing it to google maps api
[英]Getting latitude and longitude of a location using Google API and passing of variables
我是从Google API获得此代码的,我想提取该位置的纬度和经度,以便可以找到其附近的10个最近位置。
Javascript:
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?key=mykeyhere&sensor=true"></script>
<script type="text/javascript">
var geocoder;
var map;
function initialize() {
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(14.5833, 120.9667);
var mapOptions = {
zoom: 8,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
}
function codeAddress() {
var address = document.getElementById("address").value;
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
</script>
身体:
<body onload="initialize()">
<div id="map-canvas" style="width: 320px; height: 480px;"></div>
<div>
<input id="address" type="textbox">
<input type="button" value="Encode" onclick="codeAddress()">
</div>
</body>
有一个文本框,用户将在其中输入位置,而javascript将标记放在Google地图上。 如何分别获得经度和纬度并将其传递给html正文 ?
我想要做的是在获取坐标之后,将在数据库中搜索最近的10个位置(其中纬度和经度是字段)。 然后如何在地图上最近的10个位置放置标记 ? 请帮我。 谢谢!
https://developers.google.com/maps/documentation/geocoding/?csw=1 尝试使用此功能
function getLatLong(address){
var geo = new google.maps.Geocoder;
geo.geocode({'address':address},function(results, status){
if (status == google.maps.GeocoderStatus.OK) {
return results[0].geometry.location;
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
纬度和经度保留在您的results[0]
但在results[0].location.lat()
和results[0].location.lng()
。 可以通过指定要应用于它们的元素并使用信息填充html来将它们传递回html:
document.getElementById('coords').innerHTML(results[0].location.lat() + ', ' + results[0].location.lng());
理想情况下,要向地图添加新标记,您首先需要设置一个数组来保存您创建的标记。 然后,当您拥有新的标记信息时,将其推入阵列。 这将完成两件事。 一,标记将自动添加到地图。 其次,如果您需要以任何方式更改标记(可能是通过从屏幕上清除它们,或者完全删除它们),则可以对阵列执行操作。
这是与此相关的几个快速函数(假设您的数组称为markers
。
function clearMarkers() { // clears markers but doesn't remove them from the markers array
for (var i = 0, l = this.markers.length; i < l; i++) {
this.markers[i].setMap(null);
}
};
function deleteMarkers() {
this.markers = [];
}
为了向数组添加标记,您需要对其进行定义,类似于:
function addMarker(latlng) {
var marker = new google.maps.Marker({
draggable: false,
raiseOnDrag: false,
icon: // icon path
animation: google.maps.Animation.DROP,
position: latlng, // google latlng coords
map: // element holding your map
});
markers.push(marker);
}
您需要的所有内容都可以在Google Maps API中找到 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.