2016-11-27 135 views
0

作爲用戶,我想使用android應用程序來使用路線在地圖中導航。一旦我走上了路線,我會開發其他方式來組織導航,但是到目前爲止,我無法在我的地圖中顯示路線。Mapbox(android)不顯示路線

我使用本教程由Mapbox自己給出:https://www.mapbox.com/android-sdk/examples/directions/。沒有消息錯誤,事實上,在我訪問地圖視圖後,顯示路線距離的Toast消息是可見的。 ("Route is 0.0 meters long.")我檢查了日誌,通用的HTML代碼是200(這是好的),調試信息顯示一條0.0米長的路線。除此之外,日誌中沒有發現錯誤。

我已經仔細檢查了一個星期的代碼,我發現了任何錯誤,但是我沒有發現任何可能導致錯誤不顯示路線的內容。我也調試了代碼,並且它沒有任何問題地到達drawRoute函數,我也檢查了令牌,它工作得很好。

我做了什麼不能顯示路線?非常感謝!

Image example when you try to see a route.

Java代碼:

public class Mapa extends Activity { 
private static final String TAG = "Mapa"; //Tag para usar no log de debug e error 

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

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    // Mapbox usa esse token de acesso atraves de uma conta do mapbox, so precisa ser configurado uma vez! 
    MapboxAccountManager.start(this,getString(R.string.access_token)); 

    // Contem o mapview exposto em XML e precisa ser chamado LOGO APOS o account manager 
    setContentView(R.layout.activity_main); 

    // Funcao que pede permissao para o usuario no seu celular -- TODO 
    PedePermissao(); 

    // Origem: Rodoviaria 
    final Position origin = Position.fromCoordinates(-15.794082, -47.882645); 
    // Destino: Reitoria 
    final Position destination = Position.fromCoordinates(-15.762604, -47.866611); 


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

      // Criacao e declaracao do icone 
      IconFactory iconFactory = IconFactory.getInstance(Mapa.this); 
      Drawable iconDrawable = ContextCompat.getDrawable(Mapa.this, R.drawable.infoicon); 
      Icon icon = iconFactory.fromDrawable(iconDrawable); 
      Drawable iconeDrawableRota = ContextCompat.getDrawable(Mapa.this, R.drawable.infoicon_rota); 
      Icon iconeRota = iconFactory.fromDrawable(iconeDrawableRota); 

      //Marcadores das instalacoes 
      mapboxMap.addMarker(new MarkerViewOptions() 
        .position(new LatLng(-15.76363, -47.86949)) 
        .title("ICC Centro") 
        .snippet(getString(R.string.desc_ICC_Centro)) 
        .icon(icon)); 

      mapboxMap.addMarker(new MarkerViewOptions() 
        .position(new LatLng(-15.7629936, -47.86717415)) 
        .title("Reitoria") 
        .snippet(getString(R.string.desc_Reitoria)) 
        .icon(icon)); 

      mapboxMap.addMarker(new MarkerViewOptions() 
        .position(new LatLng(-15.76196106, -47.87008166)) 
        .title("FAU") 
        .snippet(getString(R.string.desc_FAU)) 
        .icon(icon)); 

      // Adicionando os marcadores de destino da rota 
      mapboxMap.addMarker(new MarkerOptions() 
        .position(new LatLng(origin.getLatitude(), origin.getLongitude())) 
        .title("Origem") 
        .icon(iconeRota)); 
      mapboxMap.addMarker(new MarkerOptions() 
        .position(new LatLng(destination.getLatitude(), destination.getLongitude())) 
        .title("Destino") 
        .icon(iconeRota)); 

      try { 
       getRoute(origin, destination); 
      } catch (ServicesException servicesException) { 
       servicesException.printStackTrace(); 
      } 

      //Dados da janela (infowindow) quando clicar em um ponteiro (marker) 
      mapboxMap.setInfoWindowAdapter(new MapboxMap.InfoWindowAdapter() { 
        //... 
       } 
      }); 
     } 
    }); 
} 

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

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


    client.enqueueCall(new Callback<DirectionsResponse>() { 
     @Override 
     public void onResponse(Call<DirectionsResponse> call, Response<DirectionsResponse> response) { 

      // Voce pode pegar mensagens de erro genericas do http sobre o response 

      Log.d(TAG, "Codigo de resposta: " + response.code()); 
      if (response.body() == null) { 
       Log.e(TAG, "Sem rotas encontradas, porém sem o uso do token, verifique o token de acesso"); 
       return; 
      } else if (response.body().getRoutes().size() < 1) { 
       Log.e(TAG, "Sem rotas encontradas!"); 
       return; 
      } 

      // Printa algumas informacoes sobre a rota 
      currentRoute = response.body().getRoutes().get(0); 
      Log.d(TAG, "Distancia: " + currentRoute.getDistance()); 
      Toast.makeText(
        Mapa.this, 
        "A rota tem " + currentRoute.getDistance() + " metros de distância.", 
        Toast.LENGTH_SHORT).show(); 

      // Desenhar a rota no mapa 
      drawRoute(currentRoute); 
     } 

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

private void drawRoute(DirectionsRoute route) { 

    // Converter as coordenadas LineString em 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()); 
    } 

    // Escrever os pontos no MapView 
    map.addPolyline(new PolylineOptions() 
      .add(points) 
      .color(Color.parseColor("#009688")) 
      .width(5)); 
} 

回答

0

您座標是不正確的順序。雖然LatLnglatitude, longitude訂單中創建,Position是xy訂購或longitude, latitude。所以你的來源和目的地應該是:

// Origem: Rodoviaria 
final Position origin = Position.fromCoordinates(-47.882645, -15.794082); 
// Destino: Reitoria 
final Position destination = Position.fromCoordinates(-47.866611, -15.762604); 
+0

...殺了我。謝謝您的幫助! ...再次! –