2011-08-09 69 views
2

我試圖檢索我的當前位置按鈕單擊使用GPS或網絡的編輯框..我嘗試了所有可能的方法,我發現在教程和舊帖子....但我的位置總是空的.. 我沒有得到我錯的地方。? 我對真實的設備有網絡,但沒有互聯網/ GPRS測試它...當前位置始終爲空

這就是我很using..i嘗試同樣與GPS_PROVIDER以及..please幫助我的代碼..

 protected void showCurrentLocation() 
    { 
    geocoder = new Geocoder(this); 
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    locationManager.requestLocationUpdates(
       LocationManager.NETWORK_PROVIDER, 
       MINIMUM_TIME_BETWEEN_UPDATES, 
       MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, 
       new MyLocationListener() 
     ); 

    Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
    System.out.println("loc.."+location); 
    if (location != null) 
    { 
    String message = String.format("Location \n Longitude: %1$s \n Latitude: %2$s", 
       location.getLongitude(), location.getLatitude()); 
     Toast.makeText(ShowActivity.this, message, 
       Toast.LENGTH_LONG).show(); 
    //acTextView.setText(message); 
    try { 
      List<Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 10); //<10> 
      for (Address address : addresses) { 
       System.out.println("my location .."+address.getAddressLine(0)); 
      acTextView.setText(address.getAddressLine(0)); 
      } 



     } catch (IOException e) { 
      Log.e("LocateMe", "Could not get Geocoder data", e); 
     } 
    } 
    else 
    { 
    AlertDialog.Builder alertbox1 = new AlertDialog.Builder(this); 
    alertbox1.setMessage("No GPS or network ..Signal please fill the location manually!"); 
    alertbox1.setNeutralButton("Ok", new DialogInterface.OnClickListener() { 
    public void onClick(DialogInterface arg0, int arg1) 
    {}}); 
    alertbox1.show(); 
    } 

} 

我只是編輯的問題,而不是問新的一個..因爲我的問題仍然是相同的......我沒有改變代碼中的任何東西,現在工作完全正常.... 。但問題是,當我使用GPS_PROVIDER時,它返回準確的地址以及經度和緯度的值..但是當我使用NETWORK_PROVIDER時,它只返回經度和緯度的值,沒有地址...我想檢索完整的地址使用NET因爲GPS不能在室內工作,所以WORK_PROVIDER ...我該怎麼做?

謝謝...... !!

+1

你在模擬器上運行代碼嗎?你總是會在lastKnownLocaton中得到空值...所以試試從開始的默認值或使用Shared Preference。 – Hanry

+0

no ..i在問題中提到「我在具有網絡但沒有互聯網/ gprs的真實設備上測試它......」 –

+0

耆那教徒的回答之後仍然存在問題嗎? – Hanry

回答

3

請檢查您的清單文件。是否添加了這些權限?

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

查看更新的答案:

protected void showCurrentLocation() 
     { 
     geocoder = new Geocoder(this); 
     locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     locationManager.requestLocationUpdates(
        LocationManager.NETWORK_PROVIDER, 
        MINIMUM_TIME_BETWEEN_UPDATES, 
        MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, 
        myLocationListener 
      ); 
     timer = new Timer(); 
     timer.schedule(new GetLastLocation(),20000); 

     } 

    class GetLastLocation extends TimerTask { 

      @Override 
      public void run() { 
       timer.cancel(); 
       locationManager.removeUpdates(locationListener); 
       Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
        System.out.println("loc.."+location); 
        if (location != null) 
        { 
        String message = String.format("Location \n Longitude: %1$s \n Latitude: %2$s", 
           location.getLongitude(), location.getLatitude()); 
         Toast.makeText(ShowActivity.this, message, 
           Toast.LENGTH_LONG).show(); 
        //acTextView.setText(message); 
        try { 
          List<Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 10); //<10> 
          for (Address address : addresses) { 
           System.out.println("my location .."+address.getAddressLine(0)); 
          acTextView.setText(address.getAddressLine(0)); 
          } 



         } catch (IOException e) { 
          Log.e("LocateMe", "Could not get Geocoder data", e); 
         } 
        } 
        else 
        { 
        AlertDialog.Builder alertbox1 = new AlertDialog.Builder(this); 
        alertbox1.setMessage("No GPS or network ..Signal please fill the location manually!"); 
        alertbox1.setNeutralButton("Ok", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface arg0, int arg1) 
        {}}); 
        alertbox1.show(); 
        } 
       } 
       return; 
      } 
     } 

    /** The location listener. */ 
     LocationListener myLocationListener = new LocationListener() { 

      public void onLocationChanged(Location location) { 

      } 
      public void onProviderDisabled(String provider) {} 
      public void onProviderEnabled(String provider) {} 
      public void onStatusChanged(String provider, int status, Bundle extras) {} 
     }; 
+0

我已經添加了所有必要的權限.. –

0

試試這個代碼:

public CurrentLocation(Context ctx) { 
    this.context = ctx; 

    locationManager = (LocationManager) context 
      .getSystemService(Context.LOCATION_SERVICE); 
    @SuppressWarnings("unused") 
    List<String> providers = locationManager.getAllProviders(); 

    Criteria locationCritera = new Criteria(); 
    String providerName = locationManager.getBestProvider(locationCritera, 
      true); 

    location = locationManager.getLastKnownLocation(providerName); 
    locationListener = new MyLocationListener(); 

    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 
      0, locationListener); 



} 
+1

謝謝hanry ..但這不是在我的情況下工作..此代碼是給空指針異常... –

1

的另一個原因是得到來自locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);空回是,如果你的網絡提供商被關閉。在我的手機上,通過設置 - >位置 - >使用無線網絡進行切換。

編輯:

您也可以嘗試:

LocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER) 

,看看是否啓用了網絡提供商或

LocationManager.getProviders(true) 

返回任何啓用的提供者。

更多信息

+0

沒有..網絡提供商打開... –

+0

@jiya - 您需要從代碼中取出System.out.println語句。 Android中沒有控制檯。用Log.d(...)替換。此外,請嘗試將MINIMUM_TIME_BETWEEN_UPDATES設置爲0,並在您的位置偵聽器中放置日誌語句,以查看在確實啓用了網絡提供程序的情況下觸發位置更新時會發生的情況。例如。它可能沒有任何位置被您的手機請求過,這意味着它將一直爲空,直到製作完成。 –

1

當我們指定NETWORK_PROVIDER監聽位置更新,

的Android系統使用的Wi-Fi入侵檢測系統或互聯網接入點的確定位置

當沒有互聯網接入,它返回空值

正巧我也一樣,它不從手機信號塔位置獲得預期

讀到這樣的:no gprs/no wi-fi location updates

0

使用此代碼

if (android.os.Build.VERSION.SDK_INT > 8) { 

     StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); 
     StrictMode.setThreadPolicy(policy); 

    } 

而且還要檢查你的設置 - >位置和安全 - >使用無線網絡啓用與否。

+0

請解釋你的代碼是做什麼的? – Geek