android - 如何将指南针添加到 map View

标签 android android-mapview compass-geolocation

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

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);

最佳答案

好吧,我不会在 onCreate() 上调用 enableCompass()(考虑到 setContentView(),这是我假设这段代码的来源) 调用)。在 onResume() 中启用指南针并在 onPause() 中禁用它,这样当您的 Activity 不在屏幕上时,您就不会让传感器保持 Activity 状态。而且您不需要 postInvalidate()

否则,这似乎没问题。请记住,它只能在实际硬件上运行。

Here is a sample projectMyLocationOverlay 上启用指南针肯定有效,但您需要替换为您自己的 android:apiKey 值。

关于android - 如何将指南针添加到 map View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5750897/

相关文章:

android - 如何在android中点击按钮播放声音 fragment ?

iphone - 从 MKUserTrackingModeFollowWithHeading 切换模式时如何始终保持 iOS 6 中的 MKMapView 朝北

android - 将 MapView 限制为屏幕的一半

android - 平板电脑上的 Mapview : How can I center the map with an offset?

iphone - 使用 iPhone 4 上的陀螺仪补偿罗盘滞后

android - Android应用程序运行时如何切换Android应用程序主题

android - 停止 AlertDialog 中按钮的多行文本

android - Gradle Android Studio 集成

accelerometer - 使用原始加速度计和磁力计数据计算磁航向

javascript - 将箭头指向位置(地理位置)