我想使用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>
我如何在這個當前用戶位置?我是否也應該使用資源代理?
謝謝。
你只是想顯示用戶的當前位置?我的情況下,它已經在stackoverflow回答https://stackoverflow.com/questions/8804788/how-to-display-the-current-position-pointer-in-osmdroid –
@josef在我的代碼的哪一部分,我應該添加此?我試過這個例子,但我無法獲得當前的用戶位置。我正在使用osmdroid 5.6.5。自定義資源代理僅適用於5.2之前的版本 您可以給我看一個小演示先生。 – Thomas
有沒有錯誤?您的應用程序是否處理某個位置的權限?你必須在manifest中聲明permissioinss,並且如果你是針對android 6或更新版本構建權限請求處理權限請求。檢查這個問題:https://stackoverflow.com/questions/1513485/how-do-i-get-the-current-gps-location-programmatically-in-android –