我的任务是在地图上显示用户的当前位置。
到目前为止,我发现在启用GPS时,最好的方法就是这样:
public class MyMapActivity extends MapActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
final MapView mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
// Acquire a reference to the system Location Manager
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
Log.d("location", "locationchanged: " + location.getLatitude() + ", " + location.getLongitude());
List<Overlay> mapOverlays = mapView.getOverlays();
GeoPoint point = new GeoPoint((int)location.getLatitude(), (int)location.getLongitude());
OverlayItem overlayitem2 = new OverlayItem(point, "title", "hello home!");
Drawable drawable = CouponsMapActivity.this.getResources().getDrawable(R.drawable.androidmarker);
HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable, CouponsMapActivity.this);
itemizedoverlay.addOverlay(overlayitem2);
mapOverlays.add(itemizedoverlay);
// mapView.postInvalidate();
mapView.invalidate();
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
}
}
我在
中放了一个调试点 Log.d("location", "locationchanged: " + location.getLatitude() + ", " + location.getLongitude());
那里的位置是正确的。
我知道我正在舍入坐标,这给了我一些错误的结果(这是GeoPoint只接受int的快速修复) - 但这不是问题,因为叠加也没有出现在错误上地点。它根本没有出现。
编辑:这是HelloItemizedOverlay:
公共类HelloItemizedOverlay扩展了ItemizedOverlay {
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
private Context mContext;
public HelloItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
}
public HelloItemizedOverlay(Drawable defaultMarker, Context context) {
super(boundCenterBottom(defaultMarker));
mContext = context;
}
public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}
@Override
protected OverlayItem createItem(int i) {
return mOverlays.get(i);
}
@Override
public int size() {
return mOverlays.size();
}
@Override
protected boolean onTap(int index) {
OverlayItem item = mOverlays.get(index);
AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.show();
return true;
}
注意:当我在主线程中手动设置点时,叠加层会起作用:
List<Overlay> mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.androidmarker);
final HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable, this);
GeoPoint point2 = new GeoPoint(35410000, 139460000);
OverlayItem overlayitem2 = new OverlayItem(point2, "Sekai, konichiwa!", "I'm in Japan!");
itemizedoverlay.addOverlay(overlayitem2);
mapOverlays.add(itemizedoverlay);
所以问题出在其他地方。
答案 0 :(得分:0)
我前段时间遇到了同样的问题,它与抽象界限有某种关系。在HelloItemizedOverlay的构造函数上设置drawable参数时尝试使用此方法。
HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(boundCenterBottom(drawable), CouponsMapActivity.this);
private Drawable boundCenterBottom(Drawable marker) {
marker.setBounds((-marker.getIntrinsicWidth()) / 2, -marker.getIntrinsicHeight(),
marker.getIntrinsicWidth() / 2, 0);
return marker;
}