Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> 百度地圖簡單的使用,百度地圖簡單使用

百度地圖簡單的使用,百度地圖簡單使用

編輯:關於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 List<CarInfo> carInfos = new ArrayList<CarInfo>();
    /**定位相關*/
    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();
    }
}

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved