示例中心
功能在线体验
menu 导航 closed 关闭

定位图标指示方向

该demo展示了定位图标方向跟随手机指向转动的效果。
00:00 / 01:00
扫描二维码在手机端体验功能
体验移动端 扫码体验移动端

使用场景

让地图中的定位图标能够识别&指示方向。

需要用户移动位置的app,尤其是用到路径规划、导航的app中,可以增加该指示效果,以便app用户能够分清移动方向。

用到产品

Android 地图SDK Android 定位 SDK

核心类/接口

接口

说明

版本

AMap

setLocationSource(LocationSource locationSource)

设置定位资源

V2.0.0版本起

AMap

addMarker(MarkerOptions options)

加一个Marker(标记)到地图上

V2.0.0版本起

AMap

addCircle(CircleOptions options)

添加圆形(circle)覆盖物到地图上

V2.0.0版本起

AMapLocationClient

startLocation()

启动定位

V2.0.0版本起

AMapLocationClient

setLocationOption(mLocationOption)

给定位客户端设置参数

V2.0.0版本起

AMapLocationListener

onLocationChanged(AMapLocation amapLocation)

监听器回调方法

V2.0.0版本起

核心难点

1、获取手机sensor角度值并过滤

@Override
public void onSensorChanged(SensorEvent event) {
    if (System.currentTimeMillis() - lastTime < TIME_SENSOR) {
        return;
    }
    switch (event.sensor.getType()) {
        case Sensor.TYPE_ORIENTATION: {
            float x = event.values[0];
            x += getScreenRotationOnPhone(mContext);
            x %= 360.0F;
            if (x > 180.0F)
                x -= 360.0F;
            else if (x < -180.0F)
                x += 360.0F;

            if (Math.abs(mAngle - x) < 3.0f) {
                break;
            }
            mAngle = Float.isNaN(x) ? 0 : x;
            if (mMarker != null) {
                mMarker.setRotateAngle(360 - mAngle);
            }
            lastTime = System.currentTimeMillis();
        }
    }
}

2、获取当前手机屏幕旋转角度

/**
 * 获取当前屏幕旋转角度
 *
 * @param context
 * @return 0表示是竖屏; 90表示是左横屏; 180表示是反向竖屏; 270表示是右横屏
 */
public static int getScreenRotationOnPhone(Context context) {
    final Display display = ((WindowManager) context
            .getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();

    switch (display.getRotation()) {
        case Surface.ROTATION_0:
            return 0;

        case Surface.ROTATION_90:
            return 90;

        case Surface.ROTATION_180:
            return 180;

        case Surface.ROTATION_270:
            return -90;
    }
    return 0;
}

3、定位回调成功处理Marker与Circle绘制

if (!mFirstFix) {
    mFirstFix = true;
    addCircle(location, amapLocation.getAccuracy());//添加定位精度圆
    addMarker(location);//添加定位图标
    mSensorHelper.setCurrentMarker(mLocMarker);//定位图标旋转
    aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(location,18));
} else {
    mCircle.setCenter(location);
    mCircle.setRadius(amapLocation.getAccuracy());
    mLocMarker.setPosition(location);
    aMap.moveCamera(CameraUpdateFactory.changeLatLng(location));
}
00:00 / 01:00
扫描二维码在手机端体验功能
体验移动端 扫码体验移动端

使用场景

让地图中的定位图标能够识别&指示方向。

需要用户移动位置的app,尤其是用到路径规划、导航的app中,可以增加该指示效果,以便app用户能够分清移动方向。

用到产品

iOS 地图SDK iOS 定位 SDK

核心类/接口

接口

说明

版本

LocationAnnotationView

rotateDegree

继承自MAAnnotationView

----

核心难点

定位回调

- (void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation
{
    if (!updatingLocation && _locationAnnotationView != nil)
    {
        [UIView animateWithDuration:0.1 animations:^{
            _locationAnnotationView.rotateDegree = userLocation.heading.trueHeading - _mapView.rotationDegree;
        }];
    }

    if (_record.latitude != userLocation.coordinate.latitude || _record.longitude != userLocation.coordinate.longitude) {
        NSLog(@"changed");
        _record = userLocation.coordinate;
    }
}
func mapView(_ mapView: MAMapView!, didUpdate userLocation: MAUserLocation!, updatingLocation: Bool) {
    if !updatingLocation && _locationAnnotationView != nil {
        UIView.animate(withDuration: 0.1, animations: { 
        self._locationAnnotationView.rotateDegree = CGFloat(userLocation.heading.trueHeading) - mapView.rotationDegree
        })
    }

    if _record.latitude != userLocation.coordinate.latitude || _record.longitude != userLocation.coordinate.longitude {
        _record = userLocation.coordinate
    }
}