編輯:關於Android編程
最近寫了百度地圖的Demo,所以總結下遇到的問題;
1.使用百度地圖,先看下官方的例子。要再清單文件中配置KEY,如果用到定位要注冊serviece:android:name="com.baidu.location.f"
2.要創建application類並在清單文件中配置;不然一直報java.lang.IllegalStateException: you have not supplyed the global app context info from SDKInitializer.initialize(Context) function. SDKInitializer.initialize(this)即可;
3.還有注意MapView的生命周期,記得及時創建和銷毀。
4.在定位當前時,獲取當前的地名使用option.setIsNeedAddress(true);//設置地址信息;原先的option.setAddrType("all")過期不過也能獲得信息。
5.在使用Marker時,marker.getExtraInfo()一直報空指針的問題;報空是因為在加入Marker是沒有將bundle賦予到每個Marker,當然會得null了;
需要在OverlayOptions中添加extraInfo(bundle)字段。
6.
如下是我demo的截圖
先定位自己,再在地圖上展示6個Marker,點擊每個Marker時展示出當前Marker的信息
如下是主要代碼
package com.it.hello.activity.baidu; import java.util.ArrayList; import java.util.List; import com.baidu.location.BDLocation; import com.baidu.location.BDLocationListener; import com.baidu.location.LocationClient; import com.baidu.location.LocationClientOption; import com.baidu.mapapi.map.BaiduMap; import com.baidu.mapapi.map.BaiduMap.OnMarkerClickListener; import com.baidu.mapapi.map.BaiduMap.OnMyLocationClickListener; import com.baidu.mapapi.map.BitmapDescriptor; import com.baidu.mapapi.map.BitmapDescriptorFactory; import com.baidu.mapapi.map.InfoWindow; import com.baidu.mapapi.map.MapStatus; import com.baidu.mapapi.map.MapStatusUpdateFactory; import com.baidu.mapapi.map.MapView; import com.baidu.mapapi.map.Marker; import com.baidu.mapapi.map.MarkerOptions; import com.baidu.mapapi.map.MyLocationData; import com.baidu.mapapi.map.InfoWindow.OnInfoWindowClickListener; import com.baidu.mapapi.map.MarkerOptions.MarkerAnimateType; import com.baidu.mapapi.map.OverlayOptions; import com.baidu.mapapi.model.LatLng; import com.it.hello.R; import com.it.hello.activity.entity.CarInfo; import com.it.hello.activity.util.DensityUtils; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.TextView; /** * 百度Marker的問題 * @author zh * */ public class MyBaidu extends Activity{ private MapView mMapView; private BaiduMap mBaiduMap; /**清除Marker*/ private Button btn_clean; /**地圖上Marker的信息*/ private ListcarInfos = new ArrayList (); /**定位相關*/ private LocationClient mLocClient; /**定位當前的位置名稱*/ private String nowPosition = ""; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_baidu); mMapView = (MapView)findViewById(R.id.baidu_mapView); btn_clean =(Button)findViewById(R.id.baidu_clean); mBaiduMap = mMapView.getMap(); // MapStatusUpdate msu = MapStatusUpdateFactory.zoomTo(12.0f); // mBaiduMap.setMapStatus(msu); // 開啟定位圖層 mBaiduMap.setMyLocationEnabled(true); // 定位初始化 mLocClient = new LocationClient(this); mLocClient.registerLocationListener(new MyLocationListenner()); LocationClientOption option = new LocationClientOption(); option.setOpenGps(true);// 打開GPS option.setCoorType("bd09ll");//設置坐標類型 option.setScanSpan(1000);//設置掃描間隔,單位是毫秒 當<1000(1s)時,定時定位無效 //option.setAddrType("all"); option.setIsNeedAddress(true);//設置地址信息,默認無地址信息 mLocClient.setLocOption(option); mLocClient.start(); /** * 地圖定位圖標點擊事件監聽接口 */ mBaiduMap.setOnMyLocationClickListener(new OnMyLocationClickListener() { @Override public boolean onMyLocationClick() { // TODO Auto-generated method stub //吐司當前位置信息 DensityUtils.showToast(MyBaidu.this, nowPosition) ; return false; } }); initOverlay(); /** * Marker點擊事件的監聽接口 */ mBaiduMap.setOnMarkerClickListener(new OnMarkerClickListener() { @Override public boolean onMarkerClick(Marker marker) { // TODO Auto-generated method stub TextView textView = new TextView(getApplicationContext()); textView.setPadding(15, 10, 15, 30); textView.setBackgroundResource(R.drawable.popup); OnInfoWindowClickListener listener = null; System.out.println(marker.getExtraInfo() + " d " + marker); final CarInfo carInfo = (CarInfo)marker.getExtraInfo().get("STATION"); textView.setText("車牌號:" +carInfo.getLicenseNumber()); textView.setTextColor(getResources().getColor(R.color.action_bar_bg_select)); listener = new OnInfoWindowClickListener() { @Override public void onInfoWindowClick() { // TODO Auto-generated method stub System.out.println(carInfo + carInfo.getLatitude() + " " + carInfo.getLongitude()); mBaiduMap.hideInfoWindow(); } }; LatLng ll = marker.getPosition(); InfoWindow mInfoWindow = new InfoWindow(BitmapDescriptorFactory.fromView(textView), ll, -47, listener); mBaiduMap.showInfoWindow(mInfoWindow); return true; } }); /** * 清除Marker,單擊事件的接口 */ btn_clean.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub mBaiduMap.clear(); carInfos.clear(); } }); } /** * 定位SDK監聽函數 * */ boolean isFirstLoc = true; // 是否首次定位 private class MyLocationListenner implements BDLocationListener { @Override public void onReceiveLocation(BDLocation location) { // TODO Auto-generated method stub if (location == null || mMapView == null) { return; } MyLocationData locData = new MyLocationData.Builder() .accuracy(location.getRadius()) // 此處設置開發者獲取到的方向信息,順時針0-360 .direction(100).latitude(location.getLatitude()) .longitude(location.getLongitude()).build(); mBaiduMap.setMyLocationData(locData); if (isFirstLoc) { isFirstLoc = false; LatLng ll = new LatLng(location.getLatitude(), location.getLongitude()); Log.d("jiejie", "Latitude" + location.getLatitude() + " Longitude" + location.getLongitude() + " location" + location.getCity() + location.getAddrStr() ); //tv_location.setText(ll + "詳細地點:" +location.getAddrStr() + " 城市:"+location.getCity()); nowPosition = location.getAddrStr(); MapStatus.Builder builder = new MapStatus.Builder(); builder.target(ll).zoom(12.0f); mBaiduMap.animateMapStatus(MapStatusUpdateFactory .newMapStatus(builder.build())); } } } /** * 添加mark */ private BitmapDescriptor bdA = BitmapDescriptorFactory .fromResource(R.drawable.icon_marka); private Bundle bundle; private void initOverlay() { // TODO Auto-generated method stub //加載的數據 carInfos.add(new CarInfo("無報警", "1", "39.939723", "116.425541", "皖A111", "9208000", "11", "Acc關閉空車")); carInfos.add(new CarInfo("無報警", "2", "39.942821", "116.369199", "皖A222", "9208000", "12", "Acc關閉空車")); carInfos.add(new CarInfo("無報警", "3", "31.843025", "117.275366", "皖N333", "9208000", "13", "ACC關閉空車")); carInfos.add(new CarInfo("無報警", "4", "31.803025", "117.255366", "皖N444", "9208000", "14", "ACC關閉空車")); carInfos.add(new CarInfo("有報警", "5", "30.82943", "116.89586", "皖WA555", "9208000", "15", "Acc不關閉")); carInfos.add(new CarInfo("有報警", "6", "31.81943", "117.134623", "皖WA666", "9208000", "16", "Acc不關閉")); for(CarInfo info : carInfos){ LatLng llLng = new LatLng(Float.parseFloat(info.getLatitude()),Float.parseFloat(info.getLongitude())); bundle = new Bundle(); bundle.putSerializable("STATION", info); // MarkerOptions ooA = new MarkerOptions().position(llLng).icon(bdA).extraInfo(bundle) // .zIndex(9).draggable(true); // ooA.animateType(MarkerAnimateType.grow); OverlayOptions options = new MarkerOptions().position(llLng).icon(bdA).extraInfo(bundle) .zIndex(9).title(info.getLicenseNumber()).draggable(true).animateType(MarkerAnimateType.grow); mBaiduMap.addOverlay(options); } } @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() { // 退出時銷毀定位 mLocClient.stop(); // 關閉定位圖層 mBaiduMap.setMyLocationEnabled(false); // MapView的生命周期與Activity同步,當activity銷毀時需調用MapView.destroy() mMapView.onDestroy(); mMapView = null; super.onDestroy(); // 回收 bitmap 資源 bdA.recycle(); } }
實現ListView過濾功能最方便的便是使用ArrayAdapter,裡面自帶的getFilter()方法能很方便的實現此功能但是在實際的開發中,ArrayAdapte
最近有幾位朋友給我留言,讓我談一下對Activity啟動模式的理解。我覺得對某個知識點的理解必須要動手操作才能印象深刻,所以今天寫一篇博文,結合案例理解Activity啟
布局文件復制代碼 代碼如下:<RelativeLayout xmlns:android=http://schemas.android.com/apk/res/and
在android中使用百度地圖,我們可以先看看百度地圖相應的SDK信息:http://developer.baidu.com/map/index.php?title=an