摘要:Parse - ParseGeoPoint
Parse支援經緯度座標的存放與查詢功能
https://www.parse.com/docs/android_guide#geo
只有三個部分
一、ParseGeoPoint
二、Query
三、whereWithInBox
除了whereWithInBox外,其實還有其它的。
whereWithinKilometers
whereWithinMiles
whereWithinRadians
如何儲存
如下
ParseGeoPoint point = new ParseGeoPoint(mLat,mLon);
parseObject.setLocation(point);
parseObject.saveInBackground();
如何搜尋
ParseQuery query = Talk.getQuery();
query.whereNear("location", userLocation);
query.setLimit(10);
query.findInBackground(new FindCallback(){
另外為了取得手機的經緯度座標。
可以參考
http://www.moke.tw/wordpress/computer/advanced/279
我另外包了一個 class,方便我在任一地方取用。
import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.widget.Toast;
public class LocationService implements LocationListener{
private Double mLat;
private Double mLon;
private boolean getService = false; //是否已開啟定位服務
private Context mContext ;
private LocationManager lms;
private String bestProvider = LocationManager.GPS_PROVIDER; //最佳資訊提供者
public void initial(Context ctx) {
mContext = ctx;
//取得系統定位服務
LocationManager status = (LocationManager) (ctx.getSystemService(Context.LOCATION_SERVICE));
if (status.isProviderEnabled(LocationManager.GPS_PROVIDER) || status.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
getService = true; //確認開啟定位服務
//如果GPS或網路定位開啟,呼叫locationServiceInitial()更新位置
locationServiceInitial();
} else {
Toast.makeText(ctx, "請開啟定位服務", Toast.LENGTH_LONG).show();
ctx.startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)); //開啟設定頁面
}
}
public Double getLongitude() {
return mLon;
}
public Double getLatitude() {
return mLat;
}
public void onResume() {
if(getService) {
lms.requestLocationUpdates(bestProvider, 10000, 1, this);
//服務提供者、更新頻率60000毫秒=1分鐘、最短距離、地點改變時呼叫物件
}
}
public void onPause() {
if(getService) {
lms.removeUpdates(this); //離開頁面時停止更新
}
}
private void locationServiceInitial() {
lms = (LocationManager) mContext.getSystemService(mContext.LOCATION_SERVICE); //取得系統定位服務
Criteria criteria = new Criteria(); //資訊提供者選取標準
bestProvider = lms.getBestProvider(criteria, true); //選擇精準度最高的提供者
Location location = lms.getLastKnownLocation(bestProvider);
getLocation(location);
}
private void getLocation(Location location) { //將定位資訊顯示在畫面中
if(location != null) {
mLon = location.getLongitude(); //取得經度
mLat = location.getLatitude(); //取得緯度
}
else {
Toast.makeText(mContext, "無法定位座標", Toast.LENGTH_LONG).show();
}
}
//當地點改變時
@Override
public void onLocationChanged(Location location) {
getLocation(location);
}
//定位狀態改變
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
//當GPS或網路定位功能開啟
@Override
public void onProviderEnabled(String provider) {
}
//當GPS或網路定位功能關閉時
@Override
public void onProviderDisabled(String provider) {
}
}
GPS精準度策略文章:
http://fecbob.pixnet.net/blog/post/38313657-android-%E6%89%8B%E6%A9%9F%E5%AE%9A%E4%BD%8D%E6%85%A2%E7%9A%84%E8%A7%A3%E6%B1%BA%E6%96%B9%E6%B3%95