dsxcv5652 2013-07-19 01:21
浏览 27
已采纳

如何使用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!

  • 写回答

1条回答 默认 最新

  • doujiaozhan4397 2013-07-19 01:39
    关注

    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]);
            }
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)