首页 技术 正文
技术 2022年11月20日
0 收藏 385 点赞 4,262 浏览 5723 个字

因为在我的寝室google基站定位返回的数据总是为空,所以换成百度地图,发现百度地图开发起来非常方便,提供了许多有用的工具,地图的加载速度也比google地图快许多。

为了加强记忆,写一点android 百度地图开发常用的方法。

1初始化

MapManager mBMapMan = new BMapManager(this);
boolean isSuccess = mBMapMan.init(this.mStrKey, new MyGeneralListener());

isSuccess 的值为true,则地图初始化成功

2定位|设置定位监听器

mBMapMan.getLocationManager().setNotifyInternal(10, 5);
   mBMapMan.start();
   
   mLocationListener = new LocationListener(){

public void onLocationChanged(Location location) 
{
// TODO Auto-generated method stub
// TODO Auto-generated method stub
if(location != null)
{
String strLog = String.format(“您当前的位置:\r\n” +
“纬度:%f\r\n” +
“经度:%f”,
location.getLatitude(), location.getLongitude());
Log.v(“Application-strLog”, strLog);
mBMapMan.getLocationManager().removeUpdates( mLocationListener);
}
}

};
       mBMapMan.getLocationManager().requestLocationUpdates(mLocationListener);

3使用实例

duduApp app = (duduApp)this.getApplication();
        // 如果使用地图SDK,请初始化地图Activity
        super.initMapActivity(app.mBMapMan);
        
        mMapView = (MapView)findViewById(R.id.map);
        mMapView.setBuiltInZoomControls(true);  //设置启用内置的缩放控件
        
        mMapController = mMapView.getController();  // 得到mMapView的控制权,可以用它控制和驱动平移和缩放
        GeoPoint point = new GeoPoint(latitudeE6,longitudeE6);  //用给定的经纬度构造一个GeoPoint,单位是微度 (度 * 1E6)
        mMapController.setCenter(point);  //设置地图中心点
        
        mMapController.setZoom(zoomLevel);    //设置地图zoom级别
        mMapView.setDrawOverlayWhenZooming(true);
        Drawable marker = getResources().getDrawable(R.drawable.iconmarka);  //得到需要标在地图上的资源
marker.setBounds(0, 0, marker.getIntrinsicWidth(), marker
.getIntrinsicHeight());   //为maker定义位置和边界

OverItemT overitem = new OverItemT(marker,1);
mMapView.getOverlays().add(overitem); //添加ItemizedO

4覆盖物

class OverItemT extends ItemizedOverlay<OverlayItem> {

public List<OverlayItem> mGeoList = new ArrayList<OverlayItem>();
private Drawable marker;

public OverItemT(Drawable marker,int count) {
super(boundCenterBottom(marker));

this.marker = marker;

// 用给定的经纬度构造GeoPoint,单位是微度 (度 * 1E6)
GeoPoint p1 = new GeoPoint(latitudeE6, longitudeE6);
GeoPoint p2 = new GeoPoint((int)(DuDuBLL.getInstance(getApplicationContext()).model.bLatitude*1E6),
(int)(DuDuBLL.getInstance(getApplicationContext()).model.bLongitude*1E6));

// 构造OverlayItem的三个参数依次为:item的位置,标题文本,文字片段
mGeoList.add(new OverlayItem(p1, “P1”, “店家位置”));
mGeoList.add(new OverlayItem(p2, “P2”, “我的位置”));

populate();  //createItem(int)方法构造item。一旦有了数据,在调用其它方法前,首先调用这个方法
}

public void updateOverlay()
{
populate();
}

@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {

// Projection接口用于屏幕像素坐标和经纬度坐标之间的变换
Projection projection = mapView.getProjection(); 
Point p1=null;
Point p2=null;
for (int index = size() – 1; index >= 0; index–) 
{ // 遍历mGeoList
OverlayItem overLayItem = getItem(index); // 得到给定索引的item

String title = overLayItem.getTitle();

Log.v(“map_title”, title);
Paint paintText = new Paint();
paintText.setColor(Color.BLUE);
paintText.setTextSize(15);
if(title.equals(“P1”))
{
// 把经纬度变换到相对于MapView左上角的屏幕像素坐标
p1= projection.toPixels(overLayItem.getPoint(), null);

// 可在此处添加您的绘制代码

canvas.drawText(“店家”, p1.x-30, p1.y-20, paintText); // 绘制文本
}
else
{
p2= projection.toPixels(overLayItem.getPoint(), null); 
canvas.drawText(“我的位置”, p2.x-30, p2.y-20,  paintText); // 绘制文本
}
}

super.draw(canvas, mapView, shadow);
//调整一个drawable边界,使得(0,0)是这个drawable底部最后一行中心的一个像素
boundCenterBottom(marker);
}

@Override
protected OverlayItem createItem(int i) {
// TODO Auto-generated method stub
return mGeoList.get(i);
}

@Override
public int size() {
// TODO Auto-generated method stub
return mGeoList.size();
}
@Override
// 处理当点击事件
protected boolean onTap(int i) {
setFocus(mGeoList.get(i));

return true;
}

@Override
public boolean onTap(GeoPoint arg0, MapView arg1) {
// TODO Auto-generated method stub
// 消去弹出的气泡

return super.onTap(arg0, arg1);
}
}
   
}

5路线查询

MKSearch mSearch=new MKSearch();

mSearch.init(app.mBMapMan,new MKSearchListener(){

public void onGetAddrResult(MKAddrInfo arg0, int arg1) {
// TODO Auto-generated method stub

}

public void onGetBusDetailResult(MKBusLineResult arg0,
int arg1) {
// TODO Auto-generated method stub

}

public void onGetDrivingRouteResult(MKDrivingRouteResult res,int error) {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
// 错误号可参考MKEvent中的定义
if (error != 0 || res == null) {
Toast.makeText(mapActivity.this, “抱歉,未找到结果”, Toast.LENGTH_SHORT).show();
return;
}
         //获得路线距离
int distance = res.getPlan(0).getRoute(0).getDistance();
RouteOverlay routeOverlay = new RouteOverlay(mapActivity.this, mMapView);
// 此处仅展示一个方案作为示例
routeOverlay.setData(res.getPlan(0).getRoute(0));
 //  mMapView.getOverlays().clear();
mMapView.getOverlays().add(routeOverlay);
mMapView.invalidate();
   
mMapView.getController().animateTo(res.getStart().pt);
}

public void onGetPoiResult(MKPoiResult arg0, int arg1,
int arg2) {
// TODO Auto-generated method stub

}

public void onGetRGCShareUrlResult(String arg0, int arg1) {
// TODO Auto-generated method stub

}

public void onGetSuggestionResult(MKSuggestionResult arg0,
int arg1) {
// TODO Auto-generated method stub

}

public void onGetTransitRouteResult(
MKTransitRouteResult arg0, int arg1) {
// TODO Auto-generated method stub

}

public void onGetWalkingRouteResult(
MKWalkingRouteResult res, int error) {
if (error != 0 || res == null) {
Toast.makeText(mapActivity.this, “抱歉,未找到结果”, Toast.LENGTH_SHORT).show();
return;
}
         //获得路线距离
int distance = res.getPlan(0).getRoute(0).getDistance();
RouteOverlay routeOverlay = new RouteOverlay(mapActivity.this, mMapView);
// 此处仅展示一个方案作为示例
routeOverlay.setData(res.getPlan(0).getRoute(0));
   mMapView.getOverlays().clear();
mMapView.getOverlays().add(routeOverlay);
mMapView.invalidate();
   
mMapView.getController().animateTo(res.getStart().pt);

}

});

//步行路线

mSearch.walkingSearch(null, startNode , null, endNode);

具体请参考http://developer.baidu.com/map/sdkandev-question.htm

6google地图与百度地图坐标体系的装换

转换方法使用的是百度的api。
转换方法为:
http://api.map.baidu.com/ag/coord/convert?from=0&to=4&x=120.0904441&y=30.3056719
在type1和type2中填上相应的值就能转换。
gps坐标的type=0
google坐标的type=2
baidu坐标的type=4
得到的结果形式为:
{“error”:0,”x”:”MTE2LjM1MTA3ODgzMDM=”,”y”:”MzkuOTgwOTIwNDEwMTU2″}x和y都经过了base64转换。

百度地图开发起来非常方便,感兴趣的可以去看api

http://developer.baidu.com/map/sdk-android.htm

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,075
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,551
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,399
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,811
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,893