2017-08-29 129 views
0

我想使用osmdroid實現OpenStreetMap。我成功地將世界地圖設置爲一組特定的地理座標。這裏是我的代碼:osmdroid獲取當前用戶位置

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(); 
} 

}

osmdroid(5.6.5)的最新版本使用。

清單文件:

<?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> 

我如何在這個當前用戶位置?我是否也應該使用資源代理?

謝謝。

+0

你只是想顯示用戶的當前位置?我的情況下,它已經在stackoverflow回答https://stackoverflow.com/questions/8804788/how-to-display-the-current-position-pointer-in-osmdroid –

+0

@josef在我的代碼的哪一部分,我應該添加此?我試過這個例子,但我無法獲得當前的用戶位置。我正在使用osmdroid 5.6.5。自定義資源代理僅適用於5.2之前的版本 您可以給我看一個小演示先生。 – Thomas

+0

有沒有錯誤?您的應用程序是否處理某個位置的權限?你必須在manifest中聲明permissioinss,並且如果你是針對android 6或更新版本構建權限請求處理權限請求。檢查這個問題:https://stackoverflow.com/questions/1513485/how-do-i-get-the-current-gps-location-programmatically-in-android –

回答

0

您可以使用MyLocationNewOverlay

... 
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); 

在活動的onResume方法再加入:

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(); 
} 

而且在在onPause

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

哪裏locationOverlay顯然是類型爲MyLocationNewOverlay場。

欲瞭解更多詳情check documentation for the class。還有an example in the sample application

+0

@我添加了這段代碼,並更新了問題中的代碼。我得到這個錯誤:無法解析符號mOsmOverlays – Thomas

+0

好吧,因爲沒有該名稱的變量。將其更改爲map.getOverlays()。add(..) –

+0

錯誤消失。但我無法在地圖中看到我目前的位置。我應該改變什麼? – Thomas