GeoMapping Bearing and Coordinate Calculation for GoogleMaps markers

旧时模样 提交于 2019-12-04 19:49:57
danny117

probably needs 360.0

 double calcBearing =  (Math.toDegrees(Math.atan2(y, x))+360.0)%360.0;

This was kindof answered here

You still have another issue. Your not considering any tilt in the map. Why not just animate with the pixels. There won't be too much distortion of curvature. What you have to do is get the pixel position of the marker. You'll have to save the latlon when adding the marker or you have to add the markers with .setAnchor which gives you an offset in pixels. If you have the latlon of the marker placement then you get the point by.

LatLon ll;
Point p = mMap.getProjection().toScreenLocation(ll);

Then you can use code like this to animate the markers. I'm making a marker bounce below by interpolating the y axis. You'll have to interpolate both axi.

    final Handler handler = new Handler();
    final long start = SystemClock.uptimeMillis();
    final long duration = 2500;

    final Interpolator interpolator = new BounceInterpolator();

    handler.post(new Runnable() {
        @Override
        public void run() {
            long elapsed = SystemClock.uptimeMillis() - start;
            float t = Math.max(
                    1 - interpolator.getInterpolation((float) elapsed
                            / duration), 0);

            marker.setAnchor(0.5f, 1.0f + 6 * t);

            if (t > 0.0) {
                // Post again 16ms later.
                handler.postDelayed(this, 16);
            }
        }
    });

The above code is from this question. I apologize for any performance issues you have when you use the above method. But you could still use the pixel positions for a more traditional animation approach.

I've got almost the same formulas as you working in another program where I animate a map to move to the expected location based on a location bearing and speed. The formula is slightly different at the end than yours. I lifted it from here and changed to longer names.

    // Define the callback method that receives location updates
@Override
public void onLocationChanged(Location location) {

    // Given the bearing, speed, and current location
    // calculate what the expected location is traveling for an
    // interval that is slightly larger than two times fastest interval of
    // the location provider and animate the map movement to the
    // expected location over the same slightly larger interval.

    // In Theory by using an interval that is slightly larger
    // than two times fastest interval of the location provider for the
    // animation length a new animation will start before the
    // currently running animation finishes. This should ensure a
    // smooth animation of the map while traveling under most
    // circumstances.

    // Negative acceleration (braking)
    // should have acceptable map animation because the map
    // animation in theory never finishes.

    // Note longer intervals, large negative accelerations, just
    // braking at the start of an interval may result in the map moving
    // backwards. But it will still be animated.

    // Some handhelds might not be able to keep up

    // TODO CHECK THE age of the location

    // location.getSpeed() =meters/second
    // interval 1/1000 seconds
    // distance in radians km/6371

    // changed.
    // (location.getSpeed()m/s)(1/1000 interval seconds)( 1/1000 km/m)
    // (1/6371 radians/km) = radians/6371000000.0
    double expectedDistance = location.getSpeed() * expectedDistMultiplier;
    // latitude in Radians
    double currentLatitude = Math.toRadians(location.getLatitude());
    // longitude in Radians
    double longitude1 = Math.toRadians(location.getLongitude());
    double bearing;
    bearing = (location.hasBearing()) ? Math.toRadians(location
            .getBearing()) : 0;

    // calculate the expected latitude and longitude based on staring
    // location
    // , bearing, and distance

    double expectedLatitude = Math.asin(Math.sin(currentLatitude)
            * Math.cos(expectedDistance) + Math.cos(currentLatitude)
            * Math.sin(expectedDistance) * Math.cos(bearing));
    double a = Math.atan2(
            Math.sin(bearing) * Math.sin(expectedDistance)
                    * Math.cos(currentLatitude),
            Math.cos(expectedDistance) - Math.sin(currentLatitude)
                    * Math.sin(expectedLatitude));
    double expectedLongitude = longitude1 + a;
    expectedLongitude = (expectedLongitude + 3 * Math.PI) % (2 * Math.PI)
            - Math.PI;

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

    // log everything for testing.
    Log.d("Location", "Bearing in radians" + bearing);
    Log.d("Location", "distance in km" + expectedDistance);
    Log.d("Location", "Current Latitude = " + location.getLatitude()
            + " Current Longitude = " + location.getLongitude());
    Log.d("Location", "New Latitude = " + expectedLatitudeDestination
            + " New Longitude = " + expectedLongitudeDestination);

    // build a camera update to animate positioning map to the expected
    // destination
    LatLng ll = new LatLng(expectedLatitudeDestination,
            expectedLongitudeDestination);
    CameraPosition.Builder cb = CameraPosition.builder()
            .zoom(mMap.getCameraPosition().zoom)
            .bearing(mMap.getCameraPosition().bearing)
            .tilt(mMap.getCameraPosition().tilt).target(ll);
    if (location.hasBearing()) {
        cb.bearing(location.getBearing());
    }
    CameraPosition camera = cb.build();
    CameraUpdate update = CameraUpdateFactory.newCameraPosition(camera);
    mMap.animateCamera(update, interval, this);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!