谷歌地图API 2图形页面不更新 [英] Google Maps API 2 MapView Not Updating

查看:169
本文介绍了谷歌地图API 2图形页面不更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在显示Android应用谷歌地图API V2 MapView类,但它并不奇怪以一致的方式正确地更新。我使用的GPS更新地图位置(试用过的LocationManager和LocationClient),虽然地图移动到位置时,无论是街道名称层无法更新或有一个模糊/模糊部分发生故障的时间大约百分之五十更新 - 直到我做图的手动拖动(滚动)。然后,整个地图会立即更新。我已经剥离出了很多处理的应用程序,看看我在某种程度上preventing刷新,但它并没有发挥作用。

I'm displaying a Google Maps API v2 MapView in an Android app, but it's curiously not updating properly in a consistent fashion. I'm using the GPS to update the map position (tried both LocationManager and LocationClient) and although the map moves to the position, about fifty percent of the time either the street name layer fails to update or there's a fuzzy/blurry section that fails to update--until I do a manual drag (scroll) of the map. Then the entire map updates instantly. I've stripped out a lot of the processing in the app to see if I was somehow preventing a refresh, but it didn't make a difference.

我插入mapView.invalidate()调用在onCameraChange但奇怪的是似乎使问题发生更容易(虽然仍时不是100%)。

I inserted a mapView.invalidate() call in onCameraChange but that oddly seemed to make the problem occur more readily (although still not 100% of the time).

所要求的图形页面我实现所有的活动回调。

I am implementing all of the Activity callbacks as required by MapView.

有没有人遇到过这样的问题,在Android上使用谷歌地图API第2版?如果是这样,你有没有查明原因,并你们是怎么解决的?

Has anyone encountered a problem like this with Google Map API v2 on Android? If so, did you identify the cause and how did you solve it?

推荐答案

您必须让地图的呼吸可以这么说。

You have to let the map breathe so to speak.

使用 animateCamera 与当动画完成后,你会得到一个电话回<$ CancelableCallback 然后C $ C> onFinish()开始下一个动画。

Use animateCamera with a CancelableCallback then when the animation is completed you'll get a call back to onFinish() start the next animation.

public class KmlReader extends ActionBarActivity implements
    CancelableCallback {


@Override
public void onFinish() {
    startAnimation(); // start next map movement
}


@Override
public void onCancel() {
    //Called when user interacts with the map while it is moving.
}


public void startAnimation(){

cameraPosition = mMap.getCameraPosition();    
LatLng ll = new LatLng(expectedLocation.getLatitude(),
                    expectedLocation.getLongitude());
            cb.zoom(cameraPosition.zoom)
            // previous camera tilt
                    .tilt(cameraPosition.tilt)
                    // new expected destination
                    .target(ll)
                    // north up or heading view
                    .bearing((isHeading) ? bearing : 0f);
            cameraPosition = cb.build();
            CameraUpdate update = CameraUpdateFactory
                    .newCameraPosition(cameraPosition);
            mMap.animateCamera(update, working_interval, this);
}

*编辑这是code现在我的工作。*
它采用了calcuations的AsyncTask的。我给它一个步行测试,但在车辆没有测试过。

* Edit this is the code I'm working on now.* it uses an asynctask for the calcuations. I've given it a walking test but haven't tested it in a vehicle.

private static CameraPosition currentCameraPosition;
private static com.google.android.gms.maps.model.CameraPosition.Builder cameraPositionBuilder;
private volatile CameraUpdate nextCameraUpdate;
// updates 
private static final long UPDATE_INTERVAL = 2500;
// fastest 
private static final int FASTEST_INTERVAL = 2500;
private static int working_interval = 5000; 
private volatile boolean isAnimating;


// Define the callback method that receives location updates
@SuppressLint("NewApi")
@Override
public void onLocationChanged(Location location) {
    Log.d("test", Boolean.toString(isAnimating)  +" onlocation");
    currentCameraPosition = mMap.getCameraPosition();

    NewCameraUpdateTask newCameraUpdateTask = new NewCameraUpdateTask();

// This task must run async
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
    newCameraUpdateTask.executeOnExecutor(
            AsyncTask.THREAD_POOL_EXECUTOR, location);
} else {
    newCameraUpdateTask.execute(location);
}
    // speed display
    setMetersPerSecond(location.getSpeed());
}

// create a newCameraUpdate to move the map with
private class NewCameraUpdateTask extends
        AsyncTask<Location, Void, CameraUpdate> {

    @Override
    protected CameraUpdate doInBackground(Location... params) {
        Location workingLocation = null;
        CameraUpdate newCameraUpdate = null;

        float bearing = 0f;
        float speed = 0f;

        for (Location mlocation : params) {
            speed = mlocation.getSpeed();

            // camera position is saved before the start of each animation.
            LatLng ll;

        if (!mlocation.hasBearing() || speed == 0) {
            workingLocation = mlocation;
            // previous bearing
        } else {
            // current bearing
            bearing = mlocation.getBearing();
            // calculate the age of the location
            // atempt for animation to end a little bit past when
            // the
            // next
            // location arrives.
            // (location.getSpeed()m/s)(1/1000 interval seconds)(
            // 1/1000
            // km/m)
            // (1/6371 radians/km) = radians/6371000000.0
            double expectedDistance = working_interval / 6371000000.0
                    * speed;

            // latitude in Radians
            double currentLatitude = Math.toRadians(mlocation
                    .getLatitude());
            // longitude in Radians
            double currentlongitude = Math.toRadians(mlocation
                    .getLongitude());

            double calcBearing = Math.toRadians(bearing);

            // the camera position is needed so I can put in the
            // previous camera bearing when the location has no
            // bearing. This should prevent the map from
            // zooming to north when the device stops moving.

            // calculate the expected latitude and longitude based
            // on
            // staring
            // location
            // , bearing, and distance
            double sincurrentLatitude = Math.sin(currentLatitude);
            double coscurrentLatitude = Math.cos(currentLatitude);
            double cosexpectedDistance = Math.cos(expectedDistance);
            double sinexpectedDistance = Math.sin(expectedDistance);

            double expectedLatitude = Math.asin(sincurrentLatitude
                    * cosexpectedDistance + coscurrentLatitude
                    * sinexpectedDistance * Math.cos(calcBearing));
            double a = Math.atan2(
                    Math.sin(calcBearing) * sinexpectedDistance
                            * coscurrentLatitude,
                    cosexpectedDistance - sincurrentLatitude
                            * Math.sin(expectedLatitude));
            double expectedLongitude = currentlongitude + a;
            expectedLongitude = (expectedLongitude + PI3) % PI2 - PI;

            // convert to degrees for the expected destination
            double expectedLongitudeDestination = Math
                    .toDegrees(expectedLongitude);
            double expectedLatitudeDestination = Math
                    .toDegrees(expectedLatitude);

            mlocation.setLatitude(expectedLatitudeDestination);
            mlocation.setLongitude(expectedLongitudeDestination);
            workingLocation = mlocation;

        }
        break;
    }

    if (workingLocation != null) {
        if (workingLocation.hasBearing()) {
            bearing = workingLocation.getBearing();
        } else {
            bearing = currentCameraPosition.bearing;
        }
        LatLng ll = new LatLng(workingLocation.getLatitude(),
                workingLocation.getLongitude());
        cameraPositionBuilder.zoom(currentCameraPosition.zoom)
        // previous camera tilt
                .tilt(currentCameraPosition.tilt)
                // new expected destination
                .target(ll)
                // north up or heading view
                .bearing((isHeading) ? bearing : 0f);
        newCameraUpdate = CameraUpdateFactory
                .newCameraPosition(cameraPositionBuilder.build());
    }

    return newCameraUpdate;
}

@Override
protected void onPostExecute(CameraUpdate result) {
    Log.d("test", Boolean.toString(isAnimating) + " onPostExecute");
    if (result != null) {
        nextCameraUpdate = result;
        // stop the currently playing animation
        // there is a new one ready to start
        if (isAnimating) {
            if (mMap != null) {
                mMap.stopAnimation();
            }
        }
        // start the next animation
        startAnimation();
        Log.d("test", Boolean.toString(isAnimating)  +" onPostExecuteComplete");
    }


}
}


// called when map animation has been canceled
@Override
public void onCancel() {
    Log.d("test", Boolean.toString(isAnimating)  +" oncancel");
    isAnimating = false;
}

@Override
public void onFinish() {
    Log.d("test", Boolean.toString(isAnimating)  +" onfinish");
    isAnimating = false;
    startAnimation();

    // call to start saved animation.
}

private void startAnimation() {
    Log.d("test", Boolean.toString(isAnimating)  +" startAnimation");
    if (action_track) {
        if (isAnimating) {
            return;
        }
        if (nextCameraUpdate == null) {
            return;
        }
        // abort if animating
        isAnimating = true;
        CameraUpdate animateCameraUpdate = nextCameraUpdate;
        nextCameraUpdate = null;
        mMap.animateCamera(animateCameraUpdate, working_interval, this);
        Log.d("test", Boolean.toString(isAnimating)  +" startanimateCamera");
    }
}

这篇关于谷歌地图API 2图形页面不更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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