android getLastKnownLocation()擷取當前位置為null解決方案

來源:互聯網
上載者:User

 用Google map api開發是一件令人糾結的事情

使用 getLastKnownLocation() 總是莫名奇妙的擷取不到值,為null,然後又莫名其妙的好了。調試了半天也沒用,上網找資料,前篇一律的一個答案

所以以後就讓它自生自滅吧。

LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);Criteria criteria = new Criteria();criteria.setAccuracy(Criteria.ACCURACY_FINE);String provider = locationManager.getBestProvider(criteria, true);locationManager.requestLocationUpdates(provider, 2000, 0, this); 
mostRecentLocation = locationManager.getLastKnownLocation(provider);

就這6句也不用多寫!

同時擷取的當前位置也不準確,不知道人家google自己怎麼用的,是我修鍊不夠嗎?

經過測試發現了,

加上一句while (mostRecentLocation==null)

於是它就會一直尋找,差不多3分鐘後就找到了,所以還是要有耐心哦

受困惑的同志們,不妨試一試!!

 

說到底還是js網頁開發吧,幹嘛還要撤到android Java上去呢?

我的思路有時候還是對的 ,就是對細節把握不行,而且對莫名奇妙的bug莫名其妙的煩躁,一點解決的耐心都沒有。

這都是問題!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.