0

我正在尝试从 ios 获取 lat 和 lon 坐标的输出(这工作正常),将其发送到 php 以使用 MySQL 进行查询,并让 php 将 xml 文档发送回 ios(此步骤不起作用,因为它是不带回该位置的mysql条目),然后在iOS UItableview上解析它(这也很好)。我试图让它与 XML 一起工作,因为我已经得到了一个更简单的 xml 脚本已经在它上面运行。但可能是由于缺乏 php 经验而导致的错误,我无法让这个 php 脚本工作!我在我的 php 脚本中做错了什么?谢谢!哦,还有,mysql 中的数据类别包括“lon”、“lat”和“name”(表示附近朋友或家人的名字)!如果有人想知道,php查询iOS纬度和经度不使用xml输出搜索附近的mysql lat和lon

<?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();
?>
4

1 回答 1

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;等以确保获得所需的输出。

上面的公式 i sin miles 改为 km 使用 6371 而不是 3959

于 2012-10-14T12:23:30.470 回答