osmdroid获取当前用户位置

时间:2017-08-29 09:43:49

标签: android osmdroid

我正在尝试使用osmdroid实现OpenStreetMap。我成功地将世界地图设置为一组特定的地理坐标。这是我的代码:

 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>

如何获取当前用户的位置?我还应该使用资源代理吗?

感谢。

1 个答案:

答案 0 :(得分: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