开发手机定位App

============问题描述============


最近想开发一个手机定位功能,在网上搜了一下,一般都是收费的。
请问各位有没有什么好的意见,或者好的可以参考的网站。
谢谢!!!

============解决方案1============


可以调用Google的定位服务

============解决方案2============


楼主有好方法艾特我下

============解决方案3============


http://www.slmsn.com

仅供参考

============解决方案4============


这个网站的原理不就是通过GPS或者基站来定位吗 这个很简单啊

============解决方案5============


引用 1 楼 qq237121087 的回复:
可以调用Google的定位服务
你记得下载Google的API 

============解决方案6============


这有什么难的啊,你是自己测试还是商用?
定位的,不管是google官方调gps api,还是百度地图,都是有偏差的。
如果仅仅是测试,就用google官方的就好了,如果是商用,你需要去购买一份LBS纠偏数据,不然偏差不是一般的大。

============解决方案7============


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();

posted on 2014-10-17 10:27  android基础教程  阅读(1578)  评论(0编辑  收藏  举报

导航

我要啦免费统计