android定位和地圖開發實例

在android開發中地圖和定位是很多軟件不可或缺的內容,這些特色功能也給人們帶來了很多方便。

首先介紹一下地圖包中的主要類:

MapController : 主要控制地圖移動,伸縮,以某個GPS座標爲中心,控制MapView中的view組件,管理Overlay,提供View的基本功能。使用多種地圖模式(地圖模式(某些城市可實時對交通狀況進行更新),衛星模式,街景模式)來查看Google Map常用方法:animateTo(GeoPoint point) setCenter(GeoPoint point) setZoom(int zoomLevel) 等。

Mapview : 是用來顯示地圖的view, 它派生自android.view.ViewGroup。當MapView獲得焦點,可以控制地圖的移動和縮放。地圖可以以不同的形式來顯示出來,如街景模式,衛星模式等,通過setSatellite(boolean) setTraffic(boolean), setStreetView(boolean) 方法。

Overlay是覆蓋到MapView的最上層,可以擴展其ondraw接口,自定義在MapView中顯示一些自己的東西。MapView通過MapView.getOverlays()Overlay進行管理。

ProjectionMapViewGPS座標與設備座標的轉換(GeoPointPoint)。

定位系統包中的主要類:

LocationManager:本類提供訪問定位服務的功能,也提供獲取最佳定位提供者的功能。另外,臨近警報功能也可以藉助該類來實現。

LocationProvider:該類是定位提供者的抽象類。定位提供者具備週期性報告設備地理位置的功能。

LocationListener:提供定位信息發生改變時的回調功能。必須事先在定位管理器中註冊監聽器對象。

Criteria:該類使得應用能夠通過在LocationProvider中設置的屬性來選擇合適的定位提供者。

Geocoder:用於處理地理編碼和反向地理編碼的類。地理編碼是指將地址或其他描述轉變爲經度和緯度,反向地理編碼則是將經度和緯度轉變爲地址或描述語言,其中包含了兩個構造函數,需要傳入經度和緯度的座標。getFromLocation方法可以得到一組關於地址的數組。


下面開始地圖定位實例的開發,在開發地圖前需要 獲取Android地圖 API密鑰 網上有很多資料,這裏就不再複述。

首先要在manifest.xml中設置全相應的權限和maps庫:

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name">
<activity
android:label="@string/app_name"
android:name=".MyMapActivity">
<intent-filter>
<actionandroid:name="android.intent.action.MAIN"/>
<categoryandroid:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<spanstyle="color:#FF6666;">
<uses-libraryandroid:name="com.google.android.maps"/></span>
</application>
<spanstyle="color:#FF6666;"> <uses-permissionandroid:name="android.permission.INTERNET"/>
<uses-permissionandroid:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permissionandroid:name="android.permission.ACCESS_COARSE_LOCATION"/></span>


在上面我標紅的千萬不要忘記。

layout下的main.xml:


<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<com.google.android.maps.MapView
android:id="@+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:apiKey="008uu0x2a7GWlK2LzCW872afBAPLhJ-U2R26Wgw"
/>
</LinearLayout>

下面是核心代碼,重要的地方我做了註釋:

publicclassMyMapActivity extendsMapActivity {
/** Called when the activity is first created. */
privateMapController mapController;
privateMapView mapView;
privateMyOverLay myOverLay;
@Override
publicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LocationManager locationManager=(LocationManager) getSystemService(Context.LOCATION_SERVICE);
mapView=(MapView) this.findViewById(R.id.mapview);
//設置交通模式
mapView.setTraffic(true);
//設置衛星模式
mapView.setSatellite(false);
//設置街景模式
mapView.setStreetView(false);
//設置縮放控制
mapView.setBuiltInZoomControls(true);
mapView.setClickable(true);
mapView.setEnabled(true);
//得到MapController實例
mapController=mapView.getController();
mapController.setZoom(15);
myOverLay=newMyOverLay();
List<Overlay> overLays=mapView.getOverlays();
overLays.add(myOverLay);
Criteria criteria=newCriteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(false);
criteria.setPowerRequirement(Criteria.POWER_LOW);
//取得效果最好的Criteria
String provider=locationManager.getBestProvider(criteria, true);
//得到Location
Location location=locationManager.getLastKnownLocation(provider);
updateWithLocation(location);
//註冊一個週期性的更新,3秒一次
locationManager.requestLocationUpdates(provider, 3000, 0, locationListener);
}
@Override
publicbooleanonCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
menu.add(0, 1, 1, "交通模式");
menu.add(0,2,2,"衛星模式");
menu.add(0,3,3,"街景模式");
returnsuper.onCreateOptionsMenu(menu);
}
@Override
publicbooleanonOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
super.onOptionsItemSelected(item);
switch(item.getItemId()) {
case1://交通模式
mapView.setTraffic(true);
mapView.setSatellite(false);
mapView.setStreetView(false);
break;
case2://衛星模式
mapView.setSatellite(true);
mapView.setStreetView(false);
mapView.setTraffic(false);
break;
case3://街景模式
mapView.setStreetView(true);
mapView.setTraffic(false);
mapView.setSatellite(false);
break;
default:
mapView.setTraffic(true);
mapView.setSatellite(false);
mapView.setStreetView(false);
break;
}
returntrue;
}
privatevoidupdateWithLocation(Location location){
if(location!=null){
//爲繪製類設置座標
myOverLay.setLocation(location);
GeoPoint geoPoint=newGeoPoint((int)(location.getLatitude()*1E6), (int)(location.getLongitude()*1E6));
//定位到指定的座標
mapController.animateTo(geoPoint);
mapController.setZoom(15);
}
}
privatefinalLocationListener locationListener=newLocationListener() {
@Override
publicvoidonStatusChanged(String provider, intstatus, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
publicvoidonProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
publicvoidonProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
//當座標改變時出發此函數
@Override
publicvoidonLocationChanged(Location location) {
// TODO Auto-generated method stub
updateWithLocation(location);
}
};
classMyOverLay extendsOverlay{
privateLocation location;
publicvoidsetLocation(Location location){
this.location=location;
}
@Override
publicbooleandraw(Canvas canvas, MapView mapView, booleanshadow,
longwhen) {
// TODO Auto-generated method stub
super.draw(canvas, mapView, shadow);
Paint paint=newPaint();
Point myScreen=newPoint();
//將經緯度換成實際屏幕的座標。
GeoPoint geoPoint=newGeoPoint((int)(location.getLatitude()*1E6), (int)(location.getLongitude()*1E6));
mapView.getProjection().toPixels(geoPoint, myScreen);
paint.setStrokeWidth(1);
paint.setARGB(255, 255, 0, 0);
paint.setStyle(Paint.Style.STROKE);
Bitmap bmp=BitmapFactory.decodeResource(getResources(), R.drawable.mypicture);
//把這張圖片畫到相應的位置。
canvas.drawBitmap(bmp, myScreen.x, myScreen.y,paint);
canvas.drawText("天堂沒有路", myScreen.x, myScreen.y, paint);
returntrue;
}
}
@Override
protectedbooleanisRouteDisplayed() {
// TODO Auto-generated method stub
returnfalse;
}
@Override
publicbooleanonKeyDown(intkeyCode, KeyEvent event) {
// TODO Auto-generated method stub
if(keyCode == KeyEvent.KEYCODE_BACK) {
AlertDialog.Builder builder = newAlertDialog.Builder(this);
builder.setMessage("你確定退出嗎?")
.setCancelable(false)
.setPositiveButton("確定",
newDialogInterface.OnClickListener() {
publicvoidonClick(DialogInterface dialog,
intid) {
MyMapActivity.this.finish();
android.os.Process
.killProcess(android.os.Process
.myPid());
android.os.Process.killProcess(android.os.Process.myTid());
android.os.Process.killProcess(android.os.Process.myUid());
}
})
.setNegativeButton("返回",
newDialogInterface.OnClickListener() {
publicvoidonClick(DialogInterface dialog,
intid) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
returntrue;
}
returnsuper.onKeyDown(keyCode, event);
}
}



接下來看一下運行後效果:

android定位和地圖開發實例

可以放大縮小:

android定位和地圖開發實例

可是使用menu鍵,切換不同的模式:

android定位和地圖開發實例

上面是切換到了衛星模式。由於地圖需要耗費大量的網絡資源,如果網絡比較慢的話會等待很長時間。

轉自:http://blog.csdn.net/wangkuifeng0118/article/details/7244364

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