问题描述
我正在尝试从ios获取纬度和经度坐标的输出(此方法工作正常),将其发送至php以使用MySQL查询,并让php将xml文档发送回ios(此步骤无效)因为它没有在该位置取回mysql条目),然后在iOS UItableview上对其进行了解析(这也很好用).我正在尝试使其与XML一起使用,因为我已经在其上运行了一个更简单的xml脚本.但是可能是由于php经验不足而导致的错误,我无法使该php脚本正常工作!我的php脚本在做什么错?谢谢!哦,而且,mysql中的数据类别包括"lon","lat"和"name"(用于附近的朋友或家人的名字)!并且,如果有人想知道的话,这是早期脚本的演进版本(也产生了相同的结果):
I am trying to get the output of the lat and lon coordinates from ios (this is working fine), send it to php to query with MySQL and have the php send an xml document back to ios(this step is not working because it is not bringing back the mysql entry within that location), then parsing it on iOS UItableview (this is working fine too). I am trying to get this to work with XML cause I've gotten a simpler xml script running already on it. But probably due to mistakes from inexperience in php, I cannot get this php script working! What am I doing wrong in my php script? Thanks! Oh, and also, the data categories in mysql consist of "lon" and "lat" and "name" (for the name of a nearby friend or family)! AND in case anyone was wondering, this is an evolved version of an earlier script (that was also producing the same results): 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();
?>
推荐答案
您的代码过于复杂.首先,查找位置的最佳解决方案是使用 Haversine_formula .
Your code is a over complicated. For a start the best solution for finding a location is to use the Haversine_formula.
我已经简化了您的代码以使用它.
I have simplified your code to use it.
<?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();
?>
我已经注释掉了您的一些代码,因为我认为这不是必需的.
I have commented out some of your code as I don't think it is required.
我也将print $doc->saveXML();
更改为echo $doc->saveXML();
我还将尝试对参数$lat =55.00;
等进行硬编码,以确保获得所需的输出.
I would also try hard coding the parameters ie $lat =55.00;
etc to ensure the required output is obtained.
我将英里数转换为公里的公式使用6371而不是3959
The formula above i sin miles to change to km use 6371 instead of 3959
这篇关于php无法将ios lon和lat与mysql lon和lat正确匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!