如何添加指南针的MapView [英] how to add compass to mapview

查看:156
本文介绍了如何添加指南针的MapView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要如何在屏幕上显示的指南针创建我的MapView时。有什么不对这个code?有什么建议?

 保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);        的setContentView(R.layout.map);        图形页面=(图形页面)findViewById(R.id.mapview);
        mapView.setBuiltInZoomControls(真);        MC = mapView.getController();
        myLoc =新MyLocationOverlay(这一点,MapView类);
        myLoc.enableCompass();
        。调用MapView.getOverlays()加(myLoc);
        mapView.postInvalidate();        locManager =(的LocationManager)getSystemService(Context.LOCATION_SERVICE);


解决方案

好了,我不会把 enableCompass()的onCreate() (这是我认为这code的来源,给出的的setContentView()调用)。启用 onResume)指南针(和的onPause禁用(),所以你不保持传感器活着当你的活动是不是在屏幕上。你不应该需要 postInvalidate()

否则,这似乎罚款。请记住,它只会在实际硬件上运行。

下面是一个示例项目,使罗盘 MyLocationOverlay 那肯定的作品,但你需要在自己的 Android的替代:apiKey

How do I get the compass to be shown on the screen when my mapview is created. What is wrong with this code? any suggestions?

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

解决方案

Well, I wouldn't call enableCompass() on onCreate() (which is where I assume this code comes from, given the setContentView() call). Enable the compass in onResume() and disable it in onPause(), so you don't keep the sensors alive when your activity is not on-screen. And you should not need postInvalidate().

Otherwise, this seems fine. Bear in mind that it will only work on actual hardware.

Here is a sample project that enables the compass on MyLocationOverlay that definitely works, though you will need to substitute in your own android:apiKey value.

这篇关于如何添加指南针的MapView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆