Servus,
aktuell komme ich bei einem Problem nicht weiter so recht weiter.
Hintergrund/Ziel
Ich programmiere eine App in der die aktuelle GNSS Position oder eine vorgegebene/eingegebene Position des Users in der Karte übernommen werden soll. Dazu möchte ich den von Android bereitstellten LocationListener nutzen, da dieser noch weitere notwendige GNSS Daten liefert.
Derzeit schaffe ich es nur die GNSS Position ausschließlich via OMSDroid Framework zu setzen und angezeigt zu bekommen. Ich möchte jedoch diese Position manuell via LocationListen/Handeingabe setzen statt, die Klasse von OSMDroid zu nutzen.
Im Anhang habe ich meinen abgespeckten Code beigefügt. Ich bedanke mich schon mal für Eure Hilfe!
Library
Der LocationListener ist noch nicht implementiert.
Activity
package de.test.osmdroidtest;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.preference.PreferenceManager;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import org.osmdroid.config.Configuration;
import org.osmdroid.tileprovider.tilesource.TileSourceFactory;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.CustomZoomButtonsController;
import org.osmdroid.views.MapView;
import org.osmdroid.views.overlay.mylocation.GpsMyLocationProvider;
import org.osmdroid.views.overlay.mylocation.MyLocationNewOverlay;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity implements LocationListener {
private final int REQUEST_PERMISSIONS_REQUEST_CODE = 1;
private MapView map = null;
MyLocationNewOverlay myLocationOverlay;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//handle permissions first, before map is created. not depicted here
//load/initialize the osmdroid configuration, this can be done
Context ctx = getApplicationContext();
Configuration.getInstance().load(ctx, PreferenceManager.getDefaultSharedPreferences(ctx));
//setting this before the layout is inflated is a good idea
//it 'should' ensure that the map has a writable location for the map cache, even without permissions
//if no tiles are displayed, you can try overriding the cache path using Configuration.getInstance().setCachePath
//see also StorageUtils
//Layout wird geholt
setContentView(R.layout.activity_main);
map = (MapView) findViewById(R.id.map);
map.setTileSource(TileSourceFactory.MAPNIK);
map.getZoomController().setVisibility(CustomZoomButtonsController.Visibility.ALWAYS);
map.setMultiTouchControls(true);
// Die Position soll hier via LocationListener oder via Handeingabe erfolgen
map.getController().setCenter(new GeoPoint(48.8583, 2.2944));
// Kann erst gesetzt werden wenn eine Position bekannt ist.
map.getController().setZoom(18D);
/**
* Hier soll das Positionssymbol gesetzt werden, wie kann das via Location Lister oder via Handeingabe erfolgen?
*
* Gibt es für den GpsMyLocationProvider() eine alternative?
*/
this.myLocationOverlay = new MyLocationNewOverlay(new GpsMyLocationProvider(this),map);
this.myLocationOverlay.enableMyLocation();
map.getOverlays().add(this.myLocationOverlay);
requestPermissionsIfNecessary(new String[]{
// Wird später auskommentiert, da GNSS noch nicht benötigt wird
// Manifest.permission.ACCESS_FINE_LOCATION,
// WRITE_EXTERNAL_STORAGE is required in order to show the map
Manifest.permission.WRITE_EXTERNAL_STORAGE
});
}
@Override
public void onResume() {
super.onResume();
map.onResume(); //needed for compass, my location overlays, v6.0.0 and up
}
@Override
public void onPause() {
super.onPause();
map.onPause(); //needed for compass, my location overlays, v6.0.0 and up
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
ArrayList<String> permissionsToRequest = new ArrayList<>();
for (int i = 0; i < grantResults.length; i++) {
permissionsToRequest.add(permissions[i]);
}
if (permissionsToRequest.size() > 0) {
ActivityCompat.requestPermissions(
this,
permissionsToRequest.toArray(new String[0]),
REQUEST_PERMISSIONS_REQUEST_CODE);
}
}
private void requestPermissionsIfNecessary(String[] permissions) {
ArrayList<String> permissionsToRequest = new ArrayList<>();
for (String permission : permissions) {
if (ContextCompat.checkSelfPermission(this, permission)
!= PackageManager.PERMISSION_GRANTED) {
// Permission is not granted
permissionsToRequest.add(permission);
}
}
if (permissionsToRequest.size() > 0) {
ActivityCompat.requestPermissions(
this,
permissionsToRequest.toArray(new String[0]),
REQUEST_PERMISSIONS_REQUEST_CODE);
}
}
@Override
public void onLocationChanged(@NonNull Location location) {
// Hier soll später bei jeder Änderung die Position neu gesetzt werden.
}
}
XML Layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<org.osmdroid.views.MapView android:id="@+id/map"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
Ich bedanke mich schon mal für Eure Anregungen!
Viele Grüße,
Teskania