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

报告相同问题?

悬赏问题

  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码