當我和世界不一樣 那就讓我不一樣

step 1 申請key

>cmd

>cd [adk資料夾底下] ex:C:\Users\user\.android\

>keytool –list -v –alias androiddebugkey -keystore debug.keystore -storepass android -keypass android

到這個網站把md5指紋認證貼上

https://developers.google.com/android/maps-api-signup?hl=zh-TW

把key給記下來

//綁定電腦 換電腦需重新申請

 

step2 加入MapView

res > layout > [googlemap.xml]

<com.google.android.maps.MapView
android:id="@+id/mapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:apiKey="Your Key"
/>

AndroidMenifest.xml

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

[GooglemapActivity] > 繼承 MapActivity

public class GoogleMap extends MapActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.XXX);
mapView = (MapView) findViewById(R.id.mapView);
mapView.setBuiltInZoomControls(true); // ZoomControls
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}

 

step 3 新增label

>新增一MapOverlay的類別,負責處理地圖標記

>>

com.google.android.maps.ItemizedOverlay類別

import java.util.ArrayList;
import android.graphics.drawable.Drawable;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;

public class MapOverlay extends ItemizedOverlay<OverlayItem>{

private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
public MapOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
}
public void addOverlayItem(OverlayItem oItem){
mOverlays.add(oItem);
populate();
}
@Override
protected OverlayItem createItem(int i) {
return mOverlays.get(i);
}
@Override
public int size() {
return mOverlays.size();
}
}

>>

com.google.android.maps.GeoPoint類別 – 經緯度位置

import com.google.android.maps.GeoPoint;

MapView mapView;
Drawable mydrawable;
//NCU
Double longitude = 24.967215;
Double latitude = 121.191655;

mydrawable = this.getResources().getDrawable(R.drawable.ic_launcher);
MapOverlay mymapoverlay = new MapOverlay(mydrawable);

//新增一位置
GeoPoint newPoint = new GeoPoint((int)(longitude * 1000000), (int)(latitude * 1000000));
OverlayItem myoverlayitem = new OverlayItem(newPoint, "", "");
mymapoverlay.addOverlayItem(myoverlayitem);

mapView.getOverlays().add(mymapoverlay);
mapView.getController().setCenter(newPoint);
mapView.getController().setZoom(16);

創作者介紹
創作者 Klv 的頭像
Klv

值得嗎?

Klv 發表在 痞客邦 留言(1) 人氣()


留言列表 (1)

發表留言
  • 奶油
  • 這啥
  • 不要問 很恐怖

    Klv 於 2012/06/14 16:16 回覆