该应用成功获取了设备的当前位置,并且通过使用标记,我能够在mapView上查明其他位置,创建路线并尝试从一个位置导航到另一个位置。该应用程序能够更新当前位置,但在尝试以GPS导航时却无法更新。即使位置发生更改,图标也会在路线开始时保持空闲状态。
代码如下:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mapView = findViewById(R.id.mapView);
startNavigation = findViewById(R.id.startNavigation);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(this);
startNavigation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
pickNavigationDialog();
}
});
}
public void pickNavigationDialog() {
new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.myDialog))
.setTitle("Choose function")
.setMessage("Would you like to simulate navigation?")
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
try {
if (originPosition != null && destinationPosition != null) {
NavigationLauncherOptions options = NavigationLauncherOptions.builder()
.origin(originPosition)
.destination(destinationPosition)
.shouldSimulateRoute(false) // Does not simulate navigation.
.build();
if (destinationPosition != null) {
NavigationLauncher.startNavigation(MainActivity.this, options); //Transitions to an other activity in order to start navigation.
}
} else {
Toast.makeText(getApplicationContext(), "Destination is not set.", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
}
基本上,我们需要修改此代码,以便能够在gps模式下更新当前位置。