2013-02-04 140 views
1


經過長時間的努力,在面對problem獲取用戶當前的GPS定位。

因此,我認爲如果我們能夠以某種方式通過預安裝的Google地圖應用程序的Current Location修補程序以編程方式獲取位置修復程序?

如果我能夠獲取背景中的位置座標..這可能嗎?
那麼,是不是可以通過使用意圖或廣播接收器以某種方式獲取當前位置座標(假設地圖應用程序是廣播吧..?)如何從Google地圖應用程序獲取用戶當前的GPS位置?

更新#2: Finally..I得到它的工作..

更新:我前面的代碼是here(引擎收錄鏈接)

任何意見,歡迎.. 謝謝..

+0

我沒有得到你的問題......你想通過GPS獲取用戶的當前位置?如果是,那麼它的「非常簡單」:使一個類從LocationListener擴展並覆蓋onLocationChanged方法。您是否已將您的MD5 API密鑰用於Google地圖? – mthama

+0

@mthama是和是..:D Infact這裏是我的[原問題](http://stackoverflow.com/questions/14692202/unable-to-get-current-gps-location-co-ordinates)。我已經完成了演習:D keystore..console..api key.pftt:p我缺少某處某處..或很可能..my電話被竊聽:但是,在這個世界怎麼樣?我的手機的谷歌地圖應用程序可以鎖定到GPS衛星上嗎? :哦,順便說一句,你是否嘗試過在HTC渴望上獲得GPS鎖?或者在基於android 2.3.3的設備上播放它? :| (實時..沒有DDMS thingy) – beerBear

+1

你的代碼是複雜的,看看有什麼不正確的。我只能說你可以使用Criteria,也可以在沒有它的情況下請求GPS_PROVIDER。而「getMyLocation」是不好的類名。 Java中的類名應以大寫字母開頭,且不能是動詞。getMyLocation是一個很好的方法名,但是類應該被命名爲LocationRetreiver或者其他東西:)並且記住要獲得一個你可能希望在外面的GPS解析修復,或者更好地使用FakeGPS https://play.google.com/store/ apps/details?id = com.lexa.fakegps - 如果一切正常,你應該馬上得到定位。 –

回答

0

可以使用無源定位提供商,但它母鹿並不是明確表示它會從GoogleMaps中獲取位置。此外,GoogleMaps應該正在運行以便接收更新。 要從GoogleMaps接收位置更新,您還必須擁有ACCES_FINE_LOCATION權限。此外,您無法啓動GoogleMaps並強制它在沒有用戶交互的情況下更新位置。如果您需要精確的用戶位置,除了使用GPS_PROVIDER之外別無它法。

http://developer.android.com/reference/android/location/LocationManager.html#PASSIVE_PROVIDER

你也可能需要閱讀 http://developer.android.com/guide/topics/location/strategies.html http://developerlife.com/tutorials/?p=1375

+0

好吧..我從網絡獲得位置就好了(實際上並沒有真的把這個代碼放在SO上),但我更傾向於獲得一個精確的位置修正,而不是800 mtrs/fts的精度。 :(是不是有一種方法可以在後臺運行Google地圖(在異步任務中獲取共同作品?:D),並在Google地圖獲得衛星修復後,以某種方式從地圖獲取地理修復,並將其反映到我的應用程序?:| – beerBear

+0

已更新答案。 –

+0

我已經在我的其他*代碼(如禁用軸承和高度,甚至將功率要求設置爲high ..但很遺憾,只是讓我網絡修復:(沒有GPS(在該代碼中,即使GPS圖標也不閃爍......不可見,但是當我檢查通知區域中的GPS複選框時)。選中(我在嘗試地理位置之前明確激活了打開GPS的意圖修復))。 – beerBear

2

好的密碼破譯,這裏是我如何實現我的位置提取程序:

// the listener to listen to the locations 
private LocationListener listener = null; 
// a location manager 
private LocationManager lm = null; 
// locations instances to GPS and NETWORk 
private Location myLocationGPS, myLocationNetwork; 

// instantiates fields 
lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
myLocationNetwork = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
myLocationGPS = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
listener = new myLocationListener(); 

// the listener that gonna notify the activity about location changes 
public class myLocationListener implements LocationListener { 
    @Override 
    public void onLocationChanged(Location location) { 
     // "location" is the RECEIVED locations and its here that you should proccess it 

     // check if the incoming position has been received from GPS or network 
     if (lm.isProviderEnabled(LocationManager.GPS_PROVIDER)) { 
      lm.removeUpdates(this); 
     } else { 
      lm.removeUpdates(listener); 
     } 
    } 
    @Override 
    public void onProviderDisabled(String provider) { 
     lm.removeUpdates(this); 
     lm.removeUpdates(listener);   
    } 
    @Override 
    public void onProviderEnabled(String provider) { 
    } 
    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) { 
    } 
} 

正如你所看到的,有沒有這麼很多技巧。在您實例化偵聽器時,您應該會看到Android設備頂部的GPS圖標。此外,只要您的位置發生變化(即隨着您的設備走動),OnLocationChanged方法將被調用。

另外,有趣的是,如果你只想得到你的位置,有幾種方法可以做到這一點,然後以不同的回報速度和不同的疾病。請檢查GoogleGLM(請求http://www.google.com/glm/mmap,返回json編碼與您的位置一致)服務,三角測量和網絡位置。在上面的片段中,我展示瞭如何通過GPS和網絡獲取位置。希望它有一些幫助... :)

+0

在行'if(lm.isProviderEnabled(LocationManager.GPS_PROVIDER)&&(isNetOrGPs%4 == 0)){'this:'isNetOrGPs%4 == 0' ?我以前的代碼是[這裏 - PasteBin Link](http://pastebin.com/Wm6h9VFV) – beerBear

+0

你可以忽略它。對不起:我從我的項目中複製/粘貼片段並錯過了這件小事。 :) – mthama

+0

最重要的部分是重寫OnLocationChanged。您可以使用Toast.makeText方法來烘烤您的位置作爲反饋。 ;) – mthama

相關問題