Google 地圖標誌
使用google map時,常要在地圖上標上標記,例如:標出現在位置、製作LBS服務時需加入附近景點標誌,皆需用到此功能。然而要使用這項功能,須使用com.google.maps.ItemizedOverlay類別,有了這項功能後,接下來要指定放置的位置,然而此項需要com.google.android.maps.GeoPoint類別來指定放置標誌的經緯度。
前提: 已做好基本的Google Map程式
Step1 首先必須先建立一個自ItemizedOverlay<OverlayItem>擴展的MapOverlay類別,依據系統指示會產生下列程式碼。
MapOverlay.java
package com.sam.test;
import android.graphics.drawable.Drawable;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;
publicclass MapOverlay extends ItemizedOverlay<OverlayItem>{
public MapOverlay(Drawable defaultMarker) {
super(defaultMarker);
// TODO Auto-generated constructor stub
}
@Override
protected OverlayItem createItem(int arg0) {
// TODO Auto-generated method stub
returnnull;
}
@Override
publicint size() {
// TODO Auto-generated method stub
return 0;
}
}
Step2 修改ItemizedOverlay類別,將地標放置進去。
package com.sam.test;
import java.util.ArrayList;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
publicclass MapOverlay extends ItemizedOverlay<OverlayItem>{
private ArrayList<OverlayItem> gList = new ArrayList<OverlayItem>();
Drawable marker;
public MapOverlay(Drawable defaultMarker) {
super(defaultMarker);
// TODO Auto-generated constructor stub
marker = defaultMarker;
}
//In order to add new OverlayItems to the ArrayList, you need a new method:
publicvoid addOverlayItem(OverlayItem oItem){
gList.add(oItem);
populate();
}
/*
Each time you add a new OverlayItem to the ArrayList, you must call
populate() for the ItemizedOverlay, which will read each of the OverlayItems
and prepare them to be drawn
*/
@Override
publicvoid draw(Canvas canvas, MapView mapview, boolean shadow){
super.draw(canvas, mapview, shadow);
boundCenterBottom(marker);
}
/*
When the populate() method executes, it will call createItem(int) in
the ItemizedOverlay to retrieve each OverlayItem. You must override this
method to properly read from the ArrayList and return the OverlayItem from the
position specified by the given integer. Your override method should look like this:
*/
@Override
protected OverlayItem createItem(int arg0) {
// TODO Auto-generated method stub
returngList.get(arg0);
}
/*
You must also override the size() method to return the current number of
items in the ArrayList:
*/
@Override
publicint size() {
// TODO Auto-generated method stub
returngList.size();
}
}
Step3 在主程式碼內用GeoPoint設定放置位置,接著宣告OverlayItem設定myPoint物件為設定的經緯度位置,再將定義好的myoverlayitem加入mymapoverlay中。
GPStest001.java
package com.sam.test;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
import android.widget.ZoomControls;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.LinearLayout;
publicclass GPStest001 extends MapActivity {
/** Called when the activity is first created. */
@Override
publicvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout linearlayout01;
MapView mapview01;
ZoomControls myZoom;
Drawable mydrawable; //new
linearlayout01 = (LinearLayout)findViewById(R.id.zoomview);
mapview01 = (MapView)findViewById(R.id.mapview);
myZoom = (ZoomControls)mapview01.getZoomControls();
linearlayout01.addView(myZoom);
mydrawable = this.getResources().getDrawable(R.drawable.icon);//指定標誌圖示
MapOverlay mymapoverlay = new MapOverlay(mydrawable);
//加入經緯度
GeoPoint addPoint = new GeoPoint(24778289, 120988108); //指定位置
OverlayItem myoverlayitem = new OverlayItem(addPoint, "", "");
mymapoverlay.addOverlayItem(myoverlayitem);
mapview01.getOverlays().add(mymapoverlay);
//依照mypoint設定之經緯度為中心點
mapview01.getController().setCenter(addPoint);
}
@Override
protectedboolean isRouteDisplayed() {
// TODO Auto-generated method stub
returnfalse;
}
}
就可以成功出現此畫面:
Step4 若要改變標誌的圖案,只要將想要用的圖案加到drawable-mdpi資料夾內,再更改一小部分程式碼就可以嚕。
GPStext001.java
mydrawable = this.getResources().getDrawable(R.drawable.icon1);
//改成圖片檔名
可愛吧,小綠人♥
留言列表