2013-02-03 63 views
1

我試圖在地圖中顯示我的當前位置以及地址。下面的代碼工作正常,但不知何故,我無法繪製或圍繞我的當前location.Any幫助將不勝感激。我使用舊的API。無法圍繞我當前在地圖上的位置繪製一個圓圈

這是我的主要課程。

public class GMapsActivity extends MapActivity implements LocationListener { 

    private static final String TAG = "LocationActivity"; 
    private static GMapsActivity instance; 
    private MapView mapView; 
    protected LocationManager locationManager; 
    public Button retrieveLocationButton; 
    Geocoder geocoder; 
    TextView locationText; 
    Location location; 
    MapController mapController; 
    CountDownTimer locationtimer; 

     // private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in Meters 
// private static final long MINIMUM_TIME_BETWEEN_UPDATES = 9000; // in Milliseconds 
// MapOverlay mapOverlay = new MapOverlay(); 
    @Override   
     public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_gmaps); 

     mapView = (MapView) findViewById(R.id.map_view); 
     mapView.setBuiltInZoomControls(true); 
     locationText = (TextView)this.findViewById(R.id.lblLocationInfo); 
     mapController = mapView.getController(); 
     mapController.setZoom(13); 
     retrieveLocationButton = (Button) findViewById(R.id.retrieve_location_button); 

    } 
    public void showcurrentlocation(View view) { 

     geocoder = new Geocoder(GMapsActivity.this); 
     locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
     mapView.getOverlays().add(new CircleOverlay(this, location.getLatitude(),location.getLongitude(),325)); 
     if (location != null) {  
      Log.d(TAG, location.toString()); 
      this.onLocationChanged(location); //<6> 

      } 
     } 



    @Override 
      public void onLocationChanged(Location location) { 
      Log.d(TAG, "onLocationChanged with location " + location.toString()); 
      String text = String.format("Lat:\t %f\nLong:\t %f\nAlt:\t %f\nBearing:\t %f", location.getLatitude(),location.getLongitude(), location.getAltitude(), 
        location.getBearing()); 
      this.locationText.setText(text); 

        try {  
         List<Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 10); //<10> 
         for (Address address : addresses) {  
          this.locationText.append("\n" + address.getAddressLine(0)); 
          }   
         int latitude = (int)(location.getLatitude() * 1000000); 
         int longitude = (int)(location.getLongitude() * 1000000); 
         GeoPoint point = new GeoPoint(latitude,longitude);  
         mapController.animateTo(point); //<11> 


        } 
        catch (IOException e) { 
         Log.e("LocateMe", "Could not get Geocoder data", e); 
         } 

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

        } 

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

        } 

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


        } 


//     @Override protected void onResume() { 
//     LocationListener locationListener = new LocationListener(){ 
//      super.onResume(); 
//     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10, this); //<7> 
//     } 
//    @Override protected void onPause() { 
//     super.onPause(); 
//     locationManager.removeUpdates(this); //<8> 
//      
//     } 


     @Override 
     protected boolean isRouteDisplayed() { 
      return false; 

     } 
} 

下面是我的圈子類。

import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Paint; 
import android.graphics.Point; 
import android.util.FloatMath; 

import com.google.android.maps.GeoPoint; 
import com.google.android.maps.MapView; 
import com.google.android.maps.Overlay; 
import com.google.android.maps.Projection; 

public class CircleOverlay extends Overlay { 

    Context context; 
    double mLat; 
    double mLon; 
    float mRadius; 

    public CircleOverlay(Context _context, double _lat, double _lon, float radius) { 
     context = _context; 
     mLat = _lat; 
     mLon = _lon; 
     mRadius = radius; 

     } 

    public void draw(Canvas canvas, MapView mapView, boolean shadow) { 
     super.draw(canvas, mapView, shadow); 

     if(shadow) return; // Ignore the shadow layer 

     Projection projection = mapView.getProjection(); 

     Point pt = new Point(); 

     GeoPoint geo = new GeoPoint((int) (mLat *1e6), (int)(mLon * 1e6)); 

     projection.toPixels(geo ,pt); 
     float circleRadius = projection.metersToEquatorPixels(mRadius) * (1/ FloatMath.cos((float) Math.toRadians(mLat))); 

     Paint innerCirclePaint; 

     innerCirclePaint = new Paint(); 
     innerCirclePaint.setColor(Color.BLUE); 
     innerCirclePaint.setAlpha(25); 
     innerCirclePaint.setAntiAlias(true); 

     innerCirclePaint.setStyle(Paint.Style.FILL); 

     canvas.drawCircle((float)pt.x, (float)pt.y, circleRadius, innerCirclePaint); 

    } 

} 

回答

0

從谷歌例如:hello-mapview

每次添加一個新的OverlayItem到ArrayList,必須呼籲ItemizedOverlay填入(),它會讀取每個OverlayItem對象,並準備他們是畫。

public void showcurrentlocation(View view) { 

    geocoder = new Geocoder(GMapsActivity.this); 
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
    mapView.getOverlays().add(new CircleOverlay(this, location.getLatitude(),location.getLongitude(),325)); 
    if (location != null) {  
     Log.d(TAG, location.toString()); 
     this.onLocationChanged(location); //<6> 
    } 
    populate(); 

}

但我認爲你應該使用MyLocationOverlay類此MyLocationOverlay

+0

嗨喬治,我想使用MyLocationOverlay,將在while.thanks更新。 – manishpro

+0

嗨,我用我的位置疊加,現在我可以看到我當前位置的默認點。感謝您的建議。 – manishpro

相關問題