2011-11-22 79 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