我希望locations变量从数据库中获取经度和纬度,从而将它们聚类显示 请在这方面需要帮助,我希望从具有存储这些数据的表的数据库中获取所有经度和纬度
<script>
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 3,
//mkoa wa dodoma location
center: {lat: -6.1630, lng: 35.7516}
});
// Create an array of alphabetical characters used to label the markers.
var labels = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
// Add some markers to the map.
// Note: The code uses the JavaScript Array.prototype.map() method to
// create an array of markers based on a given "locations" array.
// The map() method here has nothing to do with the Google Maps API.
var markers = locations.map(function(location, i) {
return new google.maps.Marker({
position: location,
label: labels[i % labels.length]
});
});
// Add a marker clusterer to manage the markers.
var markerCluster = new MarkerClusterer(map, markers,
{imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'});
}
//kwajiri ya miji
var locations = [
{lat: -6.1730, lng: 35.7516},
{lat: -6.2330, lng: 35.7523},
{lat: -6.2330, lng: 35.7523},
{lat: -6.2330, lng: 35.7523},
{lat: -6.2330, lng: 35.7523},
{lat: -6.1730, lng: 35.7516},
{lat: -6.1730, lng: 35.7516},
{lat: -7.773094,lng: 35.699120},
{lat: -7.773094,lng: 35.699120},
{lat: -7.773094,lng: 35.699120},
{lat: -5.030461,lng: 32.819431},
{lat: -5.030461,lng: 32.819431},
{lat: -5.030461,lng: 32.819431},
{lat: -5.030461,lng: 32.819431},
{lat: -8.909401,lng: 33.460773},
{lat: -8.909401,lng: 33.460773},
{lat: -8.909401,lng: 33.460773},
{lat: -6.802353,lng: 39.279556},
{lat: -6.802353,lng: 39.279556},
{lat: -6.802353,lng: 39.279556},
{lat: -6.802100,lng: 39.279119},
{lat: -6.1730, lng: 35.7516}
]
</script>
如您所见,变量位置在数组中
答案 0 :(得分:0)
客户端脚本AJAX调用可能看起来像这样:
$.ajax({
type: "GET",
url : "https://epicsite.com/getLocations.php",
success : function (data)
{
if(data.error) {
console.log(data.error);
} else {
console.log(data);
callFunctionToAddMarkersToMap(data);
}
}
});
服务器端脚本可能看起来像这样:
<?php
$con = mysqli_connect('localhost','user','pass','db');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"locations");
$sql="SELECT * FROM locations";
$result = mysqli_query($con,$sql);
mysqli_close($con);
return $result;
?>
因此,首先要在客户端脚本中调用要获取标记坐标的AJAX路由。然后它将调用函数callFunctionToAddMarkersToMap(data)
,您将查询结果作为参数传递。然后在该函数中,您只需循环给定查询结果并将标记添加到地图。