如何为mapview添加指南针

时间:2011-04-21 23:31:20

标签: android android-mapview compass-geolocation

创建mapview时,如何让指南针显示在屏幕上。这段代码有什么问题?有什么建议吗?

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.map);



        mapView = (MapView) findViewById(R.id.mapview);
        mapView.setBuiltInZoomControls(true);

        mc = mapView.getController();
        myLoc = new MyLocationOverlay(this, mapView);
        myLoc.enableCompass();
        mapView.getOverlays().add(myLoc);


        mapView.postInvalidate();

        locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

1 个答案:

答案 0 :(得分:3)

好吧,我不会在enableCompass()上调用onCreate()(这是我假设此代码来自的地方,给定setContentView()调用)。在onResume()中启用指南针并在onPause()中禁用指南针,这样当您的活动不在屏幕上时,您就不会保持传感器处于活动状态。你不应该需要postInvalidate()

否则,这似乎很好。请记住,它只适用于实际的硬件。

Here is a sample project使MyLocationOverlay上的指南针确实有效,但您需要用自己的android:apiKey值替换。