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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
| package com.example.baidumaptest;
import java.util.ArrayList; import java.util.List; import java.util.Random;
import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.Button; import android.widget.Toast;
import com.baidu.mapapi.SDKInitializer; import com.baidu.mapapi.map.BaiduMap; import com.baidu.mapapi.map.BitmapDescriptor; import com.baidu.mapapi.map.BitmapDescriptorFactory; import com.baidu.mapapi.map.Gradient; import com.baidu.mapapi.map.GroundOverlayOptions; import com.baidu.mapapi.map.HeatMap; import com.baidu.mapapi.map.InfoWindow; import com.baidu.mapapi.map.MapView; import com.baidu.mapapi.map.Marker; import com.baidu.mapapi.map.MarkerOptions; import com.baidu.mapapi.map.OverlayOptions; import com.baidu.mapapi.map.PolygonOptions; import com.baidu.mapapi.map.Stroke; import com.baidu.mapapi.map.TextOptions; import com.baidu.mapapi.model.LatLng; import com.baidu.mapapi.model.LatLngBounds;
public class MainActivity extends Activity {
MapView mMapView = null; BaiduMap mBaiduMap = null; Marker marker;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); SDKInitializer.initialize(getApplicationContext()); setContentView(R.layout.activity_main); mMapView = (MapView) findViewById(R.id.bmapView); mBaiduMap = mMapView.getMap(); }
@Override protected void onDestroy() { super.onDestroy(); mMapView.onDestroy(); }
@Override protected void onPause() { super.onPause(); mMapView.onPause(); }
@Override protected void onResume() { super.onResume(); mMapView.onResume(); }
@Override public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu); return true; }
@Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.map_type_satellite: mBaiduMap.setMapType(BaiduMap.MAP_TYPE_SATELLITE); break; case R.id.set_traffic_enable: mBaiduMap.setTrafficEnabled(true); break; case R.id.set_baiduheatmap_enable: mBaiduMap.setBaiduHeatMapEnabled(true); break; case R.id.marker_reset: if (marker != null) { marker.remove(); } LatLng point = new LatLng(39.963175, 116.400244); BitmapDescriptor bitmap = BitmapDescriptorFactory .fromResource(R.drawable.icon_marka); OverlayOptions option = new MarkerOptions().position(point) .icon(bitmap) .zIndex(9) .draggable(true); marker = (Marker) mBaiduMap.addOverlay(option);
mBaiduMap .setOnMarkerClickListener(new BaiduMap.OnMarkerClickListener() {
@Override public boolean onMarkerClick(Marker marker) { showPopOptions(marker.getPosition()); return false; } });
mBaiduMap .setOnMarkerDragListener(new BaiduMap.OnMarkerDragListener() {
@Override public void onMarkerDragStart(Marker marker) { System.out.println("--拖拽开始--"); Toast.makeText(getApplicationContext(), "拖拽开始", Toast.LENGTH_LONG).show(); }
@Override public void onMarkerDragEnd(Marker marker) { System.out.println("--拖拽结束--");
}
@Override public void onMarkerDrag(Marker marker) { System.out.println("--拖拽中--"); } }); break; case R.id.marker_clear: marker.remove(); break; case R.id.polygon_options: LatLng pt1 = new LatLng(39.93923, 116.357428); LatLng pt2 = new LatLng(39.91923, 116.327428); LatLng pt3 = new LatLng(39.89923, 116.347428); LatLng pt4 = new LatLng(39.89923, 116.367428); LatLng pt5 = new LatLng(39.91923, 116.387428); List<LatLng> pts = new ArrayList<LatLng>(); pts.add(pt1); pts.add(pt2); pts.add(pt3); pts.add(pt4); pts.add(pt5); OverlayOptions polygonOptions = new PolygonOptions().points(pts) .stroke(new Stroke(5, 0xAA00FF00)).fillColor(0xAAFF0000); mBaiduMap.addOverlay(polygonOptions); break; case R.id.text_options: LatLng textLatLng = new LatLng(39.86923, 116.397428); OverlayOptions textOptions = new TextOptions().bgColor(0xAAFFFF00) .text("TREIZE到此一游").fontColor(0xFFFF0000).fontSize(30) .position(textLatLng).rotate(-45); mBaiduMap.addOverlay(textOptions); break; case R.id.pop_options: LatLng pt = new LatLng(39.86923, 116.397428); showPopOptions(pt); break; case R.id.groud_overlay: LatLng southwest = new LatLng(39.92235, 116.380338); LatLng northeast = new LatLng(39.947246, 116.414977); LatLngBounds bounds = new LatLngBounds.Builder().include(northeast) .include(southwest).build(); BitmapDescriptor overlay = BitmapDescriptorFactory .fromResource(R.drawable.ground_overlay); OverlayOptions overlayOptions = new GroundOverlayOptions() .positionFromBounds(bounds).image(overlay) .transparency(0.8f); mBaiduMap.addOverlay(overlayOptions); break; case R.id.baidumap_clear: mBaiduMap.clear(); break;
case R.id.set_self_heatmap: int[] DEFAULT_GRADIENT_COLORS = { Color.rgb(102, 225, 0), Color.rgb(255, 0, 0) }; float[] DEFAULT_GRADIENT_START_POINTS = { 0.2f, 1f }; Gradient gradient = new Gradient(DEFAULT_GRADIENT_COLORS, DEFAULT_GRADIENT_START_POINTS); List<LatLng> randomLatLngs = new ArrayList<LatLng>(); Random random = new Random(); for (int i = 0; i < 500; i++) { int rlat = random.nextInt(370000); int rlng = random.nextInt(370000); int lat = 39780000 + rlat; int lng = 116220000 + rlng; LatLng latLng = new LatLng(lat / 1E6, lng / 1E6); randomLatLngs.add(latLng); } HeatMap heatMap = new HeatMap.Builder().data(randomLatLngs) .gradient(gradient).build(); mBaiduMap.addHeatMap(heatMap); break; } return super.onOptionsItemSelected(item); }
public void showPopOptions(LatLng pt) { Button button = new Button(getApplicationContext()); button.setText("弹出窗"); button.setTextColor(0xFFFF0000); button.setBackgroundResource(R.drawable.popup); InfoWindow mInfoWindow = new InfoWindow(button, pt, -47); mBaiduMap.showInfoWindow(mInfoWindow);
} }
|