2012-10-14 34 views
3
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; 
     } 
} 

}`哪能默认位置设置为我的地方显示的MapView

+0

这里问题问:http://stackoverflow.com/questions/8380403/android-map-zoom-level-for-current-location-and-destination和hepful资讯:HTTP:// stackoverflow.com/a/8380743/597849 – Turnsole

回答

0

默认分配你的地方Latitudelongitude,并检查它可能工作

+0

你的地点如何经纬度默认和检查它可能工作 –

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!"); 
     } 
相关问题