php is not matching ios lon and lat with a mysql l

2019-08-31 03:06发布

问题:

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

回答1:

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.

I have also changed print $doc->saveXML(); toecho $doc->saveXML();

I would also try hard coding the parameters ie $lat =55.00; etc to ensure the required output is obtained.

The formula above i sin miles to change to km use 6371 instead of 3959