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"/>
任何幫助或建議將不勝感激謝謝任何方式!