自定义我的位置重叠更新时间我的、更新时间、自定义、位置

2023-09-04 23:31:51 作者:沒距離的溫度

我想实现的 MyLocationOverlay 类从谷歌地图。然而,当我试图用我自己的locationManager - 我不能覆盖到实际上可以在地图上绘制。我想知道如果我做错了什么。

我不想叫MyLocationOverlay类的父类方法(enbaleMyLocation),因为从locationManager方式太快的要求更新和会吃我的电池还活着。

下面是我的code:

 私有类CenterOverlay扩展MyLocationOverlay {

    私人语境mContext;
    私人LocationManager mLocManager;

    公共CenterOverlay(上下文的背景下,图形页面图形页面){
        超(背景下,图形页面);
        this.mContext =背景;

    }

    @覆盖
    公共无效onLocationChanged(位置定位){
        super.onLocationChanged(位置);
        尝试 {
            doExternalCenterOverlayTask(位置);
        }赶上(JSONException E){
            e.printStackTrace();
            ErrorHandler.serviceException(mContext);
        }赶上(IOException异常E){
            e.printStackTrace();
            ErrorHandler.IOException(mContext);
        }赶上(ServiceException E){
            e.printStackTrace();
            ErrorHandler.serviceException(mContext);
        }
    }

    @覆盖
    公共布尔enableMyLocation(){
        mLocManager =(LocationManager)mContext.getSystemService(Context.LOCATION_SERVICE);
        mLocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,10000,50本);
        mLocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,5000,25,本);

        返回mLocManager.isProviderEnabled(LocationManager.GPS_PROVIDER)
                || mLocManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    }

    @覆盖
    公共无效disableMyLocation(){
        super.disableMyLocation();
        mLocManager.removeUpdates(本);
    }

    @覆盖
    公共无效onProviderDisabled(字符串提供商){
        super.onProviderDisabled(供应商);
        ViewAdapter.createStandardAlertDialog(mContext,
                您的位置提供商已被禁用,请重新启用它。);
    }

    @覆盖
    公共无效onProviderEnabled(字符串提供商){
        super.onProviderEnabled(供应商);
    }

    @覆盖
    公共无效onStatusChanged(字符串商,INT地位,捆绑演员){
        super.onStatusChanged(供应商的地位,临时演员);
        如果(状态== 0){
            ViewAdapter.showLongToast(mContext,地点是不是在这个时间);
        }否则,如果(状态== 1){
            //尝试连接
        }否则,如果(状态== 2){
            //可用
        }

    }

}
 

解决方案

我已经读懂了这条底线,这是不可能做,直到谷歌感觉就像更新自己的code。

相反,我已经创建了自己的类,它绘制的位置对我来说 - 只要你喜欢,可以更新

code因为这是这里。

  / **
*
* @author(hwrdprkns)
* 2010
*
* /
公共类CenterOverlay扩展ItemizedOverlay< OverlayItem>实现LocationListener的{

私有静态最后字符串变量=CenterOverlay:;

私人LocationManager mLocationManager;

私人长时间录入= 60000;

私有静态最终诠释updateDistance = 50;

私人的GeoPoint lastKnownPoint;

私人的位置lastKnownLocation;

私人绘制对象centerDrawable;

私人语境mContext;

私人最终名单,其中,OverlayItem> mOverlays =新的ArrayList< OverlayItem>();

私人油漆accuracyPaint;

私人点中心;

私人点左右;

私人绘制对象绘制;

私人诠释宽度;

私人诠释高度;

// 旧金山
私人最终静态双重[]指定default_location = {
        37.7749295,-122.4194155
};

私人可运行firstFixRunnable = NULL;

私人布尔firstFixRun = FALSE;

公共CenterOverlay(可绘制defaultMarker,图形页面图形页面,上下文C){
    超(boundCenter(defaultMarker));
    this.centerDrawable = defaultMarker;
    this.mContext = C;
    mLocationManager =(LocationManager)c.getSystemService(Context.LOCATION_SERVICE);

    如果(Constants.DEBUG){
        更新时间= 0;
    } 其他 {
        更新时间= 60000;
    }
}

公共无效addOverlay(OverlayItem叠加){
    mOverlays.add(叠加);
    填充();
}

私人无效checkFirstRunnable(){
    如果(firstFixRun&安培;!&安培; lastKnownLocation = NULL和放大器;!&安培;!firstFixRunnable = NULL){
        firstFixRunnable.run();
    }
}

私人OverlayItem createCenterOverlay(的GeoPoint点){
    OverlayItem I =新OverlayItem(点,位置,NULL);
    i.setMarker(centerDrawable);
    返回我;
}

私人的GeoPoint createGeoPoint(位置LOC){
    INT纬度=(INT)(loc.getLatitude()* 1E6);
    INT LNG =(INT)(loc.getLongitude()* 1E6);
    返回新的GeoPoint(纬度,经度);
}

@覆盖
保护OverlayItem createItem中(int i)以{
    返回mOverlays.get(ⅰ);
}

公共无效disableLocation(){
    mLocationManager.removeUpdates(本);
}

@覆盖
公共无效画(油画画布,图形页面图形页面,布尔影子){
    drawMyLocation(帆布,图形页面,lastKnownLocation,lastKnownPoint,0);
}

保护无效drawMyLocation(画布油画,图形页面图形页面,地点lastFix,GeoPoint对象myLoc,
        时长){

    accuracyPaint =新的油漆();
    accuracyPaint.setAntiAlias​​(真正的);
    accuracyPaint.setStrokeWidth(2.0f);

    绘制= centerDrawable;
    宽度= drawable.getIntrinsicWidth();
    身高= drawable.getIntrinsicHeight();
    中心=新的点();
    左=新的点();

    投影投影= MapView.getProjection()在;

    双纬度= lastFix.getLatitude();
    双经度= lastFix.getLongitude();
    浮动精度= lastFix.getAccuracy();

    浮动[]结果=新的浮动[1];

    Location.distanceBetween(纬度,经度,纬度,经度+ 1,结果);
    浮longitudeLineDistance =结果[0];

    的GeoPoint leftGeo =新的GeoPoint((INT)(纬度* 1e6个),(INT)((经度 - 精度
            / longitudeLineDistance)* 1e6个));
    projection.toPixels(leftGeo,左);
    projection.toPixels(myLoc,中心);
    INT半径= center.x  -  left.x;

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

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

    drawable.setBounds(center.x  - 宽/ 2,center.y  - 高度/ 2,center.x +宽/ 2,
            center.y +高/ 2);
    drawable.draw(画布);
}

公共无效enableMyLocation(){
    对于(字符串S:mLocationManager.getProviders(真)){
        mLocationManager.requestLocationUpdates(S,录入,updateDistance,这一点);
    }

    位置LOC = NULL;

    对于(字符串S:mLocationManager.getProviders(真)){
        LOC = mLocationManager.getLastKnownLocation(多个);
        如果(LOC!= NULL){
            loc.setLatitude(指定default_location [0]);
            loc.setLongitude(指定default_location [1]);
            lastKnownLocation =禄;
            lastKnownPoint = createGeoPoint(LOC);
            返回;
        }
    }

    LOC =新的位置(LocationManager.GPS_PROVIDER);
    loc.setLatitude(指定default_location [0]);
    loc.setLongitude(指定default_location [1]);

    lastKnownLocation =禄;
    lastKnownPoint = createGeoPoint(LOC);
}

公共场所getLastKnownLocation(){
    返回lastKnownLocation;
}

公众的GeoPoint getLastKnownPoint(){
    返回lastKnownPoint;
}

公共无效onLocationChanged(位置定位){
    checkFirstRunnable();
    this.lastKnownLocation =位置;
    this.lastKnownPoint = createGeoPoint(位置);
    replaceOverlay(createCenterOverlay(lastKnownPoint));

}

公共无效onProviderDisabled(字符串提供商){
    ViewAdapter.showLongToast(mContext,
            您的位置提供商已被禁用 - 请重新启用);

}

公共无效onProviderEnabled(字符串提供商){

}

公共无效onStatusChanged(字符串商,INT地位,捆绑演员){

    如果(状态== LocationProvider.AVAILABLE){

    }
    如果(状态== LocationProvider.OUT_OF_SERVICE){
        ViewAdapter.showShortToast(mContext,位置暂时停止服务。);
    }
    如果(状态== LocationProvider.TEMPORARILY_UNAVAILABLE){

    }
}

私人无效replaceOverlay(OverlayItem叠加){
    mOverlays.clear();
    mOverlays.add(叠加);
    填充();
}

公共布尔runOnFirstFix(Runnable的可运行){

    如果(lastKnownLocation!= NULL){
        runnable.run();
        返回true;
    }

    firstFixRunnable =可运行;
    返回false;

}

@覆盖
公众诠释大小(){
    返回mOverlays.size();
}

公共无效updateLocation(){

}
 
在word 中,为什么我自定义调整页边距的时候会出现这种情况

}

I am trying to implement the MyLocationOverlay class from google maps. However, when I try and use my own locationManager -- I cannot get the overlay to acutally draw on the map. I am wondering if I am doing something wrong.

I don't want to call the super method(enbaleMyLocation) of the MyLocationOverlay class because that request updates from the locationManager way too quickly and will eat my battery alive.

Here is my code:

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
        }

    }

}

解决方案

I have gotten to the bottom of this and it is not possible to do until Google feels like updating their code.

Instead, I've created my own class that draws the location for me -- that can be updated whenever you like.

Code for that is here.

/**
* 
* @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() {

}

}