Android Canvas - 绘制一个点并根据 GPS 移动重新定位它的回调

发布于 2024-11-19 08:06:31 字数 251 浏览 4 评论 0原文

我是 android 新手,刚刚研究过 canvas API。我目前有一个名为“MyMap”的活动类和一个名为“MapView”的ViewClass(它扩展了视图)。

MyMap 活动将内容视图设置为 MapView,在 MapView 内,我使用地图的位图图像创建画布,然后根据移动设备当前的 GPS 坐标在地图上绘制一个圆圈。我想知道每当 GPS 坐标发生变化时如何对 draw() 方法执行一些回调?这段代码也会出现在 MyMap 活动中还是 MapView 中?

I am new to android and i've just been looking into the canvas API. I currently have an activity class called "MyMap" and a ViewClass called "MapView" (which extends view).

The MyMap activity sets the content view to MapView and inside MapView I am creating a canvas with a bitmap image of a Map and I am then drawing a circle on the map based on the current GPS coordinates of the mobile. I want to know how do I perform some callback to the draw() method whenever the gps coordinates change? also will this code go in MyMap activity or in the MapView?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

紧拥背影 2024-11-26 08:06:31

您需要做的是首先设置一个 GPS 侦听器并对其做出响应,您可以在此处找到相关信息 LocationManager

它看起来像这样。请记住,GPS 会消耗电池,有很多方法可以做得更好,但这里是它的核心,或多或少

一旦你完成了设置,只需在 onLocationChanged 侦听器中执行你需要执行的操作

代码片段 - 不完整

import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;

public class gps extends Activity  {

    private LocationManager locManager = null; 

        public void onCreate(Bundle state) {
           super.onCreate(state);

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

        LocationListener onLocationChange=new LocationListener() {
        public void onLocationChanged(Location loc) {

                  // DO SOMETHING HERE with the updated location information to change your map

            }
        }

        public onResume() {
             startGPS(1000,1); // Every second or when we move 1 meter
        }

        public onPause() {
             stopGPS(); 
        }

        private boolean startGPS(long interval, float meterTrigger)
    {

        if (!locManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
            return false; 



                            locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,interval,meterTrigger,onLocationChange);



                return true;
        }

        public void stopGPS() {
       locManager.removeUpdates(onLocationChange); 
    }

画圆 - 您需要确定位置和半径

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    float x = 0;
    float y = 0;
    float r = 0; 

    paint = new Paint(Paint.ANTI_ALIAS_FLAG); // You can cache this higher up and reuse it 
    paint.setColor(0xFFFF0000);  
    canvas.drawCircle(x, y, r, paint);
}

What you need to do is first setup a gps listener and respond to that, you can find information about that here LocationManager

It would look something like this. Keep in mind that GPS sucks battery, there are ways to do this better but here is the meat of it, more or less

Once you have that setup just do what you need to do in the onLocationChanged listener

CODE SNIPPET - NOT COMPLETE

import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;

public class gps extends Activity  {

    private LocationManager locManager = null; 

        public void onCreate(Bundle state) {
           super.onCreate(state);

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

        LocationListener onLocationChange=new LocationListener() {
        public void onLocationChanged(Location loc) {

                  // DO SOMETHING HERE with the updated location information to change your map

            }
        }

        public onResume() {
             startGPS(1000,1); // Every second or when we move 1 meter
        }

        public onPause() {
             stopGPS(); 
        }

        private boolean startGPS(long interval, float meterTrigger)
    {

        if (!locManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
            return false; 



                            locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,interval,meterTrigger,onLocationChange);



                return true;
        }

        public void stopGPS() {
       locManager.removeUpdates(onLocationChange); 
    }

Painting the circle - you will need to determine the positioning and radius

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    float x = 0;
    float y = 0;
    float r = 0; 

    paint = new Paint(Paint.ANTI_ALIAS_FLAG); // You can cache this higher up and reuse it 
    paint.setColor(0xFFFF0000);  
    canvas.drawCircle(x, y, r, paint);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文