住所と一緒に現在地を地図に表示しようとしています。以下のコードは正常に機能しますが、どういうわけか現在の場所の周りに円を描いたりポイントしたりすることができません。助けていただければ幸いです。古い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);
}
}