Mapbox Android: как проложить маршрут от текущего местоположения до выбранного вами пункта назначения?

Хорошо, я новичок в Mapbox, я использовал GMaps до этого, но я обнаружил, что Mapbox более способен делать то, что мне нужно, проблема в том, что я немного наткнулся на стену.

Я использовал комбинацию примеров, доступных на их сайте, например.

https://www.mapbox.com/android-sdk/examples/geocoding и https://www.mapbox.com/android-sdk/examples/directions

Я пытаюсь разрешить пользователю искать пункт назначения, а затем преобразовать его в координаты, которые будут использоваться для построения на карте. Затем я хочу нарисовать маршрут от их текущего местоположения до места назначения, вот где возникает моя проблема.

 mapView.getMapAsync(new OnMapReadyCallback() {
        @Override
        public void onMapReady(MapboxMap mapboxMap) {

            map = mapboxMap;


            // Set the origin waypoint to the devices location
            Position origin = Position.fromCoordinates(mapboxMap.getMyLocation().getLongitude(), mapboxMap.getMyLocation().getLatitude());

            // Set the destination waypoint to the location point long clicked by the user
            final Position destination = updateMap(feature.getLongitude(), feature.getLatitude());

            mapboxMap.addMarker(new MarkerOptions()
                    .position(new LatLng(origin.getLatitude(), origin.getLongitude()))
                    .title("Origin")
                    .snippet("Alhambra"));
            mapboxMap.addMarker(new MarkerOptions()
                    .position(new LatLng(latitude, destination.getLongitude()))
                    .title("Destination")
                    .snippet("Plaza del Triunfo"));

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

    AndroidGeocoder geocoder = new AndroidGeocoder(context, Locale.getDefault());
    geocoder.setAccessToken(MAPBOX_ACCESS_TOKEN);
    addresses = geocoder.getFromLocation(
            location.getLatitude(),
            location.getLongitude(),
            1);

    // Set up autocomplete widget
    GeocoderAutoCompleteView autocomplete = (GeocoderAutoCompleteView) findViewById(R.id.query);
    autocomplete.setAccessToken("pk.eyJ1IjoiYmV1dHJveCIsImEiOiJjaW5ybzlwYnQwMGlqdnhtMno3cmtwNTlqIn0.y16mZzmertL4-eEfQNGeqQ");
    autocomplete.setType(GeocodingCriteria.TYPE_POI);
    autocomplete.setOnFeatureListener(new GeocoderAutoCompleteView.OnFeatureListener() {
        @Override
        public void OnFeatureClick(GeocodingFeature feature) {
            Position position = feature.asPosition();
            updateMap(position.getLatitude(), position.getLongitude());
        }
    });
}


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

    MapboxDirections client = new MapboxDirections.Builder()
            .setOrigin(origin)
            .setDestination(destination)
            .setProfile(DirectionsCriteria.PROFILE_CYCLING)
            .setAccessToken("pk.eyJ1IjoiYmV1dHJveCIsImEiOiJjaW5ybzlwYnQwMGlqdnhtMno3cmtwNTlqIn0.y16mZzmertL4-eEfQNGeqQ")
            .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;
            }

            // Display some info about the route
            currentRoute = response.body().getRoutes().get(0);
            Log.d(TAG, "Distance: " + currentRoute.getDistance());
            Toast.makeText(MainActivity.this, "Route is " +  currentRoute.getDistance() + " meters long.", Toast.LENGTH_SHORT).show();

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

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

private void drawRoute(DirectionsRoute route) {
    // Convert LineString coordinates into LatLng[]
    LineString lineString = LineString.fromPolyline(route.getGeometry(), Constants.OSRM_PRECISION_V5);
    List<Position> coordinates = lineString.getCoordinates();
    LatLng[] points = new LatLng[coordinates.size()];
    for (int i = 0; i < coordinates.size(); i++) {
        points[i] = new LatLng(
                coordinates.get(i).getLatitude(),
                coordinates.get(i).getLongitude());
    }

    // Draw Points on MapView
    map.addPolyline(new PolylineOptions()
            .add(points)
            .color(Color.parseColor("#009688"))
            .width(5));

}

private void myLocation() {

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.
        return;
    }

    mapView.setMyLocationEnabled(true);
    mapView.setMyLocationTrackingMode(MyLocationTracking.TRACKING_FOLLOW);
    mapView.getMyLocation();
}

Итак, где-то здесь моя проблема, я не могу найти много информации о Mapbox в Интернете, поскольку их новый sdk сильно отличается от предыдущих версий, в основном потому, что MapView и MapboxMap теперь разделены (это все еще меня смущает).

Любая помощь будет принята с благодарностью: D

: Edit: Я борюсь в основном с установкой пункта назначения, и из-за этого многие мои текущие переменные перепутаны. Если вам нужно объяснение, я с радостью сделаю это.


person Brian    schedule 10.05.2016    source источник
comment
вставьте свой логарифм, пожалуйста   -  person Oussema Aroua    schedule 11.05.2016
comment
Мой logcat, похоже, не работает ... Отладчик больше не активен, это то, что он говорит, но вот что говорит моя вкладка сообщений: Ошибка: (77, 56) ошибка: не удается найти функцию символьной переменной Ошибка: (77, 80 ) ошибка: не удается найти символьную переменную.   -  person Brian    schedule 11.05.2016
comment
Ошибка: (100, 17) ошибка: не удается найти расположение символьной переменной Ошибка: (101, 17) ошибка: не удается найти расположение символьной переменной Ошибка: (186, 16) ошибка: setMyLocationEnabled (логическое значение) не является общедоступным в MapView; недоступен извне пакета. Ошибка: (187, 16) ошибка: setMyLocationTrackingMode (int) не является общедоступным в MapView; недоступен извне пакета. Ошибка: (188, 16) ошибка: getMyLocation () не является общедоступным в MapView; недоступен из внешнего пакета: app: compileDebugJavaWithJavac FAILED Ошибка: Выполнение не выполнено для задачи ': app: compileDebugJavaWithJavac'.   -  person Brian    schedule 11.05.2016
comment
вы пробовали очистить и пересобрать проект?   -  person Oussema Aroua    schedule 11.05.2016
comment
Хммм, я пробовал это раньше, и это не сработало, но теперь оно есть, пожалуйста :) txt.do/5brjg   -  person Brian    schedule 11.05.2016


Ответы (1)


Несколько ошибок в вашем коде, самая большая из которых заключается в том, что вы все еще используете MapView для получения информации о местоположении, хотя вместо этого вы можете использовать MapboxMap. Вот примерный код, который сделает то, о чем вы просите. Он не проверяет права пользователя, как вы должны, но он покажет вам, как делать вещи Mapbox.

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.mapbox.mapboxsdk.annotations.MarkerOptions;
import com.mapbox.mapboxsdk.annotations.PolylineOptions;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.maps.MapView;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
import com.mapbox.services.Constants;
import com.mapbox.services.android.geocoder.ui.GeocoderAutoCompleteView;
import com.mapbox.services.commons.ServicesException;
import com.mapbox.services.commons.geojson.LineString;
import com.mapbox.services.commons.models.Position;
import com.mapbox.services.directions.v5.DirectionsCriteria;
import com.mapbox.services.directions.v5.MapboxDirections;
import com.mapbox.services.directions.v5.models.DirectionsResponse;
import com.mapbox.services.directions.v5.models.DirectionsRoute;
import com.mapbox.services.geocoding.v5.GeocodingCriteria;
import com.mapbox.services.geocoding.v5.models.GeocodingFeature;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MainActivity extends Activity {

    private final static String TAG = "MainActivity";

    private MapView mapView;
    private MapboxMap map;
    private DirectionsRoute currentRoute;

    private Position origin;
    private Position destination;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

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

                mapboxMap.setMyLocationEnabled(true);

                // Set up autocomplete widget
                GeocoderAutoCompleteView autocomplete = (GeocoderAutoCompleteView) findViewById(R.id.query);
            autocomplete.setAccessToken("<your access token>");
            autocomplete.setType(GeocodingCriteria.TYPE_POI);
            autocomplete.setOnFeatureListener(new GeocoderAutoCompleteView.OnFeatureListener() {
                @Override
                public void OnFeatureClick(GeocodingFeature feature) {

                    if(map.getMyLocation() != null) {
                        // Set the origin as user location only if we can get their location
                        origin = Position.fromCoordinates(map.getMyLocation().getLongitude(), map.getMyLocation().getLatitude());
                    }else{
                        return;
                    }

                    destination = feature.asPosition();

                    // Add origin and destination to the map
                    map.addMarker(new MarkerOptions()
                            .position(new LatLng(origin.getLatitude(), origin.getLongitude())));
                    map.addMarker(new MarkerOptions()
                            .position(new LatLng(destination.getLatitude(), destination.getLongitude())));

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

                }
            });
        }
    });
}

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

    MapboxDirections client = new MapboxDirections.Builder()
            .setOrigin(origin)
            .setDestination(destination)
            .setProfile(DirectionsCriteria.PROFILE_CYCLING)
            .setAccessToken("<your access token>")
            .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;
            }

            // Print some info about the route
            currentRoute = response.body().getRoutes().get(0);
            Log.d(TAG, "Distance: " + currentRoute.getDistance());
            Toast.makeText(MainActivity.this, "Route is " +  currentRoute.getDistance() + " meters long.", Toast.LENGTH_SHORT).show();

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

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

private void drawRoute(DirectionsRoute route) {
    // Convert LineString coordinates into LatLng[]
    LineString lineString = LineString.fromPolyline(route.getGeometry(), Constants.OSRM_PRECISION_V5);
    List<Position> coordinates = lineString.getCoordinates();
    LatLng[] points = new LatLng[coordinates.size()];
    for (int i = 0; i < coordinates.size(); i++) {
        points[i] = new LatLng(
                coordinates.get(i).getLatitude(),
                coordinates.get(i).getLongitude());
    }

    // Draw Points on MapView
    map.addPolyline(new PolylineOptions()
            .add(points)
            .color(Color.parseColor("#009688"))
            .width(5));
}

@Override
public void onResume() {
    super.onResume();
    mapView.onResume();
}

@Override
public void onPause() {
    super.onPause();
    mapView.onPause();
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    mapView.onSaveInstanceState(outState);
}

@Override
protected void onDestroy() {
    super.onDestroy();
    mapView.onDestroy();
}

@Override
public void onLowMemory() {
    super.onLowMemory();
    mapView.onLowMemory();
}

Надеюсь это поможет!

person cammace    schedule 11.05.2016
comment
СПАСИБО, попробую и дам знать: D - person Brian; 12.05.2016
comment
К сожалению, он работает отлично, но, похоже, мое геокодирование не работает в глобальном масштабе. К сожалению, я не могу протестировать его в своей стране. Есть ли у вас какие-нибудь предложения? - person Brian; 12.05.2016
comment
Хорошо, еще раз спасибо, в настоящее время я живу в Южной Африке. Там написано "Места". Означает ли это, что поиск адресов в Южной Африке не даст результатов? - person Brian; 12.05.2016
comment
Есть ли у вас какие-нибудь предложения, как я могу это обойти? - person Brian; 12.05.2016