如何在显示mapview时将默认位置设置为我的位置

时间:2012-10-14 20:31:12

标签: android gps

public class MainActivity extends MapActivity {
    LocationManager locmang;
    static  MapController mcon;
    GeoPoint geo;
    static GeoPoint point;
    static  MapView myMap;
    MyLocationOverlay comp;
    static double lat =0;
    static double loni =0;
    private LocationManager locManager;
    private LocationListener locListener;
     List<Overlay>mapOverlays;
     Drawable drawable;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    myMap= (MapView)findViewById(R.id.mapview);
    myMap.displayZoomControls(true);
    myMap.setBuiltInZoomControls(true);
    mcon=myMap.getController();
    myMap.setSatellite(false);
    geo = new GeoPoint((int)(lat*1E6),(int)(loni*1E6));
    mcon.setCenter(geo);
    mcon=myMap.getController();
    mcon.animateTo(geo);
    comp = new MyLocationOverlay(this, myMap);
    myMap.getOverlays().add(comp);
    LocationManager lm =(LocationManager)getSystemService(Context.LOCATION_SERVICE);
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,10, 100, (LocationListener) this);
     mapOverlays = myMap.getOverlays();
     MapOverlay mapOverlay = new MapOverlay();
     List<Overlay> listOfOverlays = myMap.getOverlays();
     listOfOverlays.clear();
     listOfOverlays.add(mapOverlay);  
     myMap.invalidate();
     Intent startService = new Intent(getApplicationContext(),ServiceLocation.class);
     startService(startService);


}
public static void updateMap() {
    if(ServiceLocation.curLocation!=null){
        lat = ServiceLocation.curLocation.getLatitude();
        loni = ServiceLocation.curLocation.getLongitude();        
        if(myMap!=null){
            point = new GeoPoint((int)(lat*1e6),(int)(loni*1e6));
            mcon.animateTo(point);
            myMap.invalidate();
        }
    }
}

public void createAndShowMyItemizedOverlay(Location newLocation) {
    List<Overlay> overlays = myMap.getOverlays();

     if (overlays.size() > 0) {
        for (Iterator<Overlay> iterator = overlays.iterator(); iterator.hasNext();) {
            iterator.next();
            iterator.remove();
        }
    }


    GeoPoint geopoint = new GeoPoint(
            (int) (newLocation.getLatitude() * 1E6), (int) (newLocation
                    .getLongitude() * 1E6));

     Drawable icon = getResources().getDrawable(R.drawable.image19);
    icon.setBounds(0, 0, icon.getIntrinsicWidth(), icon
            .getIntrinsicHeight());

    myMap.getController().animateTo(geopoint);


    myMap.postInvalidate();
}
@Override
protected boolean isRouteDisplayed() {

    return false;
}

public class MapOverlay extends Overlay {
     public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {
            super.draw(canvas, mapView, shadow);                   

            if(!shadow){

                Point screenPts = new Point();
                if(lat==0 && loni==0)
                    mapView.getProjection().toPixels(geo, screenPts);
                else{
                    mapView.getProjection().toPixels(point, screenPts);
                }

                Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.image19);            
                canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);
            }
            return true;
        }
}

}`

2 个答案:

答案 0 :(得分:0)

默认情况下为您的地点Latitudelongitude分配并检查它是否可行

答案 1 :(得分:0)

这对我有用: - )

Context ctx;

double LATITUDE = ; //your static latitude
double LONGITUDE = ; //your static longitude

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_landing);
        ctx = this;

         Geocoder geocoder = new Geocoder(this, Locale.getDefault());
    try {
          List<Address> addresses = geocoder.getFromLocation(LATITUDE, LONGITUDE, 1);
          if(addresses != null) {
               Address returnedAddress = addresses.get(0);
               StringBuilder strReturnedAddress = new StringBuilder();
           for(int i=0; i<returnedAddress.getMaxAddressLineIndex(); i++) {
               strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("\n");
        }
           tvAddress.setText(strReturnedAddress.toString());
        }
          else {
           tvAddress.setText("No Address returned!");
        }
    } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
          tvAddress.setText("Canont get Address!");
        }