我有以下代码,它工作正常,但只选择一个位置, 我有多个位置反对'cid',我想得到并显示所有位置。
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<title>Simple markers</title>
<style>
html, body {
height: 100%;
margin: 0;
padding: 0;
}
#map {
height: 100%;
}
</style>
</head>
<body>
<div id="map"></div>
<?php
$sql = "select * from locator_areas where cid=$city";
$res = mysql_query($sql);
while($row=mysql_fetch_assoc($res)){
?>
<script> var myLatLng = {lat: <?php echo $row['latitude'] ?>, lng: <?php echo $row['longitude']?>};</script>
<?php } ?>
<script>
function initMap() {
icon='img/bk-loc.png';
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 4,
center: myLatLng
});
var marker = new google.maps.Marker({
position: myLatLng,
map: map,
icon:icon,
title: "<?php echo $long.' || '. $lat?> "
});
}
</script>
<script async defer
src="http://maps.googleapis.com/maps/api/js?key=myKEY&callback=initMap">
</script>
<script> initMap();</script>
</body>
答案 0 :(得分:0)
您可以多次添加新的google.Maps.Marker()来添加多个标记。迭代数组来执行此操作是有意义的。
答案 1 :(得分:0)
您将N个位置保存在一个变量中。在该变量中,仅保存最后一个位置。如果要在内存中保存N个位置,则必须使用数组。
要将PHP变量发送到Javascript,我建议使用PHP和PHP函数json_encode
。
在示例的最后,您正在调用initMap
。由于此功能是从您的googleapis脚本中调用的,因此不需要它。
重写代码应如下所示:
<body>
<div id="map"></div>
<?php
$sql = "select * from locator_areas where cid=$city";
$res = mysql_query($sql);
$positions = array();
while($row=mysql_fetch_assoc($res)){
/* <script> var myLatLng = {lat: <?php echo $row['latitude'] ?>, lng: <?php echo $row['longitude']?>};</script>*/
$positions[] = array(
'lat' => $row['latitude'],
'lng' => $row['longitude'],
'title' => $row['longitude']. ' || ' .$row['latitude']
);
}
?>
<script>
var positions = <?php echo json_encode($positions) ?>;
</script>
<script>
function initMap() {
icon='img/bk-loc.png';
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 4,
center: myLatLng
});
positions.forEach(function(position){
var marker = new google.maps.Marker({
position: {
'lat' : position.lat,
'lng' : position.lng
},
map: map,
icon:icon,
title: position.title
});
});
}
</script>
<script async defer src="http://maps.googleapis.com/maps/api/js?key=myKEY&callback=initMap">
</script>
</body>
答案 2 :(得分:0)
代码应如下所示
<?php
require("phpsqlsearch_dbinfo.php");
// Get parameters from URL
$center_lat = $_GET["lat"];
$center_lng = $_GET["lng"];
$radius = $_GET["radius"];
// Start XML file, create parent node
$dom = new DOMDocument("1.0");
$node = $dom->createElement("markers");
$parnode = $dom->appendChild($node);
/ Opens a connection to a mySQL server
$connection=mysql_connect (localhost, $username, $password);
if (!$connection) {
die("Not connected : " . mysql_error());
}
// Search the rows in the markers table
$query = sprintf("SELECT address, name, lat, lng, ( 3959 * acos( cos( radians('%s') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( lat ) ) ) ) AS distance FROM markers HAVING distance < '%s' ORDER BY distance LIMIT 0 , 20",
mysql_real_escape_string($center_lat),
mysql_real_escape_string($center_lng),
mysql_real_escape_string($center_lat),
mysql_real_escape_string($radius));
$result = mysql_query($query);
$result = mysql_query($query);
if (!$result) {
die("Invalid query: " . mysql_error());
}
?>
查看这些链接https://developers.google.com/maps/articles/phpsqlsearch_v3 https://developers.google.com/maps/articles/phpsqlajax_v3
答案 3 :(得分:0)
您可以使用for循环添加多次来添加多个标记。迭代数组来执行此操作是有意义的。
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<title>Simple markers</title>
<style>
html, body {
height: 100%;
margin: 0;
padding: 0;
}
#map {
height: 100%;
}
</style>
</head>
<body>
<div id="map"></div>
<?php
$sql = "select * from locator_areas where cid=$city";
$res = mysql_query($sql);
while($row=mysql_fetch_assoc($res)){
$i=0;
?>
<script> var myLatLng = new Array();
myLatLng[<?php echo $i; ?>] = "<?php echo $value['latitude']; ?> , <?php echo $value['longitude']; ?>"; </script>
<?php
$i++;
} ?>
<script>
var locations = new Array();
var latlngbounds = new google.maps.LatLngBounds();
for (var i = 0; i < pins.length; i++) {
var coordinates = pins[i].split(", ");
locations[i] = new google.maps.LatLng(coordinates[0], coordinates[1]);
latlngbounds.extend(locations[i]);
}
var mapOptions = {
zoom: 9,
disableDefaultUI: true,
center: myLatlng,
draggable: false,
scrollwheel: false,
disableDoubleClickZoom: true,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById('map'), mapOptions);
//var contentString = "<p>I am HTML</p>";
var image = 'img/bk-loc.png';
var markers = [];
for (var i = 0; i < locations.length; i++){
markers[i] = new google.maps.Marker({
position: locations[i],
map: map,
title: pin_quick_data[i],
icon: image
});
}
</script>
<script async defer
src="http://maps.googleapis.com/maps/api/js?key=myKEY&callback=initMap">
</script>
<script> initMap();</script>
</body>