我正在尝试使用 Google Maps API 创建可以用作标记的纬度和经度数组(来自 mySQL 城市数据库)。
我不确定如何在 PHP 中动态提取城市并在 javascript 中对其进行地理编码。
这就是我到目前为止所拥有的。我知道地理编码器线不正确,但我不确定如何使用它。任何建议或解决方案都会非常有帮助。
var geocoder = google.maps.Geocoder();
var markers = [
<?php
//orgnize fans by city
$query = "SELECT city, state, COUNT(*) fans FROM users GROUP BY city ORDER BY fans DESC";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
//pulls the city, state code from the database and stores it as a string in $address
$address = '"' . $row['city'] . ", " . $row['state'] . '"';
// "$row['city'].", ".$row['state']";
geocoder.getLatLng($address, function(point) {
$latitude = point.y;
$longitude = point.x;
});
echo "{ lat: \"".$latitude.", lng: ".$longitude.", name: "'"'.$row['city'].", ".$row['state'].'"'"},\n ";
echo "<br/>";
}
?>
];
//var locationNameArray = ['Portland','Seattle','Los Angeles','San Diego','Las Vegas'];
// Create the markers ad infowindows.
for (index in markers) addMarker(markers[index]);
function addMarker(data) {
// Create the marker
var marker = new google.maps.Marker({
position: new google.maps.LatLng(data.lat, data.lng),
map: map,
title: data.name
});