В моем приложении я интегрировал Mapbox, и он работал нормально. Но теперь он не загружает карту в NavigationView. Он отлично работает в основной ветке, но не работает в ветке функций BLE, хотя я ничего не изменил в этой активности. Пожалуйста, найдите ниже код здесь, я запускаю этот NavigationActivity из MainActivity, где пользователь выбирает места отправления и назначения.
NavigationActivity-Screen:
NavigationActivity. java:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.mapbox.api.directions.v5.models.BannerText;
import com.mapbox.api.directions.v5.models.DirectionsRoute;
import com.mapbox.api.directions.v5.models.LegStep;
import com.mapbox.services.android.navigation.ui.v5.NavigationViewOptions;
import com.mapbox.services.android.navigation.ui.v5.OnNavigationReadyCallback;
import com.mapbox.services.android.navigation.ui.v5.instruction.InstructionLoader;
import com.mapbox.services.android.navigation.v5.milestone.BannerInstructionMilestone;
import com.mapbox.services.android.navigation.v5.milestone.Milestone;
import com.mapbox.services.android.navigation.v5.milestone.MilestoneEventListener;
import com.mapbox.services.android.navigation.v5.routeprogress.RouteProgress;
import java.math.BigDecimal;
import java.util.List;
public class NavigationActivity extends AppCompatActivity implements MilestoneEventListener, OnNavigationReadyCallback {
private TextView myBanner;
ImageView next, previous;
List<LegStep> steps;
TextView tv_step;
int currentStepIndex = 0;
DirectionsRoute currentRoute = null ;
com.mapbox.services.android.navigation.ui.v5.NavigationView navigationView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigations);
navigationView = findViewById(R.id.navigationView);
myBanner = findViewById(R.id.dummyBanner);
tv_step = findViewById(R.id.tv_step);
next = findViewById(R.id.iv_next);
previous = findViewById(R.id.iv_previous);
navigationView.onCreate(savedInstanceState);
navigationView.initialize(this);
try{
currentRoute = (DirectionsRoute) getIntent().getSerializableExtra("route");
if(currentRoute != null){
steps = currentRoute.legs().get(0).steps();
if(steps.size() > 0){
setup();
}
}
}catch (Exception e){}
}
private void setup(){
loadText( 0);
next.setOnClickListener((View v )-> {
if (currentStepIndex < steps.size() - 2)
loadText(++currentStepIndex);
});
previous.setOnClickListener((View v )-> {
if (currentStepIndex > 0)
loadText(--currentStepIndex);
});
}
private void loadText( int index){
BannerText bannerText = steps.get(index).bannerInstructions().get(0).primary();
InstructionLoader loader = new InstructionLoader(tv_step, bannerText );
loader.loadInstruction();
String text = tv_step.getText().toString();
String modifier = bannerText.modifier();
String directionArrow = getDirectionArrow(text, modifier);
if(!directionArrow.equals(""))
tv_step.setText(directionArrow + text);
tv_step.append(" " + getDistanceStr(steps.get(index).distance()));
Log.d("Debugging modifier", bannerText.toJson());
}
@Override
public void onNavigationReady(boolean isRunning) {
DirectionsRoute directionsRoute = (DirectionsRoute) getIntent().getSerializableExtra("route");
NavigationViewOptions options = NavigationViewOptions.builder()
.directionsRoute(directionsRoute)
.shouldSimulateRoute(false)
.milestoneEventListener(this)
.build();
navigationView.startNavigation(options);
}
@Override
public void onMilestoneEvent(RouteProgress routeProgress, String instruction, Milestone milestone) {
try {
if (milestone instanceof BannerInstructionMilestone) {
BannerText primaryInstruction = ((BannerInstructionMilestone) milestone).getBannerInstructions().primary();
primaryInstruction.text();
InstructionLoader loader = new InstructionLoader(myBanner, primaryInstruction);
loader.loadInstruction();
String text = myBanner.getText().toString();
String modifier = primaryInstruction.modifier();
String directionArrow = getDirectionArrow(text, modifier);
if(!directionArrow.equals(""))
myBanner.setText(directionArrow + text);
double distance = routeProgress.currentLegProgress().currentStep().distance();
String distanceStr = getDistanceStr(distance);
String milestoneString = myBanner.getText().toString() + distanceStr;
}
}catch (Exception e){
e.printStackTrace();
}
}
private String getDirectionArrow(String text, String modifier){
Log.e("Debugging", text + " " + modifier);
if (text.contains("right") || modifier.contains("right"))
return "> ";
else if (text.contains("left") || modifier.contains("left"))
return "< ";
else if (text.contains("straight") || modifier.contains("straight"))
return "^ ";
return "";
}
private String getDistanceStr(double distance){
if(distance > 1000)
return " " + formatAmount(distance/1000) + " km";
else return " " + distance + " m";
}
@Override
public void onStart() {
super.onStart();
navigationView.onStart();
}
@Override
public void onResume() {
super.onResume();
navigationView.onResume();
}
@Override
public void onLowMemory() {
super.onLowMemory();
navigationView.onLowMemory();
}
@Override
public void onBackPressed() {
if (!navigationView.onBackPressed()) {
super.onBackPressed();
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
navigationView.onSaveInstanceState(outState);
super.onSaveInstanceState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
navigationView.onRestoreInstanceState(savedInstanceState);
}
@Override
public void onPause() {
super.onPause();
navigationView.onPause();
}
@Override
public void onStop() {
super.onStop();
navigationView.onStop();
}
@Override
protected void onDestroy() {
super.onDestroy();
navigationView.onDestroy();
}
public static String formatAmount(Double str){
BigDecimal bd = new BigDecimal(str);
bd = bd.setScale(2, BigDecimal.ROUND_HALF_UP);
return bd.toString();
}
}
activity_navigations. xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:mapbox="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="talha.niazi.hudlitenav.MainActivity"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:id="@+id/dummyBanner"
android:background="@color/design_default_color_primary"
android:textColor="@color/mapboxWhite"
android:padding="10dp"
android:layout_height="wrap_content" />
<RelativeLayout
android:layout_width="match_parent"
android:background="@color/button_red"
android:id="@+id/ll_nav"
android:padding="15dp"
android:layout_height="wrap_content">
<ImageView
android:layout_width="30sp"
android:tint="@color/white_color"
android:id="@+id/iv_previous"
mapbox:srcCompat="@drawable/previous"
android:layout_height="30sp" />
<TextView
android:layout_width="wrap_content"
android:textColor="@color/white_color"
android:layout_marginStart="10sp"
android:textStyle="bold"
android:layout_marginEnd="10sp"
android:layout_centerVertical="true"
android:id="@+id/tv_step"
android:layout_centerHorizontal="true"
android:layout_height="wrap_content" />
<ImageView
android:layout_width="30sp"
android:layout_alignParentEnd="true"
android:id="@+id/iv_next"
android:tint="@color/white_color"
mapbox:srcCompat="@drawable/next"
android:layout_height="30sp" />
</RelativeLayout>
<com.mapbox.services.android.navigation.ui.v5.NavigationView
android:id="@+id/navigationView"
android:layout_below="@id/ll_nav"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
Было бы здорово, если вы поможете мне решить эту странную проблему.