Android 集成百度地图实现设备定位
步骤1:
申请android 端SDK :
http://lbsyun.baidu.com/
步骤2:
下载基础版SDK
步骤3:
下载示例程序
步骤4:
开始集成:
ak加入
libs加入
SDKInitializer.setCoordType(CoordType.BD09LL);
图标
类
/** * 演示覆盖物的用法 */public class OverlayDemo extends BaseActivity { /** * MapView 是地图主控件 */ private MapView mMapView; private BaiduMap mBaiduMap; private Marker mMarkerA; private Marker mMarkerB; private Marker mMarkerC; private Marker mMarkerD; private InfoWindow mInfoWindow; private SeekBar alphaSeekBar = null; private CheckBox animationBox = null; Double loc=0.00050; // 初始化全局 bitmap 信息,不用时及时 recycle BitmapDescriptor bdA = BitmapDescriptorFactory .fromResource(R.drawable.icon_marka); BitmapDescriptor bdB = BitmapDescriptorFactory .fromResource(R.drawable.icon_markb); BitmapDescriptor bdC = BitmapDescriptorFactory .fromResource(R.drawable.icon_markc); BitmapDescriptor bdD = BitmapDescriptorFactory .fromResource(R.drawable.icon_markd); BitmapDescriptor bd = BitmapDescriptorFactory .fromResource(R.drawable.icon_gcoding); BitmapDescriptor bdGround = BitmapDescriptorFactory .fromResource(R.drawable.ground_overlay); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_overlay); alphaSeekBar = (SeekBar) findViewById(R.id.alphaBar); alphaSeekBar.setOnSeekBarChangeListener(new SeekBarListener()); animationBox = (CheckBox) findViewById(R.id.animation); mMapView = (MapView) findViewById(R.id.bmapView); mBaiduMap = mMapView.getMap(); MapStatusUpdate msu = MapStatusUpdateFactory.zoomTo(14.0f); mBaiduMap.setMapStatus(msu); initOverlay(); mBaiduMap.setOnMarkerClickListener(new BaiduMap.OnMarkerClickListener() { public boolean onMarkerClick(final Marker marker) { Button button = new Button(getApplicationContext()); button.setBackgroundResource(R.drawable.popup); InfoWindow.OnInfoWindowClickListener listener = null; if (marker == mMarkerA || marker == mMarkerD) { button.setText("更改位置"); button.setTextColor(Color.BLACK); button.setWidth(300); listener = new InfoWindow.OnInfoWindowClickListener() { public void onInfoWindowClick() { LatLng ll = marker.getPosition(); LatLng llNew = new LatLng(ll.latitude 0.005, ll.longitude 0.005); marker.setPosition(llNew); mBaiduMap.hideInfoWindow(); } }; LatLng ll = marker.getPosition(); mInfoWindow = new InfoWindow(BitmapDescriptorFactory.fromView(button), ll, -47, listener); mBaiduMap.showInfoWindow(mInfoWindow); } else if (marker == mMarkerB) { button.setText("更改图标"); button.setTextColor(Color.BLACK); button.setOnClickListener(new OnClickListener() { public void onClick(View v) { marker.setIcon(bd); mBaiduMap.hideInfoWindow(); } }); LatLng ll = marker.getPosition(); mInfoWindow = new InfoWindow(button, ll, -47); mBaiduMap.showInfoWindow(mInfoWindow); } else if (marker == mMarkerC) { button.setText("删除"); button.setTextColor(Color.BLACK); button.setOnClickListener(new OnClickListener() { public void onClick(View v) { marker.remove(); mBaiduMap.hideInfoWindow(); } }); LatLng ll = marker.getPosition(); mInfoWindow = new InfoWindow(button, ll, -47); mBaiduMap.showInfoWindow(mInfoWindow); } return true; } }); } public void initOverlay() { loc=loc 0.0005; // add marker overlay LatLng llA = new LatLng(35.963175 loc, 110.400244 loc); LatLng llB = new LatLng(39.942821, 116.369199); LatLng llC = new LatLng(39.939723, 116.425541); LatLng llD = new LatLng(39.906965, 116.401394); MarkerOptions ooA = new MarkerOptions().position(llA).icon(bdA) .zIndex(9).draggable(true); if (animationBox.isChecked()) { // 掉下动画 ooA.animateType(MarkerOptions.MarkerAnimateType.drop); } mMarkerA = (Marker) (mBaiduMap.addOverlay(ooA)); MarkerOptions ooB = new MarkerOptions().position(llB).icon(bdB) .zIndex(5); if (animationBox.isChecked()) { // 掉下动画 ooB.animateType(MarkerOptions.MarkerAnimateType.drop); } mMarkerB = (Marker) (mBaiduMap.addOverlay(ooB)); MarkerOptions ooC = new MarkerOptions().position(llC).icon(bdC) .perspective(false).anchor(0.5f, 0.5f).rotate(30).zIndex(7); if (animationBox.isChecked()) { // 生长动画 ooC.animateType(MarkerOptions.MarkerAnimateType.grow); } mMarkerC = (Marker) (mBaiduMap.addOverlay(ooC)); ArrayList<BitmapDescriptor> giflist = new ArrayList<BitmapDescriptor>(); giflist.add(bdA); giflist.add(bdB); giflist.add(bdC); MarkerOptions ooD = new MarkerOptions().position(llD).icons(giflist) .zIndex(0).period(10); if (animationBox.isChecked()) { // 生长动画 ooD.animateType(MarkerOptions.MarkerAnimateType.grow); } mMarkerD = (Marker) (mBaiduMap.addOverlay(ooD)); // add ground overlay // LatLng southwest = new LatLng(39.92235, 116.380338); // LatLng northeast = new LatLng(39.947246, 116.414977); LatLng southwest = new LatLng(35.963175, 110.400244); LatLng northeast = new LatLng(35.963170, 110.400240); LatLngBounds bounds = new LatLngBounds.Builder().include(northeast) .include(southwest).build(); OverlayOptions ooGround = new GroundOverlayOptions() .positionFromBounds(bounds).image(bdGround).transparency(0.8f); mBaiduMap.addOverlay(ooGround); MapStatusUpdate u = MapStatusUpdateFactory .newLatLng(bounds.getCenter()); mBaiduMap.setMapStatus(u); mBaiduMap.setOnMarkerDragListener(new BaiduMap.OnMarkerDragListener() { public void onMarkerDrag(Marker marker) { } public void onMarkerDragEnd(Marker marker) { Toast.makeText( OverlayDemo.this, "拖拽结束,新位置:" marker.getPosition().latitude ", " marker.getPosition().longitude, Toast.LENGTH_LONG).show(); } public void onMarkerDragStart(Marker marker) { } }); } /** * 清除所有Overlay * * @param view */ public void clearOverlay(View view) { mBaiduMap.clear(); mMarkerA = null; mMarkerB = null; mMarkerC = null; mMarkerD = null; } /** * 重新添加Overlay * * @param view */ public void resetOverlay(View view) { clearOverlay(null); initOverlay(); } private class SeekBarListener implements SeekBar.OnSeekBarChangeListener { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { // TODO Auto-generated method stub float alpha = ((float) seekBar.getProgress()) / 10; if (mMarkerA != null) { mMarkerA.setAlpha(alpha); } if (mMarkerB != null) { mMarkerB.setAlpha(alpha); } if (mMarkerC != null) { mMarkerC.setAlpha(alpha); } if (mMarkerD != null) { mMarkerD.setAlpha(alpha); } } @Override public void onStartTrackingTouch(SeekBar seekBar) { // TODO Auto-generated method stub } @Override public void onStopTrackingTouch(SeekBar seekBar) { // TODO Auto-generated method stub } } @Override protected void onPause() { // MapView的生命周期与Activity同步,当activity挂起时需调用MapView.onPause() mMapView.onPause(); super.onPause(); } @Override protected void onResume() { // MapView的生命周期与Activity同步,当activity恢复时需调用MapView.onResume() mMapView.onResume(); super.onResume(); } @Override protected void onDestroy() { // MapView的生命周期与Activity同步,当activity销毁时需调用MapView.destroy() mMapView.onDestroy(); super.onDestroy(); // 回收 bitmap 资源 bdA.recycle(); bdB.recycle(); bdC.recycle(); bdD.recycle(); bd.recycle(); bdGround.recycle(); }}
来源:http://www.icode9.com/content-4-76301.html
联系客服