2017-08-29 7 views
0

Ich versuche OpenStreetMap mit Osmdroid zu implementieren. Ich habe die Weltkarte erfolgreich auf einen bestimmten Satz von Geo-Koordinaten gesetzt. hier ist mein Code:osmdroid aktuellen Benutzer Ort

package com.example.re.osm; 
    //OSM MAP CODE 
    import android.app.Activity; 
    import android.content.Context; 
    import android.os.Bundle; 
    import android.preference.PreferenceManager; 
    import android.view.LayoutInflater; 
    import android.view.View; 
    import android.view.ViewGroup; 
    import android.widget.RelativeLayout; 

import org.osmdroid.api.IMapController; 
import org.osmdroid.config.Configuration; 
import org.osmdroid.tileprovider.tilesource.TileSourceFactory; 
import org.osmdroid.util.GeoPoint; 
import org.osmdroid.views.MapView; 
import org.osmdroid.views.overlay.mylocation.GpsMyLocationProvider; 
import org.osmdroid.views.overlay.mylocation.MyLocationNewOverlay; 

public class MainActivity extends Activity { 

private MapView mMapView; 
private MyLocationNewOverlay locationOverlay; 

@Override public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    Context ctx = getApplicationContext(); 
    //important! set your user agent to prevent getting banned from the osm servers 
    Configuration.getInstance().load(ctx, PreferenceManager.getDefaultSharedPreferences(ctx)); 
    setContentView(R.layout.activity_main); 

    MapView map = (MapView) findViewById(R.id.map); 
    map.setTileSource(TileSourceFactory.MAPNIK); 
    map.setBuiltInZoomControls(true); 
    map.setMultiTouchControls(true); 

    /*IMapController mapController = map.getController(); 
    mapController.setZoom(9); 
    GeoPoint startPoint = new GeoPoint(48.8583, 2.2944); 
    mapController.setCenter(startPoint);*/ 
    //add 
    locationOverlay = new MyLocationNewOverlay(map); 
    mOsmOverlays.add(locationOverlay); 
} 

public void onResume(){ 
    super.onResume(); 
    //this will refresh the osmdroid configuration on resuming. 
    //if you make changes to the configuration, use 
    //SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); 
    //Configuration.getInstance().save(this, prefs); 
    Configuration.getInstance().load(this, PreferenceManager.getDefaultSharedPreferences(this)); 
    //add 
    locationOverlay.enableMyLocation(); 
} 

public void onPause(){ 
    super.onPause(); 
    //add 
    locationOverlay.disableMyLocation(); 
} 

}

Die aktuelle Version von osmdroid (5.6.5) verwendet wird.

Manifest-Datei:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.re.osm" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk android:minSdkVersion="10" /> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 
    <uses-permission android:name="android.permission.INTERNET"/> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> 
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 

    <application 
     android:icon="@drawable/ic_launcher" 
     android:label="OSM" > 
     <activity 
      android:name=".MainActivity" 
      android:label="OSM" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

Wie kann ich den aktuellen Standort des Nutzers in das bekommen ?. Sollte ich auch einen Proxy benutzen?

Danke.

+0

Möchten Sie nur die aktuelle Position des Benutzers anzeigen? I dieser Fall ist es bereits auf stackoverflow https://stackoverflow.com/questions/8804788/how-to-display-the-current-position-pointer-in-osmdroid beantwortet –

+0

@josef In welchem ​​Teil meines Codes sollte ich das hinzufügen ? Ich habe dieses Beispiel versucht, aber ich kann den aktuellen Benutzerstandort nicht ermitteln. Ich benutze osmdroid 5.6.5. Benutzerdefinierter Ressourcen-Proxy gilt nur für Versionen vor 5.2 Können Sie mir bitte eine kleine Demo zeigen? – Thomas

+0

Gibt es Fehler? Bearbeitet Ihre Anwendung Berechtigungen für einen Standort? Sie müssen permissions im Manifest deklarieren und die Erlaubnisanforderung behandeln, wenn Sie gegen android 6 oder neuer bauen. Überprüfen Sie diese Frage: https://stackoverflow.com/questions/1513485/how-do-i-get-the-current-gps-location-programmatic-in-android –

Antwort

0

können Sie MyLocationNewOverlay verwenden

... 
GeoPoint startPoint = new GeoPoint(48.8583, 2.2944); 
mapController.setCenter(startPoint); 
//add 
GpsMyLocationProvider provider = new GpsMyLocationProvider(this); 
provider.addLocationSource(LocationManager.NETWORK_PROVIDER); 
locationOverlay = new MyLocationNewOverlay(map, provider); 
locationOverlay.enableFollowLocation(); 
locationOverlay.runOnFirstFix(new Runnable() { 
    public void run() { 
     Log.d("MyTag", String.format("First location fix: %s", locationOverlay.getLastFix())); 
    } 
}); 
map.getOverlayManager().add(locationOverlay); 

In onResume Methode der Tätigkeit dann hinzu:

public void onResume(){ 
    super.onResume(); 
    //this will refresh the osmdroid configuration on resuming. 
    //if you make changes to the configuration, use 
    //SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); 
    //Configuration.getInstance().save(this, prefs); 
    Configuration.getInstance().load(this, PreferenceManager.getDefaultSharedPreferences(this)); 
    //add 
    locationOverlay.enableMyLocation(); 
} 

Und in onPause

public void onPause(){ 
    super.onResume(); 
    //add 
    locationOverlay.disableMyLocation(); 
} 

Wo locationOverlay ist offensichtlich ein Feld als MyLocationNewOverlay getippt .

Für weitere Details check documentation for the class. Es gibt auch an example in the sample application.

+0

@ Ich habe diesen Code hinzugefügt und auch den Code in der Frage aktualisiert. Ich erhalte diesen Fehler: Symbol kann nicht aufgelöst werden mOsmOverlays – Thomas

+0

Ok, beases theres keine Variable dieses Namens. Ändern Sie es in map.getOverlays(). Add (..) –

+0

Der Fehler ging weg. Aber ich kann meinen aktuellen Standort auf der Karte nicht sehen. Was soll ich ändern? – Thomas