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