如何在Android中获取纬度和经度?

如何在Android中获取纬度和经度?,第1张

概述如何在android2.3.3中获取当前的纬度经度?我尝试跟随this,但我无法运行程序.我的代码LatView=(TextView)findViewById(R.id.LatText);LngView=(TextView)findViewById(R.id.LngText);LocationManagerlm=(LocationManager)getSystemService(Context.LOCAT

如何在android2.3.3中获取当前的纬度和经度?

我尝试跟随this,但我无法运行程序.

我的代码

    LatVIEw = (TextVIEw)findVIEwByID(R.ID.LatText);    LngVIEw = (TextVIEw)findVIEwByID(R.ID.LngText);    LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);     Location location = lm.getLastKNownLocation(LocationManager.GPS_PROVIDER);    double longitude = location.getLongitude();    double latitude = location.getLatitude();

在androID清单中我把它.

<uses-permission androID:name="androID.permission.INTERNET" /> <uses-permission androID:name="androID.permission.ACCESS_COARSE_LOCATION" /><uses-permission androID:name="androID.permission.ACCESS_FINE_LOCATION" />

请帮忙.
谢谢.

解决方法:

这是我在我的应用程序中找到设备当前位置,它工作正常:

public class MyLocationActivity extends Activity implements LocationListener {    private LocationManager mgr;    private String best;    public static double myLocationLatitude;    public static double myLocationLongitude;    @OverrIDe    public voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        mgr = (LocationManager) getSystemService(LOCATION_SERVICE);        dumpProvIDers();        Criteria criteria = new Criteria();        best = mgr.getBestProvIDer(criteria, true);        Log.d("best provIDer", best);        Location location = mgr.getLastKNownLocation(best);        dumpLocation(location);            //may be some intent here    }    public voID onLocationChanged(Location location) {        // Todo auto-generated method stub        dumpLocation(location);    }    public voID onProvIDerDisabled(String provIDer) {        // Todo auto-generated method stub    }    public voID onProvIDerEnabled(String provIDer) {        // Todo auto-generated method stub    }    public voID onStatusChanged(String provIDer, int status, Bundle extras) {        // Todo auto-generated method stub    }    @OverrIDe    protected voID onPause() {        super.onPause();        mgr.removeUpdates(this);    }    @OverrIDe    protected voID onResume() {        super.onResume();        mgr.requestLocationUpdates(best, 15000, 1, this);    }    private voID dumpLocation(Location l) {        if (l == null) {            myLocationLatitude = 0.0;            myLocationLongitude = 0.0;        } else {            myLocationLatitude = l.getLatitude();            myLocationLongitude = l.getLongitude();        }    }    private voID dumpProvIDers() {        List<String> provIDers = mgr.getAllProvIDers();        for (String p : provIDers) {            dumpProvIDers(p);        }    }    private voID dumpProvIDers(String s) {        LocationProvIDer info = mgr.getProvIDer(s);        StringBuilder builder = new StringBuilder();        builder.append("name: ").append(info.getname());    }}

这里MyLocationLatitude和MyLocationLongitude给出了你需要的lat和long的值.

总结

以上是内存溢出为你收集整理的如何在Android中获取纬度和经度?全部内容,希望文章能够帮你解决如何在Android中获取纬度和经度?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1105885.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存