2011-11-22 80 views
2

我在這裏以下指南:http://www.firstdroid.com/2010/04/29/android-development-using-gps-to-get-current-location-2/GPS定位 - 安卓

我試圖做一個Android應用程序,只是獲取設備GPS座標並在屏幕上顯示出來。我的代碼在模擬器中工作正常,當我telnet到它並鍵入「geo fix 30.0 30.0」,但是當我在手機上使用它時,它永遠不會獲得座標。我的GPS工作正常,因爲我打開了地圖,它有我的確切位置。所以,一定出事了用下面的代碼:

import java.io.IOException; 

import android.app.Activity; 
import android.content.Context; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.util.Log; 
import android.widget.TextView; 
import android.widget.Toast; 

public class BusAppActivity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     /* Use the LocationManager class to obtain GPS locations */ 
     LocationManager locManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
     LocationListener locListener = new MyLocationListener(); 
     locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locListener); 
    } 

    /* To test in the emulator, use the telnet commands: 
    * telnet localhost 5554 
    * geo fix 30.0 30.0 */ 

    /* Class My Location Listener */ 

    public class MyLocationListener implements LocationListener 
    { 
     TextView tv = (TextView) findViewById(R.id.textview); 

     @Override 
     public void onLocationChanged(Location loc){ 
      Log.d("tag", "Finding Latitude"); 
      double lat = loc.getLatitude(); 
      Log.d("tag", "Lat: "+String.valueOf(lat)); 
      Log.d("tag", "Finding Longitude"); 
      double lon = loc.getLongitude(); 
      Log.d("tag", "Lon: "+String.valueOf(lon)); 
      String Text = "My current location is: " + 
      "\nLatitude = " + lat + 
      "\nLongitude = " + lon; 

      // Display location 
      tv.setText(Text); 
     } 

     @Override 
     public void onProviderDisabled(String provider){ 
      Toast.makeText(getApplicationContext(), "Gps Disabled", Toast.LENGTH_SHORT).show(); 
     } 

     @Override 
     public void onProviderEnabled(String provider){ 
      Toast.makeText(getApplicationContext(), "Gps Enabled", Toast.LENGTH_SHORT).show(); 
     } 

     @Override 
     public void onStatusChanged(String provider, int status, Bundle extras){ 

     } 
    } 
} 

這裏是我的清單:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="gsingh.busapp" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk android:minSdkVersion="8" /> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission> 

    <application 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" > 
     <activity 
      android:label="@string/app_name" 
      android:name=".BusAppActivity" > 
      <intent-filter > 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

也許,如果這不是太麻煩了,有人可以建造這個項目,並嘗試在手機上?所有需要添加的是將唯一的textview標記爲「textview」。

+0

你有清單中的以下行:? – NickT

+0

是的,我喜歡。我會將其添加到帖子中。我不知道,如果我沒有那條線,模擬器是否會工作... – gsingh2011

+0

您是否在真正的手機上發生錯誤或只是沒有位置更新?確保你的手機設置正確(GPS和NETWORK位置),不要有飛行模式,等等。 – Jack

回答

1

我找不到任何代碼問題。相同的代碼爲我工作得很好。

我認爲這可能是您的文本視圖(layout_height或其他東西)的問題。

+0

當我從telnet使用geo fix 30.0 30.0時,屏幕上顯示正確的消息,所以文本視圖應該沒問題。你能告訴我你嘗試過什麼類型的設備嗎? – gsingh2011

+0

我試過Galaxy S和Galaxy Europa – Hicham