php不匹配ios lon和lat与mysql lon和lat正确匹配

时间:2012-10-14 05:10:09

标签: php mysql ios xml location

我试图从ios得到lat和lon坐标的输出(这很好),发送到php用MySQL查询并让php发送一个xml文件回ios(这一步不能正常工作)因为它没有带回该位置内的mysql条目),然后在iOS UItableview上解析它(这也正常工作)。我试图让它与XML一起工作,因为我已经在它上面运行了一个更简单的xml脚本。但可能是由于PHP缺乏经验的错误,我无法让这个PHP脚本工作!我在php脚本中做错了什么?谢谢!哦,而且,mysql中的数据类别包括“lon”,“lat”和“name”(附近朋友或家人的名字)!如果有人想知道,这是一个早期脚本的进化版本(也产生相同的结果):php query for iOS latitude and longitude not searching for nearby mysql lat and lon with a xml output

<?php
    define( 'LATMILES', 1 / 69 );
    define( 'LONMILES', 1 / 53 );
    if ( isset( $_GET['lat'] ) ) { $lat = (float)$_GET['lat']; }  //Recieve ios input from: NSString *urlString = [NSString stringWithFormat:@"http://www.mysite.com/loc.php?lat=%g&lon=%g&radius=100&q=%@", latitude, longitude, searchBar.text?searchBar.text:@""];
    if ( isset( $_GET['lon'] ) ) { $lon = (float)$_GET['lon']; }  //Recieve ios input from: NSString *urlString = [NSString stringWithFormat:@"http://www.mysite.com/loc.php?lat=%g&lon=%g&radius=100&q=%@", latitude, longitude, searchBar.text?searchBar.text:@""];
    if ( isset( $_GET['radius'] ) ) { $radius = (float)$_GET['radius']; } //Recieve ios input from: NSString *urlString = [NSString stringWithFormat:@"http://www.mysite.com/loc.php?lat=%g&lon=%g&radius=100&q=%@", latitude, longitude, searchBar.text?searchBar.text:@""];
    $minlat = $lat - ( $radius * LATMILES );
    $minlon = $lon - ( $radius * LONMILES );
    $maxlat = $lat + ( $radius * LATMILES );
    $maxlon = $lon + ( $radius * LONMILES );
    $dbh = new PDO('(censored private information');
    $sql = 'SELECT lat, lon, name FROM locations WHERE lat >= ? AND lat <= ? AND lon >= ? AND lon <= ?';
    $params = array( $minlat, $maxlat, $minlon, $maxlon );
    if ( isset( $_GET['q'] ) ) {
      $sql .= " AND name LIKE ?";
      $params []= '%'.$_GET['q'].'%';
    }
    $q = $dbh->prepare( $sql );
    $q->execute( $params );
    $doc = new DOMDocument();
    $r = $doc->createElement( "locations" );
    $doc->appendChild( $r );
    foreach ( $q->fetchAll() as $row) {
      $dlat = ( (float)$row['lat'] - $lat ) / LATMILES;
      $dlon = ( (float)$row['lon'] - $lon ) / LONMILES;
      $d = sqrt( ( $dlat * $dlat ) + ( $dlon * $dlon ) );
      if ( $d <= $radius ) {
        $e = $doc->createElement( "location" );
        $e->setAttribute( 'lat', $row['lat'] );
        $e->setAttribute( 'lon', $row['lon'] );
        $e->setAttribute( 'name', $row['name'] );
        $r->appendChild( $e );
      }
    }
    print $doc->saveXML();
?>

1 个答案:

答案 0 :(得分:0)

您的代码过于复杂。首先,查找位置的最佳解决方案是使用Haversine_formula

我简化了您的代码以使用它。

<?php
if (isset( $_GET['lat'])){ 
    $lat = (float)$_GET['lat']; 
}  
if ( isset( $_GET['lon'])){ 
    $lon = (float)$_GET['lon']; 
}  
if ( isset( $_GET['radius'])){ 
    $radius = (float)$_GET['radius'];
} 
if ( isset( $_GET['q'])){ 
    $name = $_GET['q'];
} 

$dbh = new PDO('(censored private information');
//
$sql = "SELECT  name, lat, lon, ( 3959 * acos( cos( radians('%s') ) * cos( radians( lat) ) * cos( radians( lon ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( lon) ) ) ) AS distance FROM location WHERE `name` LIKE '%s' HAVING distance < '%s' ORDER BY distance LIMIT 0 , 20",
mysql_real_escape_string($lat),
mysql_real_escape_string($lon),
mysql_real_escape_string($lat),
mysql_real_escape_string($name),
mysql_real_escape_string($radius));

$q = $dbh->prepare( $sql );
$q->execute( $params );
$doc = new DOMDocument();
$r = $doc->createElement( "locations" );
$doc->appendChild( $r );
foreach ( $q->fetchAll() as $row) {
  //$dlat = ( (float)$row['lat'] - $lat ) / LATMILES;
  //$dlon = ( (float)$row['lon'] - $lon ) / LONMILES;
  //]$d = sqrt( ( $dlat * $dlat ) + ( $dlon * $dlon ) );
  //if ( $d <= $radius ) {
    $e = $doc->createElement( "location" );
    $e->setAttribute( 'lat', $row['lat'] );
    $e->setAttribute( 'lon', $row['lon'] );
    $e->setAttribute( 'name', $row['name'] );
    $r->appendChild( $e );
  //}
}
echo $doc->saveXML();
?>

我已经注释掉了一些代码,因为我认为这不是必需的。

我还将print $doc->saveXML();更改为echo $doc->saveXML();

我也会尝试对$lat =55.00;等参数进行硬编码,以确保获得所需的输出。

上面的公式我的英里数改为公里使用6371而不是3959