【问题标题】:php is not matching ios lon and lat with a mysql lon and lat properly [duplicate]php没有将ios lon和lat与mysql lon和lat正确匹配[重复]
【发布时间】:2012-10-14 05:10:09
【问题描述】:

我正在尝试从 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();
?>

【问题讨论】:

  • 嗯...你读过我的问题了吗?这是一个脚本,由于脚本执行发生变化,需要重新发布...
  • 系统不允许我撤消我的关闭投票。真可惜。如果您想避免被标记,请不要在您的问题中一遍又一遍地复制和粘贴相同的文本,并尝试将代码减少到相关的部分。否则,它看起来像是表面修改的同一个问题。遵循这些建议可能还有一个附带好处,那就是为您的问题吸引更多答案。
  • 我会记住这些。另一方面,您对修复此脚本有什么建议吗?我应该去哪里看?
  • 检查您的错误日志中的消息。使用调试器来单步调试代码,看看它在哪里做了你意想不到的事情。尤其要检查生成的 SQL 语句是否是您期望生成的。手动运行它,看看你是否得到了你期望的结果。您致电execute(),但似乎没有检查错误结果。毫无疑问,所有 PDO 代码都应该包含在 try/catch 中。
  • 谢谢!在 iOS 中,我什么也得不到。在浏览器中,我什么也得不到。还有其他替代程序可以用来检查我的 php 代码吗?

标签: php mysql ios xml location


【解决方案1】:

您的代码过于复杂。首先,查找位置的最佳解决方案是使用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-&gt;saveXML();改成了echo $doc-&gt;saveXML();

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

上面的公式我用 6371 代替 3959 换成公里

【讨论】:

  • 谢谢!我已经为我的脚本硬编码了一个参数并且它可以工作,但它不适用于 ios 位置。出于某种原因,这确实有效!谢谢!!!
猜你喜欢
  • 2012-01-08
  • 2018-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多