2012-03-19 105 views
1

我在SQLite數據庫中有4,000個位置的表格,包括經度和緯度。根據MKCoordinateRegion課程提供的來自該地區的信息,如何從地圖獲取位於該地區的地點?可能嗎?查找給定區域內的附近位置

首先,我嘗試使用Haversine公式,但我所做的只是排序最接近地圖中心的地方。但我想要位於地圖區域的地方。或者我錯了?

在此先感謝。

回答

4

你可以得到最大/最小

MKCoordinateRegion *mapRegion = myMap.region; 

然後,您可以訪問最大和M:通過訪問區域屬性從您的MKMapView經度和緯度值最小值如下:

maxLatitude = mapRegion.center.latitude + mapRegion.span.latitudeDelta/2; 
minLatitude = mapRegion.center.latitude - mapRegion.span.latitudeDelta/2; 

maxLongitude = mapRegion.center.longitude+ mapRegion.span.longitudeDelta/2; 
minLongitude = mapRegion.center.longitude- mapRegion.span.longitudeDelta/2; 

希望這有助於!如果要將數據存儲在覈心數據,NSSetNSArray中,則可以使用NSPredicate過濾符合上述條件的結果。

以下是您的MKMapViewMKCoordinateRegion的文檔。

+0

就是這樣,謝謝! – 2012-03-19 22:42:27

1

您是否正在嘗試查找矩形區域中的所有位置?

只需設置查詢這樣:

find all locations where latitude > mapMinLat 
        and latitude < mapMaxLat 
        and longitude > mapMinLon 
        and longitude < mapMaxLon 

在您的地圖的邊緣mapMinLat,mapMaxLat,mapMinLon,mapMaxLon。

如果您正在尋找多邊形區域,則需要使用更高級的算法。

+0

的確,我想要在一個矩形區域中的所有地方。根據您的代碼,我如何從地圖獲取最大最小值? – 2012-03-19 21:35:49

0

我完全不知道這是否會解決您的問題,但SpatialLite是一個第三方庫,上面寫着:

SpatiaLite是一個開源庫用來擴展SQLite的 核心,支持完全成熟的空間SQL功能。

望着在功能列表中有一整節命名爲SQL functions that implement spatial operators

滾動自己的SQLite代碼在SO回答了很多次,例如Compiling custom SQLite for an iPhone app