如何在Android中不使用GPS获取纬度经度值?

如何在Android中不使用GPS获取纬度经度值?,第1张

概述我有一个没有GPS模块的设备.有没有办法在不使用Android的GPS模块的情况下获得LatLon值.嘿,失败的选民可以解释这个问题的错误吗?如果您对问题有答案,那么请尽量理解问题.解决方法:只需在你的包中的Java类下面.MyLocation.javaimportjava.util.Timer;importjava.util.Ti

我有一个没有GPS模块的设备.有没有办法在不使用Android的GPS模块的情况下获得Lat Lon值.

嘿,失败的选民可以解释这个问题的错误吗?
如果您对问题有答案,那么请尽量理解问题.

解决方法:

只需在你的包中的java类下面.

MyLocation.java

    import java.util.Timer;import java.util.TimerTask;import androID.content.Context;import androID.location.Location;import androID.location.LocationListener;import androID.location.LocationManager;import androID.os.Bundle;public class MyLocation {    Timer timer1;    LocationManager lm;    LocationResult locationResult;    boolean gps_enabled=false;    boolean network_enabled=false;    public boolean getLocation(Context context, LocationResult result)    {        //I use LocationResult callback class to pass location value from MyLocation to user code.        locationResult=result;        if(lm==null)            lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);        //exceptions will be thrown if provIDer is not permitted.        try        {            gps_enabled=lm.isProvIDerEnabled(LocationManager.GPS_PROVIDER);        }        catch(Exception ex)        {            ex.printstacktrace();        }        try        {            network_enabled=lm.isProvIDerEnabled(LocationManager.NETWORK_PROVIDER);        }        catch(Exception ex)        {            ex.printstacktrace();        }        //don't start Listeners if no provIDer is enabled        if(!gps_enabled && !network_enabled)            return false;        if(gps_enabled)            lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListenerGps);        if(network_enabled)            lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListenerNetwork);        timer1=new Timer();        timer1.schedule(new GetLastLocation(), 20000);        return true;    }    LocationListener locationListenerGps = new LocationListener()     {        public voID onLocationChanged(Location location)         {            timer1.cancel();            locationResult.gotLocation(location);          //  lm.removeUpdates(this);            //lm.removeUpdates(locationListenerNetwork);        }        public voID onProvIDerDisabled(String provIDer) {}        public voID onProvIDerEnabled(String provIDer) {}        public voID onStatusChanged(String provIDer, int status, Bundle extras) {}    };    LocationListener locationListenerNetwork = new LocationListener()    {        public voID onLocationChanged(Location location)         {            timer1.cancel();            locationResult.gotLocation(location);           // lm.removeUpdates(this);         //   lm.removeUpdates(locationListenerGps);        }        public voID onProvIDerDisabled(String provIDer) {}        public voID onProvIDerEnabled(String provIDer) {}        public voID onStatusChanged(String provIDer, int status, Bundle extras) {}    };    class GetLastLocation extends TimerTask     {        @OverrIDe        public voID run() {             //lm.removeUpdates(locationListenerGps);             //lm.removeUpdates(locationListenerNetwork);             Location net_loc=null, gps_loc=null;             if(gps_enabled)                 gps_loc=lm.getLastKNownLocation(LocationManager.GPS_PROVIDER);             if(network_enabled)                 net_loc=lm.getLastKNownLocation(LocationManager.NETWORK_PROVIDER);             //if there are both values use the latest one             if(gps_loc!=null && net_loc!=null){                 if(gps_loc.getTime()>net_loc.getTime())                     locationResult.gotLocation(gps_loc);                 else                     locationResult.gotLocation(net_loc);                 return;             }             if(gps_loc!=null){                 locationResult.gotLocation(gps_loc);                 return;             }             if(net_loc!=null){                 locationResult.gotLocation(net_loc);                 return;             }             locationResult.gotLocation(null);        }    }    public static abstract class LocationResult    {        public abstract voID gotLocation(Location location);    }}

现在,在您的活动中,您可以找到如下代码所在的位置:

    public static Location loc;        @OverrIDe    protected voID onCreate(Bundle savedInstanceState) {        // Todo auto-generated method stub        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.splash_layout);        // to Find the Location         LocationResult locationResult = new LocationResult(){            @OverrIDe            public voID gotLocation(final Location location){                loc=location;                System.out.println("Latitude: "+loc.getLatitude());                System.out.println("Longitude: "+loc.getLongitude());            }        };        MyLocation myLocation = new MyLocation();    myLocation.getLocation(SplashActivity.this, locationResult);    }}

使用上面的代码,你不必在设备上的GPS.它将自动检查最佳可用位置管理器并根据它找到位置.

它还需要AndroIDManifest.xml文件中的一些权限.

希望它会对你有所帮助.

总结

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

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

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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存