2012-06-07 6 views
5

Sono interessato a determinare se una posizione lat/lng è all'interno dei limiti e in cerca di raccomandazioni su un algoritmo. (Javascript o php)Determinare se Lat/Lng nei limiti

Ecco quello che ho finora:

var lat = somelat; 
var lng = somelng; 

if (bounds.southWest.lat < lat && lat < bounds.northEast.lat && bounds.southWest.lng < lng && lng < bounds.northEast.lng) { 
    'lat and lng in bounds 
} 
volontà

questo lavoro? grazie

+3

Ci stai chiedendo se funzionerà? Stavo per chiederti se ha funzionato. –

+1

Penso che avrete dei problemi se i vostri limiti includono sia le coordinate di longitudine est che ovest. A seconda del sistema di coordinate che stai utilizzando, è probabile che le aree coprano i poli nord/sud, 0 gradi di longitudine (Inghilterra, Africa, ecc.) E 180 W/E (nell'oceano Pacifico) – netfire

+0

@ScottSaunders Sì, sono un po 'meno chiedendo se sembra logico. –

risposta

9

Il semplice confronto nel tuo post funzionerà per le coordinate negli Stati Uniti. Tuttavia, se si desidera una soluzione che è sicuro per la verifica attraverso la linea di data internazionale (dove la longitudine è ± 180 °):

function inBounds(point, bounds) { 
    var eastBound = point.long < bounds.NE.long; 
    var westBound = point.long > bounds.SW.long; 
    var inLong; 

    if (bounds.NE.long < bounds.SW.long) { 
     inLong = eastBound || westBound; 
    } else { 
     inLong = eastBound && westBound; 
    } 

    var inLat = point.lat > bounds.SW.lat && point.lat < bounds.NE.lat; 
    return inLat && inLong; 
} 
4

Come hai chiesto sia Javascript e PHP (e ne avevo bisogno in PHP), I convertito la grande risposta di CheeseWarlock in PHP. Che, come al solito con PHP, è molto meno elegante. :)

function inBounds($pointLat, $pointLong, $boundsNElat, $boundsNElong, $boundsSWlat, $boundsSWlong) { 
    $eastBound = $pointLong < $boundsNElong; 
    $westBound = $pointLong > $boundsSWlong; 

    if ($boundsNElong < $boundsSWlong) { 
     $inLong = $eastBound || $westBound; 
    } else { 
     $inLong = $eastBound && $westBound; 
    } 

    $inLat = $pointLat > $boundsSWlat && $pointLat < $boundsNElat; 
    return $inLat && $inLong; 
} 
Problemi correlati