如何使用PHP和Javascript在列表中获取相同的值
问题描述:
I get a list from a table in my database. It contains latitude and longitude. I want to show these makers on Google Map. So here is what i did:
<?php
$exe1 = "SELECT * FROM accident";
$result1 = mysql_query($exe1, $db) or die(mysql_error());
$location = array();
while ($row = mysql_fetch_array($result1)) {
if ($row["latitude"] != "NA") {
$location[] = array(
$row["latitude"],
$row["longitude"],
);
}
}
?>
Then i using JavaScript to get these geo code and push to markers:
for (var i = 0; i < locations.length; i++) {
lat = locations[i][0];
lng = locations[i][1];
}
Because there are so many locations in my table, some of them are have exact same latitude and longitude. I want to show these markers which contain same lat and lng. Anyone know how to do? Appreiciate any help from you!
答
If you want to make only one same location to show;
$location = array();
$hit = array();
while ($row = mysql_fetch_array($result1)) {
if ($row["latitude"] != "NA") {
$lat = $row["latitude"];
$lng = $row["longitude"];
$key = $lat . '_' . $lng;
$location[$key] = array($lat, $lng);
$hit[$key]++;
}
}
$list = array();
foreach ($hit as $key => $num) {
if ($num > 1 ) {
$list[] = $location[$key];
}
}
If you want to make all the same location to show, you can do it like this:
$location = array();
$hit = array();
while ($row = mysql_fetch_array($result1)) {
if ($row["latitude"] != "NA") {
$lat = $row["latitude"];
$lng = $row["longitude"];
$key = $lat . '_' . $lng;
$location[$key][] = array($lat, $lng);
$hit[$key]++;
}
}
$list = array();
foreach ($hit as $key => $num) {
if ($num > 1 ) {
$list = array_merge($list, $location[$key]);
}
}