Note that there are some explanatory texts on larger screens.

plurals
  1. PONot able to draw a circle around my current location in map
    primarykey
    data
    text
    <p>I am trying to show my current location in map along with the address. Below code works fine but somehow i am not able to draw or point a circle around my current location.Any help would be appreciated.I am using the old API.</p> <p>This is my main class.</p> <pre><code> 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); //&lt;6&gt; } } @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&lt;Address&gt; addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 10); //&lt;10&gt; 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); //&lt;11&gt; } 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); //&lt;7&gt; // } // @Override protected void onPause() { // super.onPause(); // locationManager.removeUpdates(this); //&lt;8&gt; // // } @Override protected boolean isRouteDisplayed() { return false; } } </code></pre> <p>Below is my circle class.</p> <pre><code>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); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload