android mapview結合LocationManager應用

應用需求:

 

1、提供地圖功能

 

2、提供三種視圖:衛星模式、地圖模式、我的位置

 

3、支持多種定位方式:gps、wifi等

 

4、根據當前的位置,獲取服務器上地點信息,並標註在當前屏幕上

 

解決方案:

 

1、首先申請google 的 MAP KEY

 

2、在xml文件中引用mapview,如:<com.google.android.maps.MapView
            android:id="@+id/map_View"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:clickable="true"
            android:enabled="true"
            android:apiKey="xxxxxx"//填寫申請到key
            />

3、要使用mapview功能,需要在AndroidManifest.xml中申請權限。

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />,同時需要在Activity中引用uses-library

<uses-library android:name="com.google.android.maps" /> 

 

4、開始進行構建邏輯。首先Activity需要extends MapActivity。 

        MapView map = (MapView)findViewById(R.id.mapview);//獲得MapView對象  
        map.getController().setCenter(new GeoPoint(39971036,116314659));//設置地圖中心  
        map.getController().setZoom(10);

 

以上代碼就可以實現顯示地圖,並以(39971036,116314659)經緯度爲中心顯示地圖。

 

5、通過對mapview的setSatellite屬性進行設置,來達到衛星模式與地圖模式的切換。

     mapview.setSatellite(true);//衛星視圖

     mapview.setSatellite(false);//地圖視圖

 

6、定位當前位置。定義一個LocationListener,實現其中的onLocationChanged方法,在onLocationChanged中實現定位到當前位置。

    public void findWhereYou(Location loc){
        GeoPoint gp = getPoint(loc.getLatitude(), loc.getLongitude());
        OverlayItem item = new OverlayItem(gp, "您的當前位置:緯度="+gp.getLatitudeE6()+",經度="+gp.getLongitudeE6(), "null");
        Drawable marker = getResources().getDrawable(R.drawable.sign);//標記當前位置的圖標
        marker.setBounds(0, 0, marker.getIntrinsicWidth(), marker.getIntrinsicHeight());
        item.setMarker(marker);
        if (mOverlay.size() > 0) {
            mOverlay.removeOverlay(0);
        }
        mOverlay.addOverlay(0, item);//在地圖上繪製座標點時,首先繪製當前座標點。
        mapview.getController().animateTo(gp,new Runnable() {
            public void run() {
                new HandleTask().execute();
            }
        });//在地圖移到當前位置時,通過AsyncTask來獲取服務器端中對應到當前位置附近的座標點
    }

其中mOverla extends 了ItemizedOverlay<OverlayItem>,在android的api這樣來解釋ItemizedOverlay<OverlayItem>:ItemizedOverlay是Overlay的一個基類,包含了一個OverlayItem列表。 從南到北的處理item,用於繪製、創建平移邊界、爲每個點繪製標記點,和維護一個焦點選中的item,同時也負責把一個屏幕點擊匹配到item上去,分 發焦點改變事件給備選的監聽器.

 

      上面說到,定義了一個LocationListener,現在需要把這個位置監聽器傳給LocationManager,才能達到定位的效果。

在定位到我的位置下這樣寫:

           Toast.makeText(this, "正在定位", Toast.LENGTH_LONG).show();
            if(!locUtil.requestLocationUpdates(null, 500, 0,locationlistener)) {
                Toast.makeText(this, "定位失敗", Toast.LENGTH_LONG).show();
            }

其中,locUtil是自己針對locationManager封裝的一個類。

public class LocationUtil {
    private LocationManager locationManager = null;
   
    private List<LocationListener> listenerList = new ArrayList();
   
    public LocationUtil(Context context){
        this.locationManager = ((LocationManager)context.getSystemService("location"));
    }
   
    public boolean requestLocationUpdates(String provider,long minTime,float minDistance,LocationListener listener){
        if(provider == null){
            provider = getProvider(null);
        }
        if(provider != null){
            this.locationManager.requestLocationUpdates(provider, minTime, minDistance, listener);
            this.listenerList.add(listener);
            return true;
        }
        return false;
    }
    public String getProvider(Criteria criteria){
        if(criteria == null){
            criteria = new Criteria();
            criteria.setAccuracy(Criteria.ACCURACY_FINE);
            criteria.setAltitudeRequired(false);
            criteria.setBearingRequired(false);
            criteria.setCostAllowed(true);
        }
        String provider = this.locationManager.getBestProvider(criteria, true);
        Log.i("MAPACTIVITY", "provider is :"+provider);
        return provider;
    }
    public Location getLastKnowLocation(){
        String provider = getProvider(null);
        Location location = locationManager.getLastKnownLocation(provider);
       
        return location;
    }
    public void removeUpdates(LocationListener listener){
        this.locationManager.removeUpdates(listener);
    }
    public void destory(){
        for(LocationListener listener :this.listenerList){
            this.locationManager.removeUpdates(listener);
        }
        this.locationManager = null;
    }
}

 

7、以上6步基本上達到需求了。整個應用用到以下幾個關鍵詞:mapview、locationManager、GeoPoint、ItemizedOverlay.OnFocusChangeListener()、Locationlistener,通過查找這幾個關鍵詞的資料,基本上對於mapview方面的應用沒有多大的問題。

 

8、附帶提下:如果要做地圖方面的應用,需要考慮一點,有些高端機並不支持google map,這個時候就需要考慮第三方地圖提供商了,國內有很多地圖提供商,個人認爲高德的地圖(mapabc)還是不錯的,它的api接口與google的兼容,可以說用同一套代碼可以加載多個地圖。mapabc的優點是:因爲是國內的,所以加載速度上要比google map快,api文檔比較豐富並且是中文簡體的,同時有多個技術討論羣,便於解決一些開發上的問題。還有很重要的一點,google的數據是買它的。  缺點:沒有衛星視圖,國內的地圖一般沒有。有些機型,可能會不顯示地圖,由於4.xxx與5.xxx版本的api名稱都有變化,但是官方資料上並沒有對兩版本進行關聯性解釋,很容易讓人迷惑,有可能是因爲5.xxx版本還是bate版的原因吧。

    說下百度地圖,啓用它的原因是,傳一個圖片過來要上M,並應用程序還大,這個在手機上是不能容忍的。百度地圖進軍移動終端看來還需要下點功夫了。

 

 

 

 

 

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章