1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
package chufan.mybaidumap;
import android.os.Bundle;
import android.widget.Toast;
import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.MKGeneralListener;
import com.baidu.mapapi.MapActivity;
import com.baidu.mapapi.MapController;
import com.baidu.mapapi.MapView;
public class BaiduMapActivity extends MapActivity {
private MapView mapView;
private BMapManager bMapManager;//加载地图引擎
//百度地图的key
private String keyString="7F16C0415EB0C02A070F5EF58B905418EB5A82CE";
//在百度地图上添加一些控件,比如放大缩小
private MapController mapController;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView=(MapView)findViewById(R.id.bmapView);
bMapManager=newBMapManager(BaiduMapActivity.this);
bMapManager.init(keyString,newMKGeneralListener() {
public void onGetPermissionState(int arg0) {
// TODO Auto-generated method stub
if(arg0==300){
Toast.makeText(BaiduMapActivity.this,"输入的Key有错请核实!!", 1).show();
}
}
public void onGetNetworkState(int arg0) {
// TODO Auto-generated method stub
}
});
initMapActivity(bMapManager);
mapView.setBuiltInZoomControls(true);//表示可以设置缩放功能
mapController=mapView.getController();
mapController.setZoom(12);
//需要定义一个经续度:
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
if(bMapManager!=null){
bMapManager.destroy();
bMapManager=null;
}
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
if(bMapManager!=null){
bMapManager.start();
}
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
if(bMapManager!=null){
bMapManager.stop();
}
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
returnfalse;
}
}
|
1
|
mapView.getOverlays().add(newMyOverlay()); //调用新建的图层
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
public class MyOverlay extends Overlay{
//构建在地图上的一个点
private GeoPoint geoPoint=newGeoPoint((int)(39.915*1E6),(int)(116.404*1E6));
//声明一上画笔的工具
private Paint paint=newPaint();
public void draw(Canvas arg0, MapView arg1, boolean arg2) {
// TODO Auto-generated method stub
super.draw(arg0, arg1, arg2);
Point point =mapView.getProjection().toPixels(geoPoint,null);
arg0.drawText("*这里天安门", point.x, point.y, paint);
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
public class MyOverlayItem extends ItemizedOverlay<OverlayItem>{
private List<OverlayItem> list=newArrayList<OverlayItem>();
private double mLat1=39.90923;//表示纬度
private double mLon1=116.397428;//表示经度
private double mLat2=39.90922;//表示纬度
private double mLon2=116.311128;//表示经度
private double mLat3=39.917723;//表示纬度
private double mLon3=116.371128;//表示经度
public MyOverlayItem(Drawable drawable) {
super(drawable);
GeoPoint geoPoint1=newGeoPoint((int)(mLat1*1E6),(int)(mLon1*1E6));
GeoPoint geoPoint2=newGeoPoint((int)(mLat2*1E6),(int)(mLon2*1E6));
GeoPoint geoPoint3=newGeoPoint((int)(mLat3*1E6),(int)(mLon3*1E6));
list.add(newOverlayItem(geoPoint1,"Point1","Point1"));
list.add(newOverlayItem(geoPoint2,"Point2","Point2"));
list.add(newOverlayItem(geoPoint3,"Point3","Point3"));
populate();//需要调用地图刷新功能
}
@Override
protected boolean onTap(int i) {
// TODO Auto-generated method stub
Toast.makeText(BaiduMapActivity.this,list.get(i).getSnippet(), 1).show();
returntrue;
}
@Override
protected OverlayItem createItem(int arg0) {
// TODO Auto-generated method stub
returnlist.get(arg0);
}
@Override
public int size() {
// TODO Auto-generated method stub
returnlist.size();
}
}
|
1
|
mkSearch=newMKSearch();//生成对象
|
1
|
mkSearch.init(bMapManager,newMySearchListener());//MySearchListener重要,所有动作都由它回调
|
1
|
mkSearch.poiSearchNearBy("kfc",newGeoPoint((int)(39.915*1E6),(int)(116.404*1E6)),5000);
|
1
2
3
4
5
6
7
8
9
|
<span class="Apple-tab-span"> </span>if(result==null){
return;//判断是否回调
}
PoiOverlay poiOverlay=newPoiOverlay(BaiduMapActivity.this, mapView);//生成图层
poiOverlay.setData(result.getAllPoi());//给图层画上数据
mapView.getOverlays().add(poiOverlay);//显示图层
//刷新
mapView.invalidate();
}
|
poiSearch:(MKPoiInfo)