首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >未固定在基地的路线上的可绘制图标

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

Stack Overflow用户
提问于 2016-11-01 05:39:21
回答 1查看 281关注 0票数 0

我正在使用可绘制的图像作为路线上的标记图标。图像的底部不会出现在点上,而是更多地出现在中间。

这个问题能解决吗?

代码语言:javascript
运行
复制
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方法:

代码语言:javascript
运行
复制
private Position(double longitude, double latitude, double altitude) 

按照经度和纬度的顺序获取参数,而不是按照预期的纬度和经度的顺序。为什么?

编辑:

MarkerOptions更改为MarkerViewOptions,图标将移动到更远的位置。我也尝试了.anchor(0,0),但没有效果。

此外,使用默认图标(处于禁用状态):

图标:

代码语言:javascript
运行
复制
// 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  
}  
EN

回答 1

Stack Overflow用户

发布于 2016-11-03 05:23:48

您需要在标记图标png的底部添加填充,或者更好的选择是使用MarkerViewOptions()。它们提供了比你当前使用的GL标记更多的选项。默认情况下,锚点位于中心底部。因此,其中一个标记应该是这样的:

代码语言:javascript
运行
复制
mapboxMap.addMarker(new MarkerViewOptions()
            .position(destinationLatLng)
            .title("Destination")
            .snippet("destination: (" + destination.getLatitude() + ", " + destination.getLongitude() + ")")
            .icon(redPinIcon));

为了回答您的另一个问题,为什么位置以经度、纬度为顺序,许多Mapbox API都按此顺序使用坐标。更大的问题是,当LatLng已经在Map SDK中找到时,为什么Position对象还存在?这是因为对象会冲突,因为它们在单独的SDK中找到,但通常一起使用。这是我们期待在不久的将来改变的东西。

EDIT:首先你需要删除mapbox-android-directions,这是一个旧的,不受支持的,我们已经弃用了。Mapbox Android Services是它的替代品,它使用Mapbox Directions V5。使用this example,它展示了如何使用MAS正确地发出方向呼叫,并将标记添加到地图中。使用问题中找到的坐标,结果如下所示:

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40351003

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档