2012-11-08 25 views
1

這個問題可能是重複的,但沒有得到滿意的答案,所以我在這裏扔我的問題。我試圖使用地理編碼器獲取當前位置經緯度,之前2天我是能夠得到緯度和經度,但今天越來越異常:產生java.io.IOException:服務不可用當嘗試獲取當前位置緯度和經度時獲取異常服務

這裏是我試圖讓經緯度

//**current latitude and longitude 

     try{ 
     lm = (LocationManager) RestaurantDetail.this.getSystemService(Context.LOCATION_SERVICE); 
     criteria = new Criteria(); 
     bestProvider = lm.getBestProvider(criteria, false); 
     location = lm.getLastKnownLocation(bestProvider); 
     } 
     catch(Exception e){ 

      Log.e("Finding Current Location:",e.toString()); 
     } 

     if (location == null){ 
      Toast.makeText(RestaurantDetail.this,"Location Not found",Toast.LENGTH_LONG).show(); 

     }else{ 

     try { 
      geocoder = new Geocoder(RestaurantDetail.this);  
      user = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1); 
      lat=(double)user.get(0).getLatitude(); 
      lng=(double)user.get(0).getLongitude(); 
      lat1=lat; 
      long1=lng; 

      Log.e("Latitude:","Current:"+lat1+""+long1+"\n"+"add"+lat2+""+long2); 


     try{ 
      float results[] = new float[3]; 
      Location.distanceBetween(lat1,long1,lat2,long2, results); 
      float distance = results[0]; 
      distance=(float) ((distance)*0.000621371); 
      Tv_restorentdetail_disData.setText(""+distance+"miles"); 
      }catch(Exception e){ 

       Log.e("Counting Distance Exception:",e.toString()); 
      } 
      }catch (Exception e) { 
       e.printStackTrace(); 
       Tv_restorentdetail_disData.setText("Service Not Available"); 
      } 

我的項目的方式版本是4.0與谷歌api 和我在三星標籤這裏有Android版本> 4.0 測試我的項目是允許的名單我已經給到清單文件

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

任何幫助或建議將不勝感激謝謝任何方式!

回答

1
private LocationManager locationManager; 
private LocationListener locationListener; 
public void Get_current_location() 
    { 
     locationManager=(LocationManager)this.getSystemService(LOCATION_SERVICE); 
     final ProgressDialog pg=new ProgressDialog(this); 
     pg.setTitle("Fetching Location"); 
     pg.setMessage("please wait...."); 
     pg.show(); 
     locationListener=new LocationListener() 
     { 

      public void onStatusChanged(String provider, int status, Bundle extras) 
      { 
       // TODO Auto-generated method stub 

      } 

      public void onProviderEnabled(String provider) 
      { 
       // TODO Auto-generated method stub 

      }    
      public void onProviderDisabled(String provider) 
      { 
       // TODO Auto-generated method stub 
       Intent intent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
       startActivity(intent); 
      } 

      public void onLocationChanged(Location location) 
      { 
       // TODO Auto-generated method stub 
       Geocoder gcd=new Geocoder(getBaseContext()); 
       try 
       { 
        List<Address>addresses=gcd.getFromLocation(location.getLatitude(), location.getLongitude(), 5); 
        Address address=addresses.get(0); 
        gps_countryName=address.getCountryName().toString(); 
        gps_stateName=address.getAdminArea().toString(); 
        gps_cityName=address.getLocality().toString();      
        gps_countryCode=address.getCountryCode().toString(); 
        Log.i("gps countrycode", gps_countryCode);      
        locationManager.removeUpdates(locationListener);  
       } 
       catch(Exception e) 
       { 
        e.printStackTrace(); 
       } 

      } 
     }; 
     locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener); 
    } 
相關問題