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
原创粉丝点击