Android地图与当前位置

时间:2011-02-16 08:47:15

标签: android gps

我有以下代码,但我的gps没有获取当前位置,显示触摸的地址,没有叠加。我的纬度和经度为0.0。基本上只有地图出现。非常感谢帮助。谢谢!

  

public class Map扩展了MapActivity {

MapView mapView;
MapController mc;
GeoPoint p;
private LocationManager lm;
private LocationListener locationListener;
Button btn_send;
int lat, lng;


class MapOverlay extends com.google.android.maps.Overlay
{
    @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(), R.drawable.pin);            
        canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, 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());
            Geocoder geoCoder = new Geocoder(
                    getBaseContext(), Locale.getDefault()); 
                try {
                    List<Address> addresses = geoCoder.getFromLocation(
                        p.getLatitudeE6()  / 1E6, 
                        p.getLongitudeE6() / 1E6, 1);

                    String add = "";
                    if (addresses.size() > 0) 
                    {
                        for (int i=0; i<addresses.get(0).getMaxAddressLineIndex(); 
                             i++)
                           add += addresses.get(0).getAddressLine(i) + "\n";
                    }

                    Toast.makeText(getBaseContext(), add, Toast.LENGTH_SHORT).show();
                }
                catch (IOException e) {                
                    e.printStackTrace();
                }   
                return true;
            }
            else                
                return false;
    }
} 


  public boolean onKeyDown(int keyCode, KeyEvent event) 
    {
        MapController mc = mapView.getController(); 
        switch (keyCode) 
        {
            case KeyEvent.KEYCODE_3:
                mc.zoomIn();
                break;
            case KeyEvent.KEYCODE_1:
                mc.zoomOut();
                break;
        }
        return super.onKeyDown(keyCode, event);
    }    


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.map);
    lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
            0, new GeoUpdateHandler());

    Button btnSend = (Button) findViewById(R.id.btn_send);
    btnSend.setOnClickListener(new View.OnClickListener(){

        public void onClick(View v){

                Intent new_intent = new Intent("net.learn2develop.SendSMS");
                startActivity(new_intent);
        }
    });

    mapView = (MapView) findViewById(R.id.mapView);
    LinearLayout zoomLayout = (LinearLayout)findViewById(R.id.zoom);  
    View zoomView = mapView.getZoomControls();

    zoomLayout.addView(zoomView, 
        new LinearLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT, 
            LayoutParams.WRAP_CONTENT)); 
    mapView.displayZoomControls(true);
    mapView.setSatellite(false);
    mapView.setStreetView(true);

    mc = mapView.getController();
    String latitude = Integer.toString(lat);
    String longitude = Integer.toString(lng); 
    String coordinates[] = {latitude, longitude};
    double lati = Double.parseDouble(coordinates[0]);
    double lngi = Double.parseDouble(coordinates[1]);
    System.out.println("lat" + lati + "lng" + lngi );
    p = new GeoPoint(
        (int) (lati * 1E6), 
        (int) (lngi * 1E6));

    mc.animateTo(p);
    mc.setZoom(17); 
    mapView.invalidate();

  //---Add a location marker---
    MapOverlay mapOverlay = new MapOverlay();
    List<Overlay> listOfOverlays = mapView.getOverlays();
    listOfOverlays.clear();
    listOfOverlays.add(mapOverlay);        

    mapView.invalidate();


}


@Override
protected boolean isRouteDisplayed() {
    // TODO Auto-generated method stub
    return false;
}
public class GeoUpdateHandler implements LocationListener {

    @Override
    public void onLocationChanged(Location location) {
        lat = (int) (location.getLatitude() * 1E6);
        lng = (int) (location.getLongitude() * 1E6);
        System.out.println("lat shld b smth " + lat + "lng smth " +lng);
        //GeoPoint point = new GeoPoint(lat, lng);
        //mc.animateTo(point); //   mapController.setCenter(point);
    }

    @Override
    public void onProviderDisabled(String provider) {
    }

    @Override
    public void onProviderEnabled(String provider) {
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
    }
}

}

2 个答案:

答案 0 :(得分:1)

如果要显示用户的当前位置,最好使用MyLocationOverlay。您也可以将其子类化并覆盖onLocationChanged,以插入自定义代码。

通过调用enableMyLocation onResume()和disableMyLocation onPause()来启用它。

答案 1 :(得分:0)

需要2到15分钟才能获得卫星GPS定位。您应该移动代码以将该点呈现为onLocationChanged()