如何将指南针添加到地图视图
创建地图视图时如何让指南针显示在屏幕上。这段代码有什么问题?有什么建议吗?
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);
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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我不会在
onCreate()
上调用enableCompass()
(鉴于 <code>setContentView(),我假设此代码来自于此) > 呼叫)。在onResume()
中启用指南针,并在onPause()
中禁用它,这样当您的活动不在屏幕上时,您就不会保持传感器处于活动状态。而且您不应该需要postInvalidate()
。否则,这看起来不错。请记住,它仅适用于实际硬件。
这是一个在
上启用指南针的示例项目
MyLocationOverlay 绝对有效,尽管您需要替换为您自己的 android:apiKey 值。
Well, I wouldn't call
enableCompass()
ononCreate()
(which is where I assume this code comes from, given thesetContentView()
call). Enable the compass inonResume()
and disable it inonPause()
, so you don't keep the sensors alive when your activity is not on-screen. And you should not needpostInvalidate()
.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 ownandroid:apiKey
value.