1 /*
2 *1.添加覆盖物及其图片预览显示
3 *
4 * */
5 public class MainActivity extends Activity {
6 MapView mapView;
7 BaiduMap mBaiduMap;
8
9 // 覆盖物相关
10 private BitmapDescriptor mMarker;
11 private RelativeLayout markly;
12
13 @Override
14 protected void onCreate(Bundle savedInstanceState) {
15 super.onCreate(savedInstanceState);
16 SDKInitializer.initialize(getApplicationContext());
17 requestWindowFeature(Window.FEATURE_NO_TITLE);
18 setContentView(R.layout.fragment_main);
19 mapView = (MapView) findViewById(R.id.bmapView);
20 mBaiduMap = mapView.getMap();
21 // 设置地图标尺500m
22 MapStatusUpdate msu = MapStatusUpdateFactory.zoomTo(15.0f);
23 mBaiduMap.setMapStatus(msu);
24
25 // 覆盖物
26 mMarker = BitmapDescriptorFactory.fromResource(R.drawable.maker);
27 markly = (RelativeLayout) findViewById(R.id.mark);
28 mBaiduMap.setOnMarkerClickListener(new OnMarkerClickListener() {
29
30 @Override
31 public boolean onMarkerClick(Marker arg0) {
32 Bundle exinfo = arg0.getExtraInfo();
33 Info info = (Info) exinfo.getSerializable("info");
34
35 ImageView image = (ImageView) markly.findViewById(R.id.info);
36 TextView distance = (TextView) markly
37 .findViewById(R.id.infodistance);
38 TextView name = (TextView) markly.findViewById(R.id.infoname);
39 TextView zan = (TextView) markly.findViewById(R.id.infozan);
40
41 image.setImageResource(info.getImgId());
42 distance.setText(info.getDistance());
43 name.setText(info.getName());
44 zan.setText(info.getZan() + "");
45
46 InfoWindow infoWindow;
47 TextView tv = new TextView(MainActivity.this);
48 tv.setBackgroundResource(R.drawable.location_tips);
49 tv.setPadding(30, 20, 30, 50);
50 tv.setText(info.getName());
51 tv.setTextColor(Color.parseColor("#ffffff"));
52 final LatLng latLng = arg0.getPosition();
53 Point point = mBaiduMap.getProjection()
54 .toScreenLocation(latLng);
55 point.y -= 47;
56 LatLng ll = mBaiduMap.getProjection().fromScreenLocation(point);
57
58 infoWindow = new InfoWindow(tv, ll, 47);
59 mBaiduMap.showInfoWindow(infoWindow);
60 markly.setVisibility(View.VISIBLE);
61 return true;
62 }
63 });
64
65 mBaiduMap.setOnMapClickListener(new OnMapClickListener() {
66
67 @Override
68 public boolean onMapPoiClick(MapPoi arg0) {
69 // TODO Auto-generated method stub
70 return false;
71 }
72
73 @Override
74 public void onMapClick(LatLng arg0) {
75 markly.setVisibility(View.GONE);
76 mBaiduMap.hideInfoWindow();
77 }
78 });
79
80 }
81
82 public void click(View view) {
83 switch (view.getId()) {
84
85 case R.id.add_overlay:
86 addOverlays(Info.infos);
87
88 break;
89 }
90 }
91
92 // 添加覆盖物
93 private void addOverlays(List<Info> infos) {
94 mBaiduMap.clear();
95 LatLng latLng = null;
96 Marker marker = null;
97 OverlayOptions options;
98 for (Info info : infos) {
99 // 经纬度
100 latLng = new LatLng(info.getLatitude(), info.getLongitude());
101 // 图标
102 options = new MarkerOptions().position(latLng).icon(mMarker)
103 .zIndex(5);
104 marker = (Marker) mBaiduMap.addOverlay(options);
105
106 Bundle arg0 = new Bundle();
107 arg0.putSerializable("info", info);
108 marker.setExtraInfo(arg0);
109 }
110 MapStatusUpdate msu = MapStatusUpdateFactory.newLatLng(latLng);
111 mBaiduMap.setMapStatus(msu);
112 }
113
114 @Override
115 protected void onResume() {
116
117 super.onResume();
118 mapView.onResume();
119 }
120
121 @Override
122 protected void onPause() {
123 // TODO Auto-generated method stub
124 super.onPause();
125 mapView.onPause();
126 }
127
128 @Override
129 protected void onDestroy() {
130 // TODO Auto-generated method stub
131 super.onDestroy();
132 mapView.onDestroy();
133 }
134
135 }
//数据源
1 public class Info implements Serializable{
2
3 private double latitude;
4 private double longitude;
5 private int imgId;
6 private String name;
7 private String distance;
8 private int zan;
9 public static List<Info>infos=new ArrayList<Info>();
10 static
11 {
12 infos.add(new Info(34.242652, 108.971171, R.drawable.a01, "英伦贵族小旅馆",
13 "距离209米", 1456));
14 infos.add(new Info(34.242952, 108.972171, R.drawable.a02, "沙井国际洗浴会所",
15 "距离897米", 456));
16 infos.add(new Info(34.242852, 108.973171, R.drawable.a03, "五环服装城",
17 "距离249米", 1456));
18 infos.add(new Info(34.242152, 108.971971, R.drawable.a04, "老米家泡馍小炒",
19 "距离679米", 1456));
20 }
21 public Info(double latitude, double longitude, int imgId, String name,
22 String distance, int zan) {
23
24 this.latitude = latitude;
25 this.longitude = longitude;
26 this.imgId = imgId;
27 this.name = name;
28 this.distance = distance;
29 this.zan = zan;
30 }
31 public double getLatitude() {
32 return latitude;
33 }
34 public void setLatitude(double latitude) {
35 this.latitude = latitude;
36 }
37 public double getLongitude() {
38 return longitude;
39 }
40 public void setLongitude(double longitude) {
41 this.longitude = longitude;
42 }
43 public int getImgId() {
44 return imgId;
45 }
46 public void setImgId(int imgId) {
47 this.imgId = imgId;
48 }
49 public String getName() {
50 return name;
51 }
52 public void setName(String name) {
53 this.name = name;
54 }
55 public String getDistance() {
56 return distance;
57 }
58 public void setDistance(String distance) {
59 this.distance = distance;
60 }
61 public int getZan() {
62 return zan;
63 }
64 public void setZan(int zan) {
65 this.zan = zan;
66 }
67 }