關於Android使用者定位擷取location值為null的解決方案

來源:互聯網
上載者:User

在玩一個GPS的東西 擷取Location一直為null  後來是從onLocationChanged裡面拿location才搞定了   貼一下吧  代碼太亂

  1. /**  
  2.  * 主要是使用者定位操作  
  3.  *@author hope
  4.  */  
  5. package com.jeedroid.tools;   
  6.   
  7. import java.io.IOException;   
  8. import java.util.List;   
  9. import java.util.Locale;   
  10.   
  11. import android.content.Context;   
  12. import android.location.Address;   
  13. import android.location.Criteria;   
  14. import android.location.Geocoder;   
  15. import android.location.Location;   
  16. import android.location.LocationListener;   
  17. import android.location.LocationManager;   
  18. import android.os.Bundle;   
  19. import android.provider.Settings;   
  20. import android.util.Log;   
  21.   
  22. public class LocationTools   
  23. {   
  24.     private Location location;   
  25. public static LocationManager getLocationManager(Context context)   
  26. {   
  27.     return (LocationManager) context.getSystemService(context.LOCATION_SERVICE);   
  28. }   
  29. //擷取位置資訊   
  30. public String getAddress(Context context)   
  31. {   
  32.     LocationManager locationManager = this.getLocationManager(context);   
  33.     if(!locationManager.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER))   
  34.     {   
  35.         //開啟GPS 需Android2.2以上系統支援   
  36.     android.provider.Settings.Secure.setLocationProviderEnabled(context.getContentResolver(), LocationManager.GPS_PROVIDER, false);   
  37.     }   
  38.     return doWork(context);   
  39. }   
  40.   
  41. private String doWork(Context context)   
  42. {   
  43.     String addres="";   
  44.     LocationManager locationManager = this.getLocationManager(context);   
  45.     Criteria criteria = new Criteria();   
  46.     // 獲得最好的定位效果     
  47.     criteria.setAccuracy(Criteria.ACCURACY_FINE);     
  48.     criteria.setAltitudeRequired(false);     
  49.     criteria.setBearingRequired(false);     
  50.     criteria.setCostAllowed(false);     
  51.     // 使用省電模式     
  52.     criteria.setPowerRequirement(Criteria.POWER_LOW);     
  53.     String provider =locationManager.getBestProvider(criteria, true);   
  54.     Log.i("provider>>>>>>", provider);   
  55.     //獲得當前位置  location為空白是一直取  從onLocationChanged裡面取   
  56.     while(location==null)   
  57.     {   
  58.     location =locationManager.getLastKnownLocation(provider);   
  59.     }   
  60.     //locationListener   
  61.     LocationListener locationListener = new LocationListener()   
  62.     {   
  63.   
  64.         @Override  
  65.         public void onLocationChanged(Location location)   
  66.         {   
  67.             LocationTools.this.location=location;   
  68.         }   
  69.   
  70.         @Override  
  71.         public void onProviderDisabled(String provider)   
  72.         {   
  73.                
  74.         }   
  75.   
  76.         @Override  
  77.         public void onProviderEnabled(String provider)   
  78.         {   
  79.                
  80.         }   
  81.   
  82.         @Override  
  83.         public void onStatusChanged(String provider, int status, Bundle extras)
      
  84.         {   
  85.                
  86.         }   
  87.            
  88.     };   
  89.     locationManager.requestLocationUpdates(provider, 1000, 10, locationListener);   
  90.      
  91.     Geocoder geo = new Geocoder(context,Locale.getDefault());   
  92.     try  
  93.     {   
  94.         List<Address> address=geo.getFromLocation(location.getLatitude(), location.getLongitude(), 1);   
  95.         if(address.size()>0)   
  96.         {   
  97.             addres=address.get(0).getAddressLine(0);   
  98.         }   
  99.     } catch (IOException e)   
  100.     {   
  101.         // TODO Auto-generated catch block   
  102.         e.printStackTrace();   
  103.     }   
  104.     return addres;   
  105. }   
  106. }  

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.