首页 > 系统 > Android > 正文

Android开发中GPS导航获取位置时的代码示范

2020-02-21 17:36:46
字体:
来源:转载
供稿:网友

随着移动互联网的发展,越来越多手机支持GPS定位获取地理位置,Android的开发人员当然也不例外,但是开发中难免会遇到问题,本篇文章描述的是Android开发中GPS导航获取位置时的代码示范,武林技术频道小编带你进入下文了解!

Android开发中GPS导航获取位置时的代码示范


getLastKnownLocation(LocationManager.GPS_PROVIDER),


于是高兴地不得了。可是一写进代码里,返回值(Location 类型)居然一直为null..郁闷的不得了。在网上查了好久,发现好多人都和我一样纠结于这个问题上,有人说是因为GPS没打开,也有人说是相关权限没加上..可是我的明明已经在设置里打开,权限自然也加上了。在api上纠结了半天,终于找出原因了,原来要打开GPS其实在于这句:
 

 
setTestProviderEnabled("gps",true);


而跟手机上的设置没多大关系(起码在我的手机上测是这样的)。手机上的设置关闭了,这一句照样能打开;而即使手机设置打开了,没这一句也是白搭。与这句对应的是
 

 
setTestProviderEnabled("gps",false);


用来关闭GPS.
GPS打开后可以用上面的方法获取Location了吗?还是不可以!确切地说是有时候可以,因为这个函数获取的是上次已经获得的位置信息,设想如果此程序第一次跑,先前并没有获取过位置信息,当然返回值为null了。经仔细查看api,在

 

 


requestLocationUpdates (String provider, long minTime, float minDistance, LocationListener listener)


里发现了这样一句话:It may take a while to receive the most recent location. If an immediate location is required, applications may use the getLastKnownLocation(String) method. 因此为了获取位置信息,应该用此方法为manager设置监听器,在监听器中onLocationChanged(Location location)里获取。
测试代码如下:

 

 


public void onLocationChanged(Location location)
            {
                Log.i("onLocationChanged", "come in");
                if (location != null)
                {
                    Log.w("Location","Current altitude = "+ location.getAltitude());
                    Log.w("Location","Current latitude = "+ location.getLatitude());
                }
            }
    


经过测试,经过一段时间后可以获取Location(获取时间与minTime、minDistance相关)。还需注意的一个问题是在设置了监听器后,删除监听器之前不能用上面的方法关闭gps,否则会报错。因此关闭gps的方法是
 

 
manager.removeUpdates (listener);//listener 即为监听器实例
manager.setTestProviderEnabled("gps",false);


以下是测试代码,所需权限有:

 

 




 

 


import android.app.Activity;
 import android.content.Context;
 import android.location.Criteria;
 import android.location.Location;
 import android.location.LocationListener;
 import android.location.LocationManager;
 import android.os.Bundle;
 import android.util.Log;
 public class audio extends Activity
 {
     /** Called when the activity is first created. */
     LocationManager locationManager;
     LocationListener llistener;
     String provider;
     public void onCreate(Bundle savedInstanceState)
     {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);
         Criteria criteria = new Criteria();
         criteria.setAccuracy(Criteria.ACCURACY_FINE);
         criteria.setAltitudeRequired(false);
         criteria.setBearingRequired(false);
         criteria.setCostAllowed(true);
         criteria.setPowerRequirement(Criteria.POWER_LOW);
         String serviceName = Context.LOCATION_SERVICE;
         locationManager = (LocationManager) getSystemService(serviceName);
         locationManager.setTestProviderEnabled("gps", true);
         provider = locationManager.getBestProvider(criteria, true);
         Log.d("provider", provider);
         llistener = new LocationListener() {
             @Override
             public void onLocationChanged(Location location)
             {
                 // TODO Auto-generated method stub
                 Log.i("onLocationChanged", "come in");
                 if (location != null)
                 {
                     Log.w("Location", "Current altitude = "
                             + location.getAltitude());
                     Log.w("Location", "Current latitude = "
                             + location.getLatitude());
                 }
                 locationManager.removeUpdates(this);
                 locationManager.setTestProviderEnabled(provider, false);
             }
             @Override
             public void onProviderDisabled(String provider)
             {
                 // TODO Auto-generated method stub
                 Log.i("onProviderDisabled", "come in");
             }
             @Override
             public void onProviderEnabled(String provider)
             {
                 // TODO Auto-generated method stub
                 Log.i("onProviderEnabled", "come in");
             }
             @Override
             public void onStatusChanged(String provider, int status,
                     Bundle extras)
             {
                 // TODO Auto-generated method stub
                 Log.i("onStatusChanged", "come in");
             }
         };
          locationManager.requestLocationUpdates(provider, 1000, (float) 1000.0, llistener);
     }
     protected void onDestroy()
     {
         locationManager.removeUpdates(llistener);
         locationManager.setTestProviderEnabled(provider, false);
         super.onDestroy();
     }

上文是Android开发中GPS导航获取位置时的代码示范,有兴趣的朋友可以按照这些代码操作一下,如果有问题可以在武林技术频道留言,小编会为你一一解答。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表