路线上的可绘制图标未固定在基座上 [英] Drawable icons on route not pinned at base

查看:81
本文介绍了路线上的可绘制图标未固定在基座上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将可绘制图像用于路线上的标记图标.图像的底部不在该点出现,而是在中间出现.
可以解决吗?

I am using drawable images for marker icons on a route. The base of the image does not appear at the point but rather more in the middle.
Can this be addressed?

Double latitude = new Double(getString(R.string.sagrada_latitude));
Double longitude = new Double(getString(R.string.sagrada_longitude));
final Position origin = Position.fromCoordinates(longitude, latitude);

latitude = new Double(getString(R.string.mataro_latitude));
longitude = new Double(getString(R.string.mataro_longitude));
final Position destination = Position.fromCoordinates(longitude, latitude); 

// Create an Icon object for the marker to use
IconFactory iconFactory = IconFactory.getInstance(this);
Drawable iconDrawable = ContextCompat.getDrawable(this, R.drawable.green_pin);
final Icon greenPinIcon = iconFactory.fromDrawable(iconDrawable);
iconDrawable = ContextCompat.getDrawable(this, R.drawable.red_pin);
final Icon redPinIcon = iconFactory.fromDrawable(iconDrawable);

// Setup the MapView
mapView = (MapView) findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(new OnMapReadyCallback() {
    @Override
    public void onMapReady(MapboxMap mapboxMap) {
        map = mapboxMap;

        // Add origin and destination to the map
        LatLng originLatLng = (new LatLng(origin.getLatitude(), origin.getLongitude()));
        mapboxMap.addMarker(new MarkerOptions()
                .position(originLatLng)
                .title("Origin")
                .snippet("current location: (" + origin.getLatitude() + ", " + origin.getLongitude() + ")")
                .icon(greenPinIcon));

        Log.d(TAG, "getMapAsync(): destination: (" + destination.getLatitude() + ", " + destination.getLongitude() + ")");
        LatLng destinationLatLng = (new LatLng(destination.getLatitude(), destination.getLongitude()));
        mapboxMap.addMarker(new MarkerOptions()
                .position(destinationLatLng)
                .title("Destination")
                .snippet("destination: (" + destination.getLatitude() + ", " + destination.getLongitude() + ")")
                .icon(redPinIcon));
            mapboxMap.easeCamera(CameraUpdateFactory.newLatLngBounds(latLngBounds, 50), 5000);

            // Get route from API
            try {
                getRoute(origin, destination);
            }
            catch (ServicesException servicesException) {
                Log.e(TAG, servicesException.toString());
                servicesException.printStackTrace();
            }
        }
    });
}

private void getRoute(Position origin, Position destination) throws ServicesException {

    client = new MapboxDirections.Builder()
            .setOrigin(origin)
            .setDestination(destination)
            .setProfile(DirectionsCriteria.PROFILE_CYCLING)
            .setAccessToken(MapboxAccountManager.getInstance().getAccessToken())
            .build();

    client.enqueueCall(new Callback<DirectionsResponse>() {
        @Override
        public void onResponse(Call<DirectionsResponse> call, Response<DirectionsResponse> response) {
            // You can get the generic HTTP info about the response
            Log.d(TAG, "Response code: " + response.code());
            if (response.body() == null) {
                Log.e(TAG, "No routes found, make sure you set the right user and access token.");
                return;
            } else if (response.body().getRoutes().size() < 1) {
                Log.e(TAG, "No routes found");
                return;
            }

            // Print some info about the route
            currentRoute = response.body().getRoutes().get(0);
            Log.d(TAG, "Distance: " + currentRoute.getDistance());
            Double km = currentRoute.getDistance() / 1000;
            // there are 4 digits to the right of the decimal, make it 2
            String kilometers = km.toString();
            int index = kilometers.lastIndexOf(".");
            kilometers = kilometers.substring(0, index + 3);
            Toast.makeText(
                    DirectionsActivity.this,
                    "Route is " + kilometers + " kilometers",
                    Toast.LENGTH_SHORT).show();

            // Draw the route on the map
            drawRoute(currentRoute);
        }

        @Override
        public void onFailure(Call<DirectionsResponse> call, Throwable throwable) {
            Log.e(TAG, "Error: " + throwable.getMessage());
            Toast.makeText(DirectionsActivity.this, "Error: " + throwable.getMessage(), Toast.LENGTH_SHORT).show();
        }
    });
}

另一个问题... Position.fromCoordinates方法:

A side question ... the Position.fromCoordinates method:

private Position(double longitude, double latitude, double altitude) 

按照经度然后按纬度的顺序接受参数,而不是像人们期望的那样按纬度然后按经度的顺序.为什么?

takes the arguments in order of longitude then latitude, not latitude then longitude as one might expect. Why?


MarkerOptions更改为MarkerViewOptions,图标移得更远.还尝试了.anchor(0,0)无效.


Changed MarkerOptions to MarkerViewOptions and the icons moved even further away. Also tried .anchor(0,0) which had no effect.

此外,默认图标(已关闭):

Also, with default Icons (which are off):

图标:

// Mapbox dependencies  
compile('com.mapbox.mapboxsdk:mapbox-android-sdk:4.1.1@aar') {  
    transitive = true  
}  
compile ('com.mapbox.mapboxsdk:mapbox-android-directions:1.0.0@aar'){  
    transitive=true  
}  
compile('com.mapbox.mapboxsdk:mapbox-android-services:1.3.1@aar') {  
    transitive = true  
}  

推荐答案

您需要在标记图标png的底部添加填充,或者更好的选择是使用MarkerViewOptions().与您当前使用的GL标记(包括锚点)相比,它们提供了更多选项.默认情况下,锚点位于底部中央.因此,其中一个标记将如下所示:

You either need to add padding to the bottom of the marker icon png or a better option would be using MarkerViewOptions() instead. They give more options then the GL markers your currently using including anchor. By default the anchoring is center bottom. So one of you markers would look like this:

mapboxMap.addMarker(new MarkerViewOptions()
            .position(destinationLatLng)
            .title("Destination")
            .snippet("destination: (" + destination.getLatitude() + ", " + destination.getLongitude() + ")")
            .icon(redPinIcon));

要回答您的另一个问题,为什么位置按该顺序采用经度,纬度,许多Mapbox API均按该顺序使用坐标.更大的问题是,在Map SDK中已经找到LatLng时,为什么Position对象存在?这是因为对象会冲突,因为它们是在单独的SDK中找到的,但通常会一起使用.这是我们期待在不久的将来发生变化的事情.

To answer your other question, why position takes in longitude, latitude in that order, many of the Mapbox APIs consume coordinates in that order. The bigger question is why does the Position object exist when LatLng is found already in the Map SDK? This is because the objects would conflict since they are found in separate SDKs yet are typically used together. It is something we look forward to changing in the near future.

编辑:首先,您需要删除mapbox-android-directions,这是我们不建议使用的旧的,不支持的SDK.它是Mapbox Android服务(MAS)的替代产品,并使用Mapbox Directions V5.使用此示例,该示例显示了如何使用MAS和将标记添加到地图.使用在问题中找到的坐标,结果看起来像这样:

first you need to remove the mapbox-android-directions, this is an old, non supported, SDK we have deprecated. Mapbox Android Services (MAS) is it's replacement and uses Mapbox Directions V5. Use this example which shows how to properly make a directions call using MAS and add the markers to the map. Using the coordinates found in your question, the result looks like this:

这篇关于路线上的可绘制图标未固定在基座上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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