如何使用 osmdroid 添加我的当前位置?
How to add my current location with osmdroid?
我是 Android 工作室的新手,我想构建一个项目来显示用户在 openstreet 地图中的 x、y 坐标(作为指针)。我查看了 GitHub 代码,但这确实是一场斗争..
我添加了地图,你能解释一下如何从 gps 获取我的位置吗?
这是我的代码..
package mytestapplication.hello.com.map;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.preference.PreferenceManager;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
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.IMyLocationConsumer;
import org.osmdroid.views.overlay.mylocation.IMyLocationProvider;
import org.osmdroid.views.overlay.mylocation.MyLocationNewOverlay;
public class MainActivity extends AppCompatActivity {
MapView map = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Context ctx = getApplicationContext();
Configuration.getInstance().load(ctx, PreferenceManager.getDefaultSharedPreferences(ctx));
map = findViewById(R.id.map);
map.setTileSource(TileSourceFactory.MAPNIK);
IMapController mapController = map.getController();
map.setBuiltInZoomControls(true);
map.setMultiTouchControls(true);
mapController.setZoom((long) 14);
GeoPoint startPoint = new GeoPoint(x, y);
mapController.animateTo(startPoint);
}
@Override
protected void onPause() {
super.onPause();
map.onPause();
}
@Override
protected void onResume() {
super.onResume();
map.onResume();
}
}
任何答案都会很有帮助...
查看 osmdroid wiki. The simplest solution is to add a My Location overlay 到您的地图视图:
this.mLocationOverlay = new MyLocationNewOverlay(new GpsMyLocationProvider(context),mMapView);
this.mLocationOverlay.enableMyLocation();
mMapView.getOverlays().add(this.mLocationOverlay);
我是 Android 工作室的新手,我想构建一个项目来显示用户在 openstreet 地图中的 x、y 坐标(作为指针)。我查看了 GitHub 代码,但这确实是一场斗争..
我添加了地图,你能解释一下如何从 gps 获取我的位置吗? 这是我的代码..
package mytestapplication.hello.com.map;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.preference.PreferenceManager;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
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.IMyLocationConsumer;
import org.osmdroid.views.overlay.mylocation.IMyLocationProvider;
import org.osmdroid.views.overlay.mylocation.MyLocationNewOverlay;
public class MainActivity extends AppCompatActivity {
MapView map = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Context ctx = getApplicationContext();
Configuration.getInstance().load(ctx, PreferenceManager.getDefaultSharedPreferences(ctx));
map = findViewById(R.id.map);
map.setTileSource(TileSourceFactory.MAPNIK);
IMapController mapController = map.getController();
map.setBuiltInZoomControls(true);
map.setMultiTouchControls(true);
mapController.setZoom((long) 14);
GeoPoint startPoint = new GeoPoint(x, y);
mapController.animateTo(startPoint);
}
@Override
protected void onPause() {
super.onPause();
map.onPause();
}
@Override
protected void onResume() {
super.onResume();
map.onResume();
}
}
任何答案都会很有帮助...
查看 osmdroid wiki. The simplest solution is to add a My Location overlay 到您的地图视图:
this.mLocationOverlay = new MyLocationNewOverlay(new GpsMyLocationProvider(context),mMapView);
this.mLocationOverlay.enableMyLocation();
mMapView.getOverlays().add(this.mLocationOverlay);