2013-12-20 63 views
9

我想根据从加速度传感器接收到的方位或传感器值旋转标记,以向用户显示实际移动的位置。我已将标记图标和平面值设置为true,但不能按要求运行。根据用户指示在Google Maps V2上旋转标记Android

mCurrentLocationMarker.position(new LatLng(
          LocationUtils.sLatitude, LocationUtils.sLongitude)); 
        mCurrentLocationMarker.icon(icon); 
        mCurrentLocationMarker.flat(true); 
        mCurrentLocationMarker.rotation(LocationUtils.sBearing); 

        if (currentMarker != null) { 
         currentMarker.setPosition(new LatLng(
           LocationUtils.sLatitude, 
           LocationUtils.sLongitude)); 
        } else { 
         currentMarker = mGoogleMap 
           .addMarker(mCurrentLocationMarker); 
        } 
        animateCameraTo(true); 

我已经使用这个marker作为标记。

我不知道为什么它不按照用户的方向旋转。如果有人有任何想法,请在我犯错的地方帮助我。

LocationUtils.sBearing是我从onLocationChanged或加速度计收到的方位的值。

基本上我想让我的标记与谷歌地图标记相同,它显示用户在哪个方向移动或转动。

+0

嘘声!!!任何回答这个或https://stackoverflow.com/questions/33687236/rotate-marker-as-per-user-direction-on-google-maps-v2-android ????? – Stella

回答

7

这是一个老问题,看起来API自那时以来已经发生了变化。

我假设你能够获得设备承载。如果这里不是handy tutorial

首先要创建一个我们可以用于轴承更新的标记。

private Marker marker; 

// Create this marker only once; probably in your onMapReady() method 
marker = mGoogleMap.addMarker(new MarkerOptions() 
     .position(new LatLng(myLatitude, myLongitude)) 
     .flat(true)); 

注意.flat(true)部分。确保我们的标记向北对齐,以便即使用户旋转地图,我们的轴承也能正常工作。

现在,当您得到您的轴承更新,你可以做以下

marker.setRotation(bearing); 
// or if following the linked tutorial 
// marker.setRotation((float) azimuth); 

这里假设你的标记图标在顶部的前进方向。如果您的标记像图中那样旋转,则在将其设置到标记之前,您必须调整轴承进行补偿。只是一个简单的setRotation(bearing - 45)应该这样做。

+0

'如果(mLastLocation.hasBearing()){marker.setRotation(轴承); “对吧? – Stella

+0

@Stella如果那是你从哪里得到你的方位,当然 – Breavyn

4

Im发布了此答案,因为像我这样的人正在寻找与上述问题相关的解决方案可能会发现它很有用。

所以在这里,我是如何做到的。

由于@colin表示您必须启用.flat(true)来旋转标记。

1.对于方位角我使用了下面的代码。

这里latLng1 - 我的旧位置& & latLng2 - 我的新位置

private double bearingBetweenLocations(LatLng latLng1,LatLng latLng2) { 

     double PI = 3.14159; 
     double lat1 = latLng1.latitude * PI/180; 
     double long1 = latLng1.longitude * PI/180; 
     double lat2 = latLng2.latitude * PI/180; 
     double long2 = latLng2.longitude * PI/180; 

     double dLon = (long2 - long1); 

     double y = Math.sin(dLon) * Math.cos(lat2); 
     double x = Math.cos(lat1) * Math.sin(lat2) - Math.sin(lat1) 
       * Math.cos(lat2) * Math.cos(dLon); 

     double brng = Math.atan2(y, x); 

     brng = Math.toDegrees(brng); 
     brng = (brng + 360) % 360; 

     return brng; 
    } 

2.To使用上述方位角我已经使用这个代码

这里isMarkerRotating是一个布尔值旋转标记。在OnCreate方法

private void rotateMarker(final Marker marker, final float toRotation) { 
     if(!isMarkerRotating) { 
      final Handler handler = new Handler(); 
      final long start = SystemClock.uptimeMillis(); 
      final float startRotation = marker.getRotation(); 
      final long duration = 2000; 

      final Interpolator interpolator = new LinearInterpolator(); 

      handler.post(new Runnable() { 
       @Override 
       public void run() { 
        isMarkerRotating = true; 

        long elapsed = SystemClock.uptimeMillis() - start; 
        float t = interpolator.getInterpolation((float) elapsed/duration); 

        float rot = t * toRotation + (1 - t) * startRotation; 

        float bearing = -rot > 180 ? rot/2 : rot; 

        marker.setRotation(bearing); 

        if (t < 1.0) { 
         // Post again 16ms later. 
         handler.postDelayed(this, 16); 
        } else { 
         isMarkerRotating = false; 
        } 
       } 
      }); 
     } 
    } 

3添加isMarkerRotating = false。使用上述代码

LatLng oldLocation, newLocaation; 

float bearing = (float) bearingBetweenLocations(oldLocation, newLocaation); 
rotateMarker(start_marker, bearing); 
相关问题