自定义我的位置叠加层更新时间

2024-01-20

我正在努力实施MyLocationOverlay http://code.google.com/android/add-ons/google-apis/reference/com/google/android/maps/MyLocationOverlay.html来自谷歌地图的课程。但是,当我尝试使用自己的 locationManager 时,我无法在地图上实际绘制叠加层。我想知道我是否做错了什么。

我不想调用 MyLocationOverlay 类的超级方法(enbaleMyLocation),因为该请求从 locationManager 更新的速度太快,会耗尽我的电池。

这是我的代码:

private class CenterOverlay extends MyLocationOverlay {

    private Context mContext;
    private LocationManager mLocManager;

    public CenterOverlay(Context context, MapView mapView) {
        super(context, mapView);
        this.mContext = context;

    }

    @Override
    public void onLocationChanged(Location location) {
        super.onLocationChanged(location);
        try {
            doExternalCenterOverlayTask(location);
        } catch (JSONException e) {
            e.printStackTrace();
            ErrorHandler.serviceException(mContext);
        } catch (IOException e) {
            e.printStackTrace();
            ErrorHandler.IOException(mContext);
        } catch (ServiceException e) {
            e.printStackTrace();
            ErrorHandler.serviceException(mContext);
        }
    }

    @Override
    public boolean enableMyLocation() {
        mLocManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
        mLocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 50, this);
        mLocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 5000, 25, this);

        return mLocManager.isProviderEnabled(LocationManager.GPS_PROVIDER)
                || mLocManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    }

    @Override
    public void disableMyLocation() {
        super.disableMyLocation();
        mLocManager.removeUpdates(this);
    }

    @Override
    public void onProviderDisabled(String provider) {
        super.onProviderDisabled(provider);
        ViewAdapter.createStandardAlertDialog(mContext,
                "Your location provider has been disabled, please re-enable it.");
    }

    @Override
    public void onProviderEnabled(String provider) {
        super.onProviderEnabled(provider);
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        super.onStatusChanged(provider, status, extras);
        if (status == 0) {
            ViewAdapter.showLongToast(mContext, "Location is not available at this time");
        } else if (status == 1) {
            //Trying to connect
        } else if (status == 2) {
            // Available
        }

    }

}

我已经弄清楚了这一点,直到谷歌想要更新他们的代码之前,这是不可能的。

相反,我创建了自己的类来为我绘制位置——您可以随时更新该类。

代码在这里。

/**
* 
* @author (hwrdprkns)
* 2010
*
*/
public class CenterOverlay extends ItemizedOverlay<OverlayItem> implements LocationListener {

private static final String TAG = "CenterOverlay: ";

private LocationManager mLocationManager;

private long updateTime = 60000;

private static final int updateDistance = 50;

private GeoPoint lastKnownPoint;

private Location lastKnownLocation;

private Drawable centerDrawable;

private Context mContext;

private final List<OverlayItem> mOverlays = new ArrayList<OverlayItem>();

private Paint accuracyPaint;

private Point center;

private Point left;

private Drawable drawable;

private int width;

private int height;

// San Francisco
private final static double[] DEFAULT_LOCATION = {
        37.7749295, -122.4194155
};

private Runnable firstFixRunnable = null;

private boolean firstFixRun = false;

public CenterOverlay(Drawable defaultMarker, MapView mapView, Context c) {
    super(boundCenter(defaultMarker));
    this.centerDrawable = defaultMarker;
    this.mContext = c;
    mLocationManager = (LocationManager) c.getSystemService(Context.LOCATION_SERVICE);

    if (Constants.DEBUG) {
        updateTime = 0;
    } else {
        updateTime = 60000;
    }
}

public void addOverlay(OverlayItem overlay) {
    mOverlays.add(overlay);
    populate();
}

private void checkFirstRunnable() {
    if (!firstFixRun && lastKnownLocation != null && firstFixRunnable != null) {
        firstFixRunnable.run();
    }
}

private OverlayItem createCenterOverlay(GeoPoint point) {
    OverlayItem i = new OverlayItem(point, "Location", null);
    i.setMarker(centerDrawable);
    return i;
}

private GeoPoint createGeoPoint(Location loc) {
    int lat = (int) (loc.getLatitude() * 1E6);
    int lng = (int) (loc.getLongitude() * 1E6);
    return new GeoPoint(lat, lng);
}

@Override
protected OverlayItem createItem(int i) {
    return mOverlays.get(i);
}

public void disableLocation() {
    mLocationManager.removeUpdates(this);
}

@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
    drawMyLocation(canvas, mapView, lastKnownLocation, lastKnownPoint, 0);
}

protected void drawMyLocation(Canvas canvas, MapView mapView, Location lastFix, GeoPoint myLoc,
        long when) {

    accuracyPaint = new Paint();
    accuracyPaint.setAntiAlias(true);
    accuracyPaint.setStrokeWidth(2.0f);

    drawable = centerDrawable;
    width = drawable.getIntrinsicWidth();
    height = drawable.getIntrinsicHeight();
    center = new Point();
    left = new Point();

    Projection projection = mapView.getProjection();

    double latitude = lastFix.getLatitude();
    double longitude = lastFix.getLongitude();
    float accuracy = lastFix.getAccuracy();

    float[] result = new float[1];

    Location.distanceBetween(latitude, longitude, latitude, longitude + 1, result);
    float longitudeLineDistance = result[0];

    GeoPoint leftGeo = new GeoPoint((int) (latitude * 1e6), (int) ((longitude - accuracy
            / longitudeLineDistance) * 1e6));
    projection.toPixels(leftGeo, left);
    projection.toPixels(myLoc, center);
    int radius = center.x - left.x;

    accuracyPaint.setColor(0xff6666ff);
    accuracyPaint.setStyle(Style.STROKE);
    canvas.drawCircle(center.x, center.y, radius, accuracyPaint);

    accuracyPaint.setColor(0x186666ff);
    accuracyPaint.setStyle(Style.FILL);
    canvas.drawCircle(center.x, center.y, radius, accuracyPaint);

    drawable.setBounds(center.x - width / 2, center.y - height / 2, center.x + width / 2,
            center.y + height / 2);
    drawable.draw(canvas);
}

public void enableMyLocation() {
    for (String s : mLocationManager.getProviders(true)) {
        mLocationManager.requestLocationUpdates(s, updateTime, updateDistance, this);
    }

    Location loc = null;

    for (String s : mLocationManager.getProviders(true)) {
        loc = mLocationManager.getLastKnownLocation(s);
        if (loc != null) {
            loc.setLatitude(DEFAULT_LOCATION[0]);
            loc.setLongitude(DEFAULT_LOCATION[1]);
            lastKnownLocation = loc;
            lastKnownPoint = createGeoPoint(loc);
            return;
        }
    }

    loc = new Location(LocationManager.GPS_PROVIDER);
    loc.setLatitude(DEFAULT_LOCATION[0]);
    loc.setLongitude(DEFAULT_LOCATION[1]);

    lastKnownLocation = loc;
    lastKnownPoint = createGeoPoint(loc);
}

public Location getLastKnownLocation() {
    return lastKnownLocation;
}

public GeoPoint getLastKnownPoint() {
    return lastKnownPoint;
}

public void onLocationChanged(Location location) {
    checkFirstRunnable();
    this.lastKnownLocation = location;
    this.lastKnownPoint = createGeoPoint(location);
    replaceOverlay(createCenterOverlay(lastKnownPoint));

}

public void onProviderDisabled(String provider) {
    ViewAdapter.showLongToast(mContext,
            "Your location provider has been disabled -- please reenable it");

}

public void onProviderEnabled(String provider) {

}

public void onStatusChanged(String provider, int status, Bundle extras) {

    if (status == LocationProvider.AVAILABLE) {

    }
    if (status == LocationProvider.OUT_OF_SERVICE) {
        ViewAdapter.showShortToast(mContext, "Location is temporarily out of service.");
    }
    if (status == LocationProvider.TEMPORARILY_UNAVAILABLE) {

    }
}

private void replaceOverlay(OverlayItem overlay) {
    mOverlays.clear();
    mOverlays.add(overlay);
    populate();
}

public boolean runOnFirstFix(Runnable runnable) {

    if (lastKnownLocation != null) {
        runnable.run();
        return true;
    }

    firstFixRunnable = runnable;
    return false;

}

@Override
public int size() {
    return mOverlays.size();
}

public void updateLocation() {

}

}

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

自定义我的位置叠加层更新时间 的相关文章

随机推荐

  • 使用 KnpPaginatorBundle 对连接表进行排序

    我设置了一个测试来更加熟悉 Symfony2 和 KnpPaginatorBundle 我有一张包含宠物的桌子 其中引用了宠物动物类型 Dog Cat ETC 我可以按id name 但是当我尝试按动物排序时type我收到一条错误消息 指出
  • 将 Google 文档集成到网站中以进行内容创建

    我正在建立一个自助出版网站 我想将 Google 文档集成到我的网站中 并允许每个出版商 作家从我的网站编写她 他的书 然后从我的网站或直接从 Google 文档更新内容 并保持两个版本的内容同步 这可能吗 谢谢 是的 这是可能的 您可以获
  • .net 4 向后兼容性

    在 net 4 中开发的 Windows 应用程序引用在 net 3 5 中开发的程序集 是否需要安装 net 4 和 net 3 5 才能运行该应用程序 我怀疑是这样 正如我的观察之一 此外 这感觉很合乎逻辑 因为两者的执行都需要不同的运
  • 使用 Kerberos 对 Windows 进行 Ansible 不起作用

    我尝试使用 Ansible 1 9 0 1 使用域用户名配置 Windows 服务器 我已经成功设置了 Linux Ansible 控制盒 并且能够使用基本身份验证来运行 ansible ansible playbook play 但是 使
  • 如何从剪贴板中清除指定格式的数据?

    我将一些数据放入剪贴板 从剪贴板复制数据后 我想清除数据而不清除整个剪贴板 像这样的东西 wchar t buf NULL if OpenClipboard NULL 0 HANDLE hData GetClipboardData CF U
  • 如何在以 struct 作为参数的 Ruby FFI 方法中包装函数?

    我正在尝试使用 ruby ffi 从共享对象调用函数 我将以下内容编译成共享对象 include
  • Laravel 的数据表服务器端 php 类

    我希望我的数据表在服务器端处理数据 我引用了这个示例 服务器端示例 http www datatables net examples data sources server side html 然而 本例中给出的服务器端 php 类 ssp
  • Internet Explorer 10 后退按钮缓存

    在 Internet Explorer 10 中 如果您按后退按钮 它会尝试从浏览器缓存中获取上一页 此行为与几乎所有其他浏览器 包括 IE9 不同 在 IE9 中 按后退按钮将完全重新加载上一页 而不是重用缓存 我如何从网站与 IE10
  • 错误 22 无法从程序集中加载“EnsureBindingRedirects”任务

    我使用 vs 2013 克隆了一个项目 当我运行它时 我收到此错误 Error 1 The EnsureBindingRedirects task could not be loaded from the assembly D BMaste
  • dplyr 输出类 data.frame

    我可以总结一个数据框dplyr像这样 mtcars gt group by cyl gt summarise mean mpg 将输出转换回类data frame 我目前的做法是这样的 as data frame mtcars gt gro
  • 如何使用 --set 来设置 Prometheus 图表的值?

    例如 设置alertmanager ingress annotations要添加两个项目 这两种方法都不起作用 helm install stable prometheus set alertmanager ingress enabled
  • AES CBC 加密/解密仅解密前 16 个字节

    我正在使用 AES CBC 和 openssl 进行一些工作 目前 我遇到了一个问题 我无法猜测出什么问题 一如既往 如果消息长度小于 16 字节 则加密和解密过程可以正常工作 但当消息大于 16 字节时 解密仅对第 16 个字节有效 当我
  • 由于递归隐式,spray-json 中的 NPE(上下文绑定问题?)

    也许我发现了一个bug http goo gl C79j8在 Spray json 中 当我尝试获取具有自身类型字段的对象的 json 时 出现空指针异常 例子是 case class TestItem subitems Option Li
  • System.Windows.Media.RenderCapability.Tier 返回的不是渲染模式

    I use System Windows Media RenderCapability Tier http msdn microsoft com en us system windows media rendercapability tie
  • MPMusicPlayerController 未正确准备/预加载

    我正在使用 MPMusicPlayerController 因此我的应用程序可以播放用户通过 iTunes 购买的音乐 当我选择一首歌曲并开始播放时 声音开始之前有一段延迟 我假设这首歌是从云端缓冲的 问题是我还没有找到一种方法来知道缓冲何
  • socket.io 通过 XHR 轮询强制断开连接

    我有一个客户端 服务器应用程序 在服务器上使用nodejs 并使用socket io 作为连接机制 出于与我的应用程序相关的原因 我希望每个浏览器只有一个活动连接 并拒绝来自稍后可能在会话期间打开的其他选项卡的所有连接 这对于 WebSoc
  • 在.Net 3.5中写入app.config?

    我需要能够写入 Net 3 5 Windows 应用程序中的 app config 文件来存储一些系统设置 我可以从文件中读取但无法写入它 我发现的所有内容都是针对 2 0 的 与 3 5 看起来不一样 NET 2 0 是 NET 3 5
  • startkey 和 endkey 在 CouchDB 中到底是如何工作的?

    我正在使用 CouchDB 中的位置数据库 我创建了一个视图 其中我的键是一个带有纬度和经度舍入值的数组 现在我根据以下条件进行选择 Startkey 52 34 4 883 Endkey 52 37 4 903 在这里 我预计只会收到纬度
  • Parse.com 查询的主线程问题

    我正在尝试在我的 Unity 游戏中使用 parse com 服务 我的问题是根据查询收到的结果实例化对象 例如 当我运行以下代码时 var queryCurrent ParseObject GetQuery Levels WhereEqu
  • 自定义我的位置叠加层更新时间

    我正在努力实施MyLocationOverlay http code google com android add ons google apis reference com google android maps MyLocationOv