Baidu地图的相关开发经验
来源:互联网 发布:礼券自助提货系统源码 编辑:程序博客网 时间:2024/06/05 09:15
知识1:
因为百度地图经纬度和gps经纬度有一定的差距,在实际项目中需要将百度经纬度坐标转化成gps坐标,贴出代码如下:
/** * 步骤1: * @param bx * @param by */public void baidu2Gps(final String bx,final String by){showDialog("请稍后...");//http://api.map.baidu.com/ag/coord/convert?from=0&to=4&x=116.397428&y=39.90923&callback=BMap.Convertor.cbk_7594 //参考传参数方法String url = "http://api.map.baidu.com/ag/coord/convert";//固定不变AjaxParams params = new AjaxParams();params.put("from", "0");params.put("to", "4");params.put("x", bx);params.put("y", by);Callback callback = new Callback(tag,this) {@Overridepublic void onSuccess(String t) {try {JsonParser parser = new JsonParser(); JsonObject root = parser.parse(t).getAsJsonObject(); String base64x = root.get("x").getAsString(); String base64y = root.get("y").getAsString(); double x1 = Double.parseDouble(bx); double y1 = Double.parseDouble(by); //Log.i(TAG +"=======x1===============", x1+""); //Log.i(TAG +"=======y1===============", y1+""); String tempx = new String(Base64Util.decode(base64x)); String tempy = new String(Base64Util.decode(base64y)); //Log.i(TAG +"=======tempx==============", tempx+""); //Log.i(TAG +"=======tempy===============", tempy+""); double x2 = Double.parseDouble(tempx); double y2 = Double.parseDouble(tempy); //Log.i(TAG +"=======x2===============", x2+""); //Log.i(TAG +"=======y2===============", y2+""); x = 2 * x1 - x2; y = 2 * y1 - y2; //Log.i(TAG +"=======x===============", x+""); //Log.i(TAG +"=======y===============", y+""); getGps(x+"", y+"");} catch (Exception e) {e.printStackTrace();Log.i(TAG + "baidu2Gps 坐标转换异常", "baidu map");dimissDialog();}}@Overridepublic void onFailure(Throwable t, int errorNo, String strMsg) {super.onFailure(t, errorNo, strMsg);dimissDialog();}};FinalHttp finalHttp = new FinalHttp();finalHttp.get(url, params, callback);}
/** * * 获得百度坐标对应的gps坐标 * * 步骤2: */public void getGps(String bx,String by){String url = "http://api.map.baidu.com/ag/coord/convert";AjaxParams params = new AjaxParams();params.put("from", "0");params.put("to", "4");params.put("x", bx);params.put("y", by);Callback callback = new Callback(tag,this) {@Overridepublic void onSuccess(String t) {dimissDialog();try {JsonParser parser = new JsonParser(); JsonObject root = parser.parse(t).getAsJsonObject(); //获得真正的gps坐标 String tempx = root.get("x").getAsString(); String tempy = root.get("y").getAsString(); String resultx = new String(Base64Util.decode(tempx)); String resulty = new String(Base64Util.decode(tempy)); MapSendAddressEvent mapSendAddressEvent = new MapSendAddressEvent();mapSendAddressEvent.address = moveAddress;mapSendAddressEvent.latitude = resulty + "";mapSendAddressEvent.longitude = resultx + "";EventBus.getDefault().post(mapSendAddressEvent);finish(); //Log.i(TAG+"============最后结果===========resultx", resultx+""); //Log.i(TAG +"===============最后结果========resulty", resulty+""); } catch (Exception e) {e.printStackTrace();Log.i(TAG + "getGps坐标转换异常", "baidu map");}}@Overridepublic void onFailure(Throwable t, int errorNo, String strMsg) {super.onFailure(t, errorNo, strMsg);dimissDialog();}};FinalHttp finalHttp = new FinalHttp();finalHttp.get(url, params, callback);}
//知识2:
通过百度经纬度转化地方名称 和地方名称转百度经纬度
改知识点用到了一个核心的接口OnGetGeoCoderResultListener,自带两个回调方法onGetReverseGeoCodeResult,onGetGeoCodeResult
//知识点3:
仿uber实现拖动百度地图取出百度地图中心点位置的地方名称,用到一个比较核心的类是OnMapStatusChangeListener(百度地图状态改变的监听)
//用于搜索
GeoCoder mSearchmSearch = GeoCoder.newInstance();
mSearch.setOnGetGeoCodeResultListener(this);
mBaiduMap.setOnMapStatusChangeListener(onMapStatusChangeListener);OnMapStatusChangeListener onMapStatusChangeListener = new OnMapStatusChangeListener() {@Overridepublic void onMapStatusChangeStart(MapStatus arg0) {}//状态结束@Overridepublic void onMapStatusChangeFinish(MapStatus arg0) {//获取百度地图的中心点LatLng latLng = arg0.target;double longitude = latLng.longitude;//经度double latitude = latLng.latitude;//维度LatLng ptCenter = new LatLng(latitude, longitude);mSearch.reverseGeoCode(new ReverseGeoCodeOption().location(ptCenter));}@Overridepublic void onMapStatusChange(MapStatus arg0) {}};
//关于搜索的两个回调方法
@Overridepublic void onGetGeoCodeResult(GeoCodeResult arg0) {}//编码转成实际地址@Overridepublic void onGetReverseGeoCodeResult(ReverseGeoCodeResult result) {if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {Toast.makeText(MapActivity.this, "亲,没找到该地址", 200).show();return;}mBaiduMap.setMyLocationConfigeration(null);mBaiduMap.clear();//mBaiduMap.addOverlay(new MarkerOptions().position(result.getLocation())//.icon(BitmapDescriptorFactory//.fromResource(R.drawable.current_addres_point_icon)));//mBaiduMap.setMapStatus(MapStatusUpdateFactory.newLatLng(result//.getLocation()));LatLng latLng = result.getLocation();mlat = latLng.latitude;//纬度mlong = latLng.longitude;//经度if (!TextUtils.isEmpty(result.getAddress())) {//Toast.makeText(MapActivity.this, result.getAddress(),100).show();moveAddress = result.getAddress();addressView.setText("地址:"+moveAddress);}else {addressView.setText("地址:没有找到该地方名称");}}
0 0
- Baidu地图的相关开发经验
- 百度地图(baidu mao)开发经验
- baidu地图相关
- 开发baidu地图去掉左下角的baidu logo
- Android开发baidu地图的blog url
- Baidu地图开发,定位,检索
- 地图开发相关的资源
- 地图开发相关的资源
- Baidu地图
- 各种技术相关的开发经验
- 基于百度地图的相关开发
- android baidu地图资源的问题
- 百度地图Baidu LBS 的使用
- Baidu 地图 集成出现的异常
- AndroidAnnotation+Baidu 地图遇到的错误
- 软件开发经验相关
- 百度地图开发相关
- 百度地图-定位开发经验
- SimpleConfig结果显示导致app崩溃
- 图像的 2DPCA 与 2D2DPCA 特征提取
- QTcpSocket 及 TCP粘包分析
- 使用UncaughtExceptionHandler重启线程
- 再次复习java正则表达式
- Baidu地图的相关开发经验
- Python中的下划线(译文)
- Java停止线程
- 【poj2001】Shortest Prefixes 字典树
- VMware虚拟机上网络连接(network type)的三种模式--bridged、host-only、NAT
- YTU 1009: University
- 记一次Ubuntu突然无法进入系统的经历
- cookie 和session 的区别详解
- web开发中文字符乱码解决集合文GOOD