替换默认的 Android Maps API v2 更改 MyLocation 图标 [英] Replace default Android Maps API v2 Change MyLocation icon

查看:21
本文介绍了替换默认的 Android Maps API v2 更改 MyLocation 图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用我自己的图片替换 Android Maps V2 用于我的位置"的默认图标.我创建了自己的图块提供程序,其中引入了一些主要是蓝色的地图,因此默认的我的位置"图标(蓝色小箭头)很难看到.

I would like to replace the default icon that Android Maps V2 uses for 'My Location' with my own image. I've created my own tile provider that brings in a few maps which are predominantly blue and as such the default My Location icon, the little blue arrow, is very hard to see.

以前我只是覆盖了 MyLocationOverlay 的 draw 方法,但新 API 中似乎没有.

Previously I would have just overridden the draw method of the MyLocationOverlay, but there doesn't seem to be one in the new API.

我还需要图标能够旋转,与箭头的旋转方式相同,具体取决于您面向的方向.所以我不能只使用普通的标记.基本上我只需要为那个箭头创建一个自定义图像.

I also need the icon to be able to rotate, the same way that the arrow does depending on which way you are facing. So I can't just use a normal marker. Basically I just need to create a custom image for that arrow.

推荐答案

Google Play 服务的最新更新现在允许您使用平面"标记并旋转它们,这正是我所需要的.这是我实现它的方式的一个简单版本.我可能可以做很多事情来优化和调整动画,但目前它可以完成这项工作.欢迎任何反馈.

The latest update to Google Play Services now allows you to use 'Flat' Markers and rotate them which is exactly what I needed. Here is a simple version of the way I implemented it. There is probably a fair bit I can do to optimize and tweak the animation, but it does the job for the moment. Any feedback is welcome.

Marker mPositionMarker;
GoogleMap mMap;

@Override
public void onLocationChanged(Location location) {

    if (location == null)
        return;

    if (mPositionMarker == null) {

        mPositionMarker = mMap.addMarker(new MarkerOptions()
                .flat(true)
                .icon(BitmapDescriptorFactory
                        .fromResource(R.drawable.positionIndicator))
                .anchor(0.5f, 0.5f)
                .position(
                        new LatLng(location.getLatitude(), location
                                .getLongitude())));
    }

    animateMarker(mPositionMarker, location); // Helper method for smooth
                                                // animation

    mMap.animateCamera(CameraUpdateFactory.newLatLng(new LatLng(location
            .getLatitude(), location.getLongitude())));

}

public void animateMarker(final Marker marker, final Location location) {
    final Handler handler = new Handler();
    final long start = SystemClock.uptimeMillis();
    final LatLng startLatLng = marker.getPosition();
    final double startRotation = marker.getRotation();
    final long duration = 500;

    final Interpolator interpolator = new LinearInterpolator();

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

            double lng = t * location.getLongitude() + (1 - t)
                    * startLatLng.longitude;
            double lat = t * location.getLatitude() + (1 - t)
                    * startLatLng.latitude;

            float rotation = (float) (t * location.getBearing() + (1 - t)
                    * startRotation);

            marker.setPosition(new LatLng(lat, lng));
            marker.setRotation(rotation);

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

这篇关于替换默认的 Android Maps API v2 更改 MyLocation 图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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