osmdroid 获取当前用户位置 [英] osmdroid get current user location

查看:75
本文介绍了osmdroid 获取当前用户位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 osmdroid 实现 OpenStreetMap.我成功地将世界地图设置为一组特定的地理坐标.这是我的代码:

I am trying to implement OpenStreetMap using osmdroid. I successfully got the world map set to a particular set of geo co-ordinates. here is my code:

 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).

The current version of osmdroid (5.6.5) is used.

清单文件:

<?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>

我如何在此获取当前用户位置?.我还应该使用资源代理吗?

How do I get the current user location in this?. Should I also use resource proxy?

谢谢.

推荐答案

您可以使用 MyLocationNewOverlay

You can use 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);

在活动的 onResume 方法中添加:

In onResume method in the activity then add:

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 中

And in onPause

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

locationOverlay 显然是一个输入为 MyLocationNewOverlay 的字段.

Where locationOverlay is obviously a field typed as MyLocationNewOverlay.

有关更多详细信息检查该类的文档.还有 .

For more details check documentation for the class. There's also an example in the sample application.

这篇关于osmdroid 获取当前用户位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆