2017-08-29 37 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

間違いはありますか?あなたのアプリケーションはある場所の権限を扱いますか?マニフェストでパーミッションを宣言し、アンドロイド6以降でビルドする場合は許可リクエストを処理する必要があります。この質問をチェックする:https://stackoverflow.com/questions/1513485/how-do-i-get-the-current-gps-location-programmatically-in-android –

答えて

0

あなたはその後、追加アクティビティでonResumeメソッドで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); 

を使用することができます。

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でのlocationOverlayは明らかにMyLocationNewOverlayとして型指定されたフィールドである

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

詳細はcheck documentation for the classです。 an example in the sample applicationもあります。

+0

@このコードを追加し、質問のコードも更新しました。私はこのエラーが表示されます:シンボルmOsmOverlaysを解決できません – Thomas

+0

その名前の変数はありません。これをmap.getOverlays()に変更します。add(..) –

+0

エラーは消えました。しかし、私は現在の場所を地図で見ることができません。私は何を変えるべきですか? – Thomas

関連する問題