那要如何在mapView中標出自己的位置呢?要先利用Location以及LocationManager來取得手機目前的座標位置。取得座標位置之後再放入mapView中,請參照以下程式碼:
public class MyLocation extends MapActivity {
private MyLocationOverlay mylayer;
private MapView mapView;
private MapController mapController;
private int intZoomLevel = 0;
private double dLat = 0;
private double dLng = 0;
private GeoPoint geoPoint;
private String mapLable = "";
public static Context context;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.parking_space);
context = getApplicationContext();
Location mLocation = getLocation(this);
dLat = mLocation.getLatitude();//取得現在所在的緯度
dLng = mLocation.getLongitude();//取得現在所在的經度
mapLable = "您的位置";
getMapView();
}
//利用getLocation來取得當前座標
private Location getLocation(Context context) {
LocationManager locMan = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
Location location = locMan.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location == null) {
location = locMan.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
return location;
}
private void getMapView() {
intZoomLevel = 17;
mapView = (MapView) findViewById(R.id.map);
mapController = mapView.getController();
mapView.setSatellite(false);
mapView.setStreetView(true);
geoPoint = new GeoPoint((int) (dLat * 1E6), (int) (dLng * 1E6));//將剛剛取得的座標置入geoPoint
mapView.displayZoomControls(true);
mapController.animateTo(geoPoint);//將map的中心點移到自己所在的位置
mapController.setZoom(intZoomLevel);//設定地圖級距
//加入地圖標籤
MapOverlay mapOverlay = new MapOverlay();
List
listOfOverlays.clear();
listOfOverlays.add(mapOverlay);
}
public class MapOverlay extends Overlay {
@Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow,long when) {
super.draw(canvas, mapView, shadow);
Projection projection = mapView.getProjection();
Point screenPts = new Point();
//將geoPoint轉化成Point
projection.toPixels(geoPoint, screenPts);
Paint paint = new Paint();
paint.setColor(R.color.BLACK);
//設定文字大小
paint.setTextSize(20);
paint.setAntiAlias(true);
paint.setFakeBoldText(true);
Bitmap bmp = BitmapFactory.decodeResouce(getResource().R.drawable.pin);
//將大頭針放入地圖中,並標示出自己的位置
canvas.drawBitmap(screenPts.x, screenPts.y, null);
canvas.drawText(mapLable, screenPts.x - 50, screenPts.y-50, paint);return true;
}
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
別忘了在設定檔中加入以下權限:
<application .........>
<activity........>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<uses-library android:name="com.google.android.maps"/>
</application>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
其實這篇文章除了getLocation,以及用了一點重構的技巧,其餘幾乎跟另外一篇一模一樣囉!但是如果能好好熟悉這些mathod,可以寫出很多很實用的功能喔!
你好!!
ReplyDelete最近在開發有關 google map app
想請問你如果要在map上
加一點動畫效果,如從天而降的大頭針
我在IPHONE上有看過
該如改寫呢
我在google map api上看很久
都找不到= =
在麻煩你解答了
感激~
謝謝
我也不會....XD
Delete