首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >选择不同的经度和纬度值并找到它们的中位数

选择不同的经度和纬度值并找到它们的中位数
EN

Stack Overflow用户
提问于 2012-12-22 05:24:03
回答 2查看 1.1K关注 0票数 3

我正在使用Google Maps API V3和jQuery创建一个Phonegap应用程序。

当检测到凹坑时,应用程序将经度值和纬度值分别存储在MySQL数据库中。我需要做的是选择任何相对接近的值,并且很可能是相同的凹坑。

在jQuery/PHP/SQL中,有没有办法获得相对接近的值,找到它们的均值,然后使用这些值继续处理其他一些事情?

基本上我需要的是,一旦一个特定的坑被检测到5次,它将被绘制在谷歌地图上。但是,只有在达到此阈值时才能实现。困难在于,相同的凹坑可能在略有不同的经度和纬度值下报告,这取决于报告它的设备的准确性。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-12-22 16:41:01

当您需要识别新的凹坑附近的凹坑时,下面的代码可以做到这一点。它在使用带错误检查的POD的MySQL查询中使用。

$lat & $lng是新坑的坐标& $radius是搜索半径,单位为米。6378160是地球在赤道的半径,单位是米。

为确保此级别的精度,数据库中的坐标必须至少有4位小数位

编辑

代码语言:javascript
复制
    try {
    // Prepare search statement
    $stmt1 = $dbh->prepare("SELECT id, lat, lng, cnt, ( 6378160 * acos( cos( radians(?) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(?) ) + sin( radians(?) ) * sin( radians( lat ) ) ) ) AS  distance FROM potholes  HAVING distance <  ? ORDER BY distance LIMIT 0,10");
    // Assign parameters
    $stmt1->bindParam(1,$lat);
    $stmt1->bindParam(2,$lng);
    $stmt1->bindParam(3,$lat);
    $stmt1->bindParam(4,$radius);
    //Execute query
    $stmt1->setFetchMode(PDO::FETCH_ASSOC);
    $stmt1->execute();
    if ($stmt1->rowCount()>0) {//Existing pothole 
        // fetch row
        $row = $stmt1->fetch();
        $id = $row['id'];
        $lat1 = $row['lat'];
        $lng1 = $row['lng'];
        $cnt = $row['cnt'];
        $meanLat = (($lat1*$cnt)+$lat)/($cnt+1);
        $meanLng = (($lng1*$cnt)+$lng)/($cnt+1);
        ++$cnt;
        // Prepare UPDATE statement existing pothole
        $stmt2 = $dbh->prepare("UPDATE  `potholes` SET  `lat` = ?,`cnt` =  ? WHERE  `potholes`.`id` = ? LIMIT 1");
        // Assign parameters
        $stmt2->bindParam(1,$meanLat);
        $stmt2->bindParam(2,$cnt);
        $stmt2->bindParam(3,$id);
        $stmt2->execute();
    //  }
    }else{//New pothole
        // Prepare INSERT statement new pothole
        $stmt3 = $dbh->prepare("INSERT INTO `potholes` (`id`, `lat`, `lng`, `cnt`) VALUES (NULL, ?, ?, '1')");
        // Assign parameters
        $stmt3->bindParam(1,$lat);
        $stmt3->bindParam(2,$lng);
        $stmt3->execute();

    }
echo json_encode($data);//Echo to calling page if required
}



catch(PDOException $e) {
    echo "Error Message.". $e->getMessage() ;// Remove or modify after testing 
    file_put_contents('PDOErrors.txt',date('[Y-m-d H:i:s]').", data2c.php, ". $e->getMessage()."\r\n", FILE_APPEND);  
 }
//Close the connection
$dbh = null; 
票数 1
EN

Stack Overflow用户

发布于 2012-12-22 05:46:10

您需要的是Haversine's Formula来计算两个给定位置的经度和纬度坐标之间的距离。在此基础上,您可以编写一个方法,使用以kms为单位的范围检查与给定坐标接近/接近的所有值。

计算距离的哈维正弦公式

代码语言:javascript
复制
$('some_element').click(function(){
    var location1 = [lat, lon] // Specify Longitude and latitude
    var location2 = [lat, lon] // Specify Longitude and latitude

    var lat1 = location1[0], lon1 = location1[1];
    var lat2 = location2[0], lon2 = location2[1];
    var earth = 6371 // Earth's Radius in Kms

    var dLat = (lat2-lat1) * Math.PI / 180;
    var dLon = (lon2-lon1) * Math.PI / 180;
    var nlat1 = (lat1) * Math.PI / 180;
    var nlat2 = (lat2) * Math.PI / 180;
    // Calculation
    var a = Math.sin(dLat/2) * Math.sin(dLat/2) + Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(nlat1) * Math.cos(nlat2);
    var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
    var d = earth * c;
    var distance = Math.round(d*Math.pow(10,2)) / Math.pow(10,2); //Round off to 2 decimal places
    alert(distance);
});
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13997385

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档