============問題描述============java
============解決方案1============android
============解決方案2============git
============解決方案3============json
============解決方案4============api
============解決方案5============服務器
============解決方案6============app
============解決方案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();