開發手機定位App

============問題描述============java


最近想開發一個手機定位功能,在網上搜了一下,通常都是收費的。
請問各位有沒有什麼好的意見,或者好的能夠參考的網站。
謝謝!!!

============解決方案1============android


能夠調用Google的定位服務

============解決方案2============git


樓主有好方法艾特我下

============解決方案3============json


http://www.slmsn.com

僅供參考

============解決方案4============api


這個網站的原理不就是經過GPS或者基站來定位嗎 這個很簡單啊

============解決方案5============服務器


引用 1 樓 qq237121087 的回覆:
能夠調用Google的定位服務
你記得下載Google的API 

============解決方案6============app


這有什麼難的啊,你是本身測試仍是商用?
定位的,無論是google官方調gps api,仍是百度地圖,都是有誤差的。
若是僅僅是測試,就用google官方的就行了,若是是商用,你須要去購買一份LBS糾偏數據,否則誤差不是通常的大。

============解決方案7============dom


import java.util.Map;

import org.json.JSONObject;

import android.app.Activity;

import android.app.PendingIntent;

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.net.Uri;

import android.os.Bundle;

import android.util.Log;

import com.baidu.location.BDLocation;

import com.baidu.location.BDLocationListener;

import com.baidu.location.LocationClient;

import com.baidu.location.LocationClientOption;

import com.baidu.platform.comapi.basestruct.GeoPoint;

import com.taocaiku.gaea.common.TckApp;

import com.taocaiku.gaea.domain.Domain;

import com.taocaiku.gaea.domain.Json;

import com.taocaiku.gaea.domain.Region;

import com.taocaiku.gaea.domain.context.Container;

import com.taocaiku.gaea.domain.context.OtherUrls;

import com.taocaiku.gaea.domain.context.TckUrls;

import com.taocaiku.gaea.service.BackupService;

import com.taocaiku.gaea.service.SellerMarketService;



/**

 * 手機座標定位的工具類

 * @author TCK-001

 * @version 1.0

 */

public final class PointUtil {

	

	private PointUtil() {}

	private static PointUtil bean = new PointUtil();

	private double[] myPoint = new double[3];

	

	/** 天安門的座標 */

	public static final GeoPoint CENTER_POINT = new GeoPoint((int) (39.945d * 1E6), (int) (116.404d * 1E6));

	

	/**

	 * 單例模式得到bean

	 * @return FileUtil

	 */

	public static PointUtil get() {

		return bean;

	}

	

	private LocationListener listener = new LocationListener() {

		

		public void onStatusChanged(String provider, int status, Bundle extras) {

		}



		public void onProviderEnabled(String provider) {

		}



		public void onProviderDisabled(String provider) {

		}



		public void onLocationChanged(Location location) {

			if (location != null) {

				double[] old = myPoint;

				myPoint = new double[] { location.getLongitude(), location.getLatitude(), location.getAltitude() };

				if (old[0] != myPoint[0] || old[1] != myPoint[1]) {

					if (MapUtil.get().getDistance(old, myPoint) > 100d) {

						editMemberPoint();

					}

					if (MapUtil.get().getDistance(old, myPoint) > 1000d) {

						SellerMarketService.get().editDistance();

					}

					if (MapUtil.get().getDistance(old, myPoint) > 10000d || ToolUtil.get().isBlank(Container.city)) {

						getCity();

					}

					doCallback();

				}

			}

		}

		

	};

	

	private BDLocationListener baiduListener = new BDLocationListener() {

		

		public void onReceivePoi(BDLocation location) {

			if (null == location) {return;}

		}

		

		public void onReceiveLocation(BDLocation location) {

			if (location != null) {

				double[] old = myPoint;

				myPoint = new double[] { location.getLongitude(), location.getLatitude(), location.getAltitude() };

				if (old[0] != myPoint[0] || old[1] != myPoint[1]) {

					if (MapUtil.get().getDistance(old, myPoint) > 100d) {

						editMemberPoint();

					}

					if (MapUtil.get().getDistance(old, myPoint) > 1000d) {

						SellerMarketService.get().editDistance();

					}

					if (MapUtil.get().getDistance(old, myPoint) > 10000d || ToolUtil.get().isBlank(Container.city)) {

						getCity();

					}

					doCallback();

				}

			}

		}

	};

	

	// 註冊一個普通座標

	private void plainReg() {

		LocationManager manager = (LocationManager) pointContext.getSystemService(Context.LOCATION_SERVICE);

		boolean isGps = manager.isProviderEnabled(LocationManager.GPS_PROVIDER);

		boolean isNetWork = manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

		if (!isGps || !isNetWork) {

			Intent GPSIntent = new Intent(); 

			GPSIntent.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");

			GPSIntent.addCategory("android.intent.category.ALTERNATIVE"); 

			GPSIntent.setData(Uri.parse("custom:3"));

			try {PendingIntent.getBroadcast(pointContext, 0, GPSIntent, 0).send();} catch (Exception e) {}

		}

		String provider = LocationManager.GPS_PROVIDER;

		if (manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {

			Criteria criteria = new Criteria();

			criteria.setAccuracy(Criteria.ACCURACY_FINE);// 獲取精準位置

			criteria.setCostAllowed(true);// 容許產生開銷

			criteria.setPowerRequirement(Criteria.POWER_HIGH);// 消耗大的話,獲取的頻率高

			criteria.setSpeedRequired(true);// 手機位置移動

			criteria.setAltitudeRequired(true);// 海拔

			provider = manager.getBestProvider(criteria, true);// 使用GPS衛星

		} else {

			provider = LocationManager.NETWORK_PROVIDER;

		}

		manager.requestLocationUpdates(provider, 1000, 1f, listener);

	}

	

	// 註冊一個百度的座標

	private void baiduReg() {

		LocationClient client = new LocationClient(pointContext);

		client.registerLocationListener(baiduListener);

		LocationClientOption option = new LocationClientOption();

		option.setOpenGps(true);// 打開gps

		option.setCoorType("bd09ll"); // 設置座標類型

		option.setScanSpan(1000);

		client.setLocOption(option);

		client.start();

	}

	

	// 更新用戶的位置

	private void editMemberPoint() {

		if (!ViewUtil.get().isNet() || null == Container.member) {return;}

		new Thread() {

			public void run() {

				try {

					ToolUtil tool = new ToolUtil(Domain.LONG_REQUEST_TIME * 2, TckUrls.POINT_EDIT_URL);

					Map<String, Object> map = tool.createMap(new String[] { "longitude", "latitude", "ip" }, 

							new Object[] { myPoint[0], myPoint[1], tool.getLocalIp() });

					tool.requestTck(map, false);

				} catch (Exception e) {

					Log.e("AbstractActivity", "更新用戶的位置", e);

				}

			}

		}.start();

	}

	

	// 獲取當前城市

	private void getCity() {

		if (!ViewUtil.get().isNet()) {return;}

		new Thread() {

			public void run() {

				try {

					String url = OtherUrls.BAIDU_GEOCODER + "?ak=" + Domain.URL_MAP_KEY + "&location=" + getMyPoint()[1] + "," + getMyPoint()[0] + "&output=json&pois=1";

					ToolUtil tool = new ToolUtil(Domain.LONG_REQUEST_TIME * 2, url);

					JSONObject json = new JSONObject(tool.sound(tool.getRequestUrl(), null, null, null, null, false));

					JSONObject jsonObj = json.getJSONObject("result").getJSONObject("addressComponent");

					Container.province = jsonObj.getString("province");

					Container.city = jsonObj.getString("city");

					findCity();

				} catch (Exception e) {

					Log.e("AbstractActivity", "獲取當前城市", e);

				}

			}

		}.start();

	}

	

	// 去淘材庫服務器查找城市

	private void findCity() throws Exception {

		ToolUtil tool = new ToolUtil(Domain.LONG_REQUEST_TIME, TckUrls.REGION_FIND_CITY_URL);

		String params = tool.getMapParam(tool.createMap(new String[] { "province", "city" }, new Object[] { Container.province, Container.city }));

		String response = BackupService.get().getData(tool.getRequestUrl(), params);

		response = (!tool.isBlank(response) ? response : tool.sound(tool.getRequestUrl(), Domain.REQUEST_POST, params, null, null, false));

		if (tool.isBlank(response)) {return;}

		BackupService.get().add(tool.getRequestUrl(), params, response);

		Json result = BeanUtil.get().turnJsonVo(response);

		String regionStr = result.getKeyData("result");

		if (tool.isBlank(regionStr)) {return;}

		Container.region = BeanUtil.get().jsonToBean(regionStr, Region.class);

	}

	

	/**

	 * 註冊一個週期性座標(百度座標和原來的手機座標都使用)

	 */

	public void register() {

		pointContext = TckApp.get().getApplicationContext();

		baiduReg();

		plainReg();

	}

	

	/**

	 * 執行回調事件

	 */

	private void doCallback() {

		if (pointContext instanceof Activity && !ToolUtil.get().isBlank(pointCallback)) {

			ViewUtil.get().runCallback(ToolUtil.get().createMap(new String[] { "callback", "activity" }, new Object[] { pointCallback, pointContext }));

		}

	}

	

	/**

	 * 得到手機當前的地理座標: 經度,緯度,海拔

	 * @return double[]

	 */

	public double[] getMyPoint() {

		return myPoint;

	}

	

	/**

	 * 設置當前容器,若是不設置,默認爲CarApp的實例

	 * @param pointContext

	 */

	public void setPointContext(Context pointContext) {

		this.pointContext = pointContext;

	}



	/**

	 * 設置導航時的回調事件

	 * @param pointCallback

	 */

	public void setPointCallback(String pointCallback) {

		this.pointCallback = pointCallback;

	}

	

	private Context pointContext = null;

	private String pointCallback = null;



}


啓動定位:
PointUtil.get().register();


得到手機定位:
double[] point = PointUtil.get().getMyPoint();
相關文章
相關標籤/搜索