路线上的可绘制图标不固定在基地

时间:2016-10-31 21:39:21

标签: android mapbox

我在路线上使用可绘制图像作为标记图标。图像的底部不会出现在该点,而是出现在中间 这可以解决吗?

Route

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方法:

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

按照经度和纬度的顺序获取参数,而不是纬度,然后是人们可能期望的经度。为什么?

编辑:
MarkerOptions更改为MarkerViewOptions,图标移动得更远。还尝试了.anchor(0,0)没有效果。

MarkerViewOptions

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

Default Icons

图标: Red Pin

// 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  
}  

1 个答案:

答案 0 :(得分:0)

您需要在标记图标png的底部添加填充,否则更好的选项将使用MarkerViewOptions()。它们提供了比您当前使用的GL标记更多的选项,包括锚点。默认情况下,锚定位于中心底部。所以你们中的一个标记看起来像这样:

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

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

编辑:首先您需要删除mapbox-android-directions,这是一个旧的,不受支持的SDK,我们已弃用。 Mapbox Android Services(MAS)是它的替代品并使用Mapbox Directions V5。使用this example显示如何使用MAS正确拨打电话,并将标记添加到地图中。使用问题中的坐标,结果如下:

enter image description here