資源簡介
傳統數據庫索引
存儲用戶登入字段主要包括 userId,lat,lng。分別代表用戶 ID、最近一次 check-in 的經度、緯度。lat/lng 建立復合索引。
然后通過手機的定位,得到自己的位置,比如記為 myLat,myLng。
代碼如下,先做一個計算,算出 1km 所對應的經緯度范圍:
double range = 180 / Math.PI * 1 / 6372.797; //里面的 1 就代表搜索 1km 之內,單位km
double lngR = range / Math.cos(myLat * Math.PI / 180.0);
double maxLat = myLat range;
double minLat = myLat - range;
double maxLng = myLng lngR;
double minLng = myLng - lngR;
然后執行 SQL :
SELECT xxxx FROM checkinTable WHERE ((lat BETWEEN ? AND ?) AND (lng BETWEEN ? AND ?)) LIMIT 100
這四個問號,分別代入變量
minLat、maxLat、minLng、maxLng
然后就可以查詢得到結果
但是,這樣得到的結果不是有序的。
如果要排序,在程序代碼(比如 app 客戶端)執行。
不建議在 SQL 層上執行,因為上述的那個 SQL 是可以用到索引進行查詢的,一旦引入排序后,索引就無法發揮作用,從而會影響效率。
代碼片段和文件信息
package?com.andieguoe.locationdemo;
import?java.util.Iterator;
import?android.app.Activity;
import?android.location.GpsSatellite;
import?android.location.GpsStatus;
import?android.location.Location;
import?android.location.LocationListener;
import?android.location.LocationManager;
import?android.os.Bundle;
import?android.util.Log;
import?android.widget.Toast;
public?class?GpsActivity?extends?Activity?{
private?LocationManager?locationManager;
private?GpsStatus?gpsstatus;
private?static?final?String?TAG?=?“GpsActivity“;
@Override
protected?void?onCreate(Bundle?savedInstanceState)?{
//?TODO?Auto-generated?method?stub
super.onCreate(savedInstanceState);
setContentView(R.layout.gps);
//?獲取到LocationManager對象
locationManager?=?(LocationManager)?getSystemService(LOCATION_SERVIC
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2014-10-22?15:13??nearbydemo-master\
?????文件?????????474??2014-10-22?15:13??nearbydemo-master\.gitignore
?????文件?????????648??2014-10-22?15:13??nearbydemo-master\GETDISTANCE.sql
?????文件???????11515??2014-10-22?15:13??nearbydemo-master\LICENSE
?????目錄???????????0??2014-10-22?15:13??nearbydemo-master\LocationDemo\
?????文件?????????356??2014-10-22?15:13??nearbydemo-master\LocationDemo\.classpath
?????文件?????????815??2014-10-22?15:13??nearbydemo-master\LocationDemo\.project
?????目錄???????????0??2014-10-22?15:13??nearbydemo-master\LocationDemo\.settings\
?????文件?????????173??2014-10-22?15:13??nearbydemo-master\LocationDemo\.settings\org.eclipse.jdt.core.prefs
?????文件????????1359??2014-10-22?15:13??nearbydemo-master\LocationDemo\AndroidManifest.xm
?????文件???????51394??2014-10-22?15:13??nearbydemo-master\LocationDemo\ic_launcher-web.png
?????文件?????????781??2014-10-22?15:13??nearbydemo-master\LocationDemo\proguard-project.txt
?????文件?????????563??2014-10-22?15:13??nearbydemo-master\LocationDemo\project.properties
?????目錄???????????0??2014-10-22?15:13??nearbydemo-master\LocationDemo\res\
?????目錄???????????0??2014-10-22?15:13??nearbydemo-master\LocationDemo\res\drawable-hdpi\
?????文件????????7658??2014-10-22?15:13??nearbydemo-master\LocationDemo\res\drawable-hdpi\ic_launcher.png
?????目錄???????????0??2014-10-22?15:13??nearbydemo-master\LocationDemo\res\drawable-mdpi\
?????文件????????3777??2014-10-22?15:13??nearbydemo-master\LocationDemo\res\drawable-mdpi\ic_launcher.png
?????目錄???????????0??2014-10-22?15:13??nearbydemo-master\LocationDemo\res\drawable-xhdpi\
?????文件???????12516??2014-10-22?15:13??nearbydemo-master\LocationDemo\res\drawable-xhdpi\ic_launcher.png
?????目錄???????????0??2014-10-22?15:13??nearbydemo-master\LocationDemo\res\drawable-xxhdpi\
?????文件???????24777??2014-10-22?15:13??nearbydemo-master\LocationDemo\res\drawable-xxhdpi\ic_launcher.png
?????目錄???????????0??2014-10-22?15:13??nearbydemo-master\LocationDemo\res\layout\
?????文件?????????618??2014-10-22?15:13??nearbydemo-master\LocationDemo\res\layout\gps.xm
?????文件????????1218??2014-10-22?15:13??nearbydemo-master\LocationDemo\res\layout\main.xm
?????文件?????????621??2014-10-22?15:13??nearbydemo-master\LocationDemo\res\layout\network.xm
?????文件?????????621??2014-10-22?15:13??nearbydemo-master\LocationDemo\res\layout\telepony.xm
?????目錄???????????0??2014-10-22?15:13??nearbydemo-master\LocationDemo\res\menu\
?????文件?????????253??2014-10-22?15:13??nearbydemo-master\LocationDemo\res\menu\main.xm
?????目錄???????????0??2014-10-22?15:13??nearbydemo-master\LocationDemo\res\values-sw600dp\
?????文件?????????195??2014-10-22?15:13??nearbydemo-master\LocationDemo\res\values-sw600dp\dimens.xm
............此處省略186個文件信息
評論
共有 條評論