无法将我的位置叠加添加到osmdroid地图

时间:2018-02-18 05:34:31

标签: android openstreetmap osmdroid

我正在尝试使用以下代码将我的GPS位置添加到地图中:

MyLocationNewOverlay locationOverlay;
locationOverlay = new MyLocationNewOverlay(new GpsMyLocationProvider(mContext), mapView);
            locationOverlay.enableMyLocation();
            mapView.getOverlays().add(locationOverlay);

但是,代码没有效果,并且没有添加任何内容(例如,没有图标,没有文本等)。请注意,缩放叠加和手势叠加效果很好,但是当我将它们添加到地图视图时,MyLocationNewOverlay叠加层无效。有任何想法吗?

更新

这是我活动的代码:

public class MainActivity extends AppCompatActivity implements View.OnClickListener, LocationListener {
    private static final int STORAGE_REQUEST = 100;
    private static final int LOCATION_REQUEST = 101;
    private static final String TAG = "MainActivity";
    private Context mContext;
    private MapView mMap;
    private EditText latitude;
    private EditText longitude;
    private Button searchLocation;
    IMapController mapController;

    private MyLocationNewOverlay locationOverlay;

    ArrayList<OverlayItem> items = new ArrayList<>();

    /**
     * Compass overlay
     */
    private CompassOverlay mCompassOverlay;


    private RotationGestureOverlay mRotationOverlay;

    private ScaleBarOverlay mScaleBarOverlay;


    private MyLocationNewOverlay mMyLocationOverlay;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mContext = getApplicationContext();

        Configuration.getInstance().load(mContext, PreferenceManager.getDefaultSharedPreferences(mContext));
        Configuration.getInstance().setUserAgentValue("Dalvik");


        setContentView(R.layout.activity_main);


        //Check Permissions
        if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, STORAGE_REQUEST);
        }

        if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, LOCATION_REQUEST);
        }


        mMap = findViewById(R.id.mapView);
        mMap.setTileSource(TileSourceFactory.MAPNIK);


        mMap.setBuiltInZoomControls(true);
        mMap.setMultiTouchControls(true);


        latitude = findViewById(R.id.latitude);
        longitude = findViewById(R.id.longitude);
        searchLocation = findViewById(R.id.searchLocation);
        searchLocation.setOnClickListener(this);


        mapController = mMap.getController();
        mapController.setZoom(9.0f);

        //Add my location

        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);

        final LocationManager locMgr = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
        final String provider = locMgr.getBestProvider(criteria, true);
        locMgr.requestLocationUpdates(provider, 1000, 100, this);

        Location loc = locMgr.getLastKnownLocation(provider);
        Log.d(TAG, "onCreate: " + loc.getLatitude() + ", " + loc.getLongitude());

        //Start on Iran
        GeoPoint iran = new GeoPoint(32.4279, 53.6880);
        mapController.setCenter(iran);


        //Add my location overlay
        mMyLocationOverlay = new MyLocationNewOverlay(new GpsMyLocationProvider(mContext), mMap);
        mMyLocationOverlay.enableMyLocation();
        mMap.getOverlays().add(mMyLocationOverlay);

        //Add compass overlay
        mCompassOverlay = new CompassOverlay(mContext, mMap);
        mCompassOverlay.enableCompass();
        mCompassOverlay.setEnabled(true);
        mMap.getOverlays().add(mCompassOverlay);



        //Add rotation gesture overlay
        mRotationOverlay = new RotationGestureOverlay(mMap);
        mRotationOverlay.setEnabled(true);
        mMap.setMultiTouchControls(true);
        mMap.getOverlays().add(mRotationOverlay);


        //Add map scale bar overlay
        mScaleBarOverlay = new ScaleBarOverlay(mMap);
        mScaleBarOverlay.setCentred(true);
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        mScaleBarOverlay.setScaleBarOffset(dm.widthPixels / 2, 30);
        mMap.getOverlays().add(mScaleBarOverlay);



    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        //Check and decide
        if (grantResults[0] != PackageManager.PERMISSION_GRANTED) {
            Toast.makeText(mContext, R.string.permission_not_granted_please_allow, Toast.LENGTH_LONG).show();
        }

    }


    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.searchLocation) {
            double lat = Double.valueOf(latitude.getText().toString());
            double lng = Double.valueOf(longitude.getText().toString());

            GeoPoint geoPoint = new GeoPoint(lat, lng);
            mapController.setCenter(geoPoint);
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        mMyLocationOverlay.setEnabled(false);
        mMap.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mMyLocationOverlay.setEnabled(true);
        mMap.onResume();

    }

    @Override
    public void onLocationChanged(Location location) {
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
    }

    @Override
    public void onProviderEnabled(String provider) {
    }

    @Override
    public void onProviderDisabled(String provider) {
    }
}

2 个答案:

答案 0 :(得分:1)

这被问了很多。这是不工作的最常见原因

  • 您的设备是否还有GPS?
  • 您的设备是否启用了GPS服务?
  • osmdroid默认只添加GPS,您可能需要将网络位置服务器添加到覆盖查询的传感器列表
  • 你在外面并且有GPS信号吗?
  • 您是否在清单中授予了权限?
  • 如果定位API23 +,您是否曾要求用户提供运行时权限?

如果您碰巧有自定义位置提供商或需要自定义叠加层使用的位置Feed,请尝试以下

GpsMyLocationProvider prov= new GpsMyLocationProvider(); 
prov.addLocationSource(LocationManager.NETWORK_PROVIDER);
MyLocationNewOverlay locationOverlay = new MyLocationNewOverlay 
locationOverlay(prov, mapView);
locationOverlay.enableMyLocation();
mMap.getOverlayManager().add(locationOverlay);

然后照常进行。

答案 1 :(得分:0)

您似乎无法使用GpsMyLocationProvider向地图添加叠加层。更好的方法可能是使用ItemizedIconOverlay,您可以使用Android getLocationUpdates从Android获取位置,创建GeoPoint,创建ItemOverlay,将其添加到ItemizedIconOverlay,然后将其添加到MapView:

  final LocationManager locMgr = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
        final String provider = locMgr.getBestProvider(criteria, true);
        locMgr.requestLocationUpdates(provider, 1000, 100, this);

        Location loc = locMgr.getLastKnownLocation(provider);


        //Add my location overlay
        OverlayItem newItem = new OverlayItem("Here", "You are here", new GeoPoint(loc));
        items.add(newItem);

        ItemizedIconOverlay<OverlayItem> myLocOverlay = new ItemizedIconOverlay<OverlayItem>(items,
                getResources().getDrawable(R.drawable.my_location, null),
                new ItemizedIconOverlay.OnItemGestureListener<OverlayItem>() {
                    @Override
                    public boolean onItemSingleTapUp(int index, OverlayItem item) {
                        return false;
                    }

                    @Override
                    public boolean onItemLongPress(int index, OverlayItem item) {
                        return false;
                    }
                },
        mContext);
        mMap.getOverlays().add(myLocOverlay);