我有一个MySQL数据库,其中一些lat / lng点代表商店。我将要有一张地图,用户(以及其他东西)可以从下拉列表中选择一种商店,它将更新地图并仅显示这些点。它还会显示侧栏中这些点的信息。
我正试图围绕所有代码如何工作来实现这一切。我甚至不确定我最初如何拉取MySQL lat / lng位置,并将它们转换为javascript / jquery。
有没有人有这方面的快速教程?
答案 0 :(得分:4)
答案 1 :(得分:1)
这是一个为我工作的解决方案,我从数据库中提取数据并使用地址地理编码和谷歌地图api显示它们:
<script type="text/javascript" charset="utf-8">
var customIcons = {
restaurant: {
icon: 'http://labs.google.com/ridefinder/images/mm_20_blue.png',
shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
},
bar: {
icon: 'http://labs.google.com/ridefinder/images/mm_20_red.png',
shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
},
club:
{
icon: 'http://labs.google.com/ridefinder/images/mm_20_yellow.png',
shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
},
church:
{
icon: 'http://labs.google.com/ridefinder/images/mm_20_green.png',
shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
}
};
function initialize()
{
var mapOptions = {
center: new google.maps.LatLng(37.976178, 23.735881),
zoom: 7,
mapTypeId: google.maps.MapTypeId.roadmap
};
var map = new google.maps.Map(document.getElementById("map-canvas"),
mapOptions);
<?php header("content-type: text/html;charset=utf-8");
$getpoints = "SELECT lat, lng, name, address, type FROM markers";
if(!$result = $con->query($getpoints)){
die('There was an error running the query
[' . $con->error . ']');
}
else
{
while ($row = $result->fetch_assoc())
{
$thematic = "'$row[type]'";
$name = "'$row[name]'";
$map_address = "$row[address]";
$url = "http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=".urlencode($map_address);
$lat_long = get_object_vars(json_decode(file_get_contents($url)));
// pick out what we need (lat,lng)
$lat_long = $lat_long['results'][0]->geometry->location->lat . "," . $lat_long['results'][0]->geometry->location->lng;
echo "var myLatlng1 = new google.maps.LatLng($lat_long);
var icon = customIcons[$thematic] || {};
var marker1 = new google.maps.Marker({
position: myLatlng1,
map: map,
icon: icon.icon,
title: '$map_address'
});";
}
}
?>
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>