如何使用osmdroid添加我的当前位置?

时间:2018-12-06 12:36:47

标签: android location openstreetmap point osmdroid

我是Android Studio中的全新用户,我想构建一个项目,该项目在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();
    }


}

任何答案都将完全有帮助...

1 个答案:

答案 0 :(得分:1)

请参见osmdroid wiki。最简单的解决方案是在地图视图中添加My Location overlay

this.mLocationOverlay = new MyLocationNewOverlay(new GpsMyLocationProvider(context),mMapView);
this.mLocationOverlay.enableMyLocation();
mMapView.getOverlays().add(this.mLocationOverlay);