2012-04-29 196 views
2

我一直在研究這段代碼一段時間,一切似乎都沒問題,但是我的編輯器,eclipse總是告訴我投myManager。代碼如下,GPS當前位置

package com.gps.project; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.TextView; 
import android.content.Context; 
import android.widget.Button; 
/*LOcation based API*/ 
import android.location.LocationManager; 


public class GpsActivity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     /*Create ab ttuon we are to use in our GPS*/ 
     final Button gpsButton=(Button) findViewById(R.id.gpsButton); 
     gpsButton.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 
       // TODO Auto-generated method stub 
       LoadCoords(); 

      } 
     }); 

    } 
    public void LoadCoords(){ 
     /*Create two textfields that will contain information as latitudes and longitudes*/ 
     TextView latText=(TextView) findViewById(R.id.latText); 
     TextView lngText=(TextView) findViewById(R.id.lngText); 

     /*Creation of a location manager from which we are to pull the location values*/ 
     LocationManager myManager=(LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     /*To pull the coordinates from myManager, we use the getCurrentLocation() method.*/ 
     Double latPoint = myManager.getCurrentLocation("gps").getLatitude(); 
     Double lngPoint = myManager.getCurrentLocation("gps").getLongitude(); 
     /*Finally, take the new Double values and pass them to your TextViews:*/ 
     latText.setText(latPoint.toString()); 
     lngText.setText(lngPoint.toString()); 
    } 
} 

如果有人幫我糾正我的錯誤,會很高興。我打算獲得經度和緯度。

+0

嗯,我找不到''getCurrentLocation'in的LocationManager'http實例方法android/location/LocationManager.html 應該是'getLastKnownLocation'我想。 –

+0

我的回答有幫助嗎?不要忘記+1並接受如果是這樣的話,謝謝:) –

回答

0

正如我在我的回覆中所說的,我認爲你應該使用getLastKnownLocation而不是getCurrentLocation,這是我在文檔中找不到的(可能不推薦使用)。

返回指示從最後已知位置 數據位置鎖定從給定的供應商處獲得。這可以在沒有 啓動提供程序的情況下完成。請注意,如果設備已關閉並移動到另一個 位置,則該位置可能已過時,例如 。如果提供者當前被禁用,則返回null。

,你需要添加permission是://developer.android.com/reference/:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> 
+0

Thanx。那些幫助。只是古怪我應該在我的清單xml文件中添加什麼權限。 – user1364687

+0

我已經用適當的權限編輯了我的答案。 –

+0

Thanx。我剛剛上傳了另一個標題爲將數據上傳到遠程網站的問題.....,你認爲你可以再次獲得幫助嗎? – user1364687