2011-08-05 44 views
0

我作了一個發現位置的應用程序,顯示在TextView的經度和緯度。這裏是我的代碼:如何使這個位置查找(GPS)在Android上的作品?

package com.application.geocoding; 

import android.app.Activity; 
import android.os.Bundle; 
import android.content.Context; 
import android.location.Location; 
import android.location.LocationManager; 
import android.widget.TextView; 

public class main extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     LocationManager locationManager; 
     String context = Context.LOCATION_SERVICE; 
     locationManager = (LocationManager)getSystemService(context); 

     String provider = LocationManager.GPS_PROVIDER; 
     Location location = 
      locationManager.getLastKnownLocation(provider); 

     updateWithNewLocation(location); 
    } 

    private void updateWithNewLocation(Location location) { 
     String latLongString; 
     TextView myLocationText; 
     myLocationText=(TextView)findViewById(R.id.textView1); 

     if (location != null) { 
      double lat = location.getLatitude(); 
      double lng = location.getLongitude(); 
      latLongString = "Lat:" + lat + "\nLong:" + lng; 
     } else { 
      latLongString = "No location found"; 
     } 
     myLocationText.setText("Your Current Position is:\n" + 
           latLongString); 
    } 
} 

和這裏的清單:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
     package="com.application.geocoding" 
     android:versionCode="1" 
     android:versionName="1.0"> 
    <uses-sdk android:minSdkVersion="8" /> 
    <uses-permission android:name="android.permission.INTERNET"/> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> 

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

    </application> 
</manifest> 

代碼仍然沒有工作和只能顯示「找不到位置」textview ..有誰知道如何解決這個問題??謝謝..

回答

1

您可以使用此:

LocationListener myLocationListener = new LocationListener() { 
      public void onLocationChanged(Location location) { 
      String latLongString; 
       TextView myLocationText; 
       myLocationText=(TextView)findViewById(R.id.textView1); 
       double lat = location.getLatitude(); 
       double lng = location.getLongitude(); 
       latLongString = "Lat:" + lat + "\nLong:" + lng; 
       myLocationText.setText("Your Current Position is:\n" + 
          latLongString); 
} 
     } 

     public void onProviderDisabled(String provider) { 

     } 

     public void onProviderEnabled(String provider) { 
     } 

     public void onStatusChanged(String provider, int status, 
       Bundle extras) { 
     } 
    }; 

    int t = 5000; // millisekunder 
    int dist = 5; // meter 
    locationManager.requestLocationUpdates(provider, t, dist, myLocationListener); 
+0

如何使代碼的工作只有一次?該代碼總是試圖找到新的位置?我需要它只工作一次.. tahnks –

+0

如果你希望在獲得你的第一個位置後停下來,然後當你想停止它時使用 locationManager.removeUpdates(myLocationListener)。 或者使用locationManager.requestSingleUpdate,甚至可能對您的目的更有效。 –

+0

Lasern:謝謝finn ..它真的對我有用..再次非常感謝你..祝你有美好的一天.. :) –

0

你試過執行LocationListener和覆蓋onLocationChanged(Location loc)?另外,你在使用模擬器嗎?編輯:你可以找到一個例子here

0

從活動開始就得到了GPS座標,但在那個時候,如果你不送GPS從DDMS座標那麼它將返回null

,而這將如果你給你只是一個時間值希望得到你需要定義監聽器,像LocationListener的的繼續GPS值

看到的教程鏈接

http://developer.android.com/guide/topics/location/obtaining-user-location.html

http://www.firstdroid.com/2010/04/29/android-development-using-gps-to-get-current-location-2/

http://www.androidcompetencycenter.com/?s=GPS

0

你不是實際調用GPS定位採集,但要求最後一個。如果它從未被調用,它將始終返回null。

更好的辦法是在onResume()被註冊LocationListener並通過locationManager.requestLocattionUpdates()locationManager.removeUpdates()註銷它onPause(),恭恭敬敬。