我正在使用GPS来获取我的位置但这段代码显示我在太平洋的位置,虽然我在中东...
此代码的另一个功能是在我触摸屏幕的位置添加一个图标(标记),根据我的代码,它是否正常工作?!
这是我的代码:
package com.adhamenaya.android;
import java.util.List;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.MotionEvent;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
public class MapApp extends MapActivity {
private MapView mapView;
private MapController mapController;
private LocationManager locationManager;
private GeoPoint p;
List<Overlay> listOfOverlays ;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initLayout();
initMap();
listOfOverlays = mapView.getOverlays();
}
private void initLayout(){
mapView = (MapView) findViewById(R.id.mapview);
}
private void initMap(){
mapView.setBuiltInZoomControls(true);
mapView.setStreetView(true);
mapController=mapView.getController();
mapController.setZoom(10);// 1 is world view
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0, 0, new GeoUpdateHandler());
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
class MapOverlay extends com.google.android.maps.Overlay
{
private GeoPoint p;
private int res;
public MapOverlay(GeoPoint p,int res){
this.p=p;
this.res=res;
}
@Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when)
{
super.draw(canvas, mapView, shadow);
//---translate the GeoPoint to screen pixels---
Point screenPts = new Point();
mapView.getProjection().toPixels(p, screenPts);
//---add the marker---
Bitmap bmp = BitmapFactory.decodeResource(getResources(),res);
canvas.drawBitmap(bmp, screenPts.x, screenPts.y-20, null);
return true;
}
@Override
public boolean onTouchEvent(MotionEvent event, MapView mapView)
{
//---when user lifts his finger---
if (event.getAction() == 1) {
GeoPoint p = mapView.getProjection().fromPixels((int) event.getX(),(int) event.getY());
mapController.animateTo(p);
MapOverlay mapOverlay = new MapOverlay(p,R.drawable.redicon);
listOfOverlays.add(mapOverlay);
mapView.invalidate();
}
return false;
}
}
class GeoUpdateHandler implements LocationListener {
@Override
public void onLocationChanged(Location location) {
int lat=(int)(location.getLatitude()*1E6);
int lng=(int)(location.getLongitude()*1E6);
GeoPoint p=new GeoPoint(lat,lng);
mapController.animateTo(p);
MapOverlay mapOverlay = new MapOverlay(p,R.drawable.blueicon);
listOfOverlays.add(mapOverlay);
mapView.invalidate();
}
@Override
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub
}
}
}