» 首頁 » 討論區 » Android程式設計 »Google Map中如何顯示現在速度

Google Map中如何顯示現在速度

發表人: sebastina
積分: 6
發表時間: 2011-08-20 16:36:50
不好意思小弟是Android新手,以下程式碼是已經可以顯示目前所在位置並標籤,我想要在目前地圖上標出速度請問要如何實現呢




import java.util.Iterator;
import java.util.List;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.LinearLayout;
import android.widget.ZoomControls;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

public class MapApiExample03 extends MapActivity {

private LocationManager locationManager;
private LocationListener locationListener;

LinearLayout linearLayout;
MapView mapView;
ZoomControls mZoom;
Drawable mdrawable;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);


linearLayout = (LinearLayout) findViewById(R.id.zoomView);
mapView = (MapView) findViewById(R.id.mapView);
mZoom = (ZoomControls) mapView.getZoomControls();
linearLayout.addView(mZoom);

mdrawable = this.getResources().getDrawable(R.drawable.icon);

GeoPoint addPoint = new GeoPoint(24968134,121195464);
mapView.getController().setCenter(addPoint);
UpdateLocation();
}

public void UpdateLocation()
{
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

locationListener = new LocationListener() {

public void onLocationChanged(Location newLocation) {
UpdateNewLocation(newLocation);

}

public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub

}

public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub

}

public void onStatusChanged(String provider, int status,
Bundle extras) {
// TODO Auto-generated method stub

}
};
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}

void UpdateNewLocation(Location newLocation) {
List overlays = mapView.getOverlays();

// 移除舊的overlay
if ( overlays.size() > 0 ) {
for (Iterator iterator = overlays.iterator(); iterator.hasNext();) {
iterator.next();
iterator.remove();
}
}

Double longitude = newLocation.getLongitude() * 1000000;
Double latitude = newLocation.getLatitude() * 1000000;



GeoPoint newPoint = new GeoPoint( longitude.intValue(), latitude.intValue());
Log.i("Location=", "X=" + longitude.intValue() + ", Y=" + latitude.intValue());
MapOverlay overlay = new MapOverlay(mdrawable);
OverlayItem overlayItem = new OverlayItem(newPoint, "", "");
overlay.addOverlayItem(overlayItem);
mapView.getOverlays().add(overlay);
mapView.getController().animateTo(newPoint);
mapView.postInvalidate();
}

@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}