尝试获取当前位置时获取异常服务Latitude&经度

时间:2012-11-08 06:06:18

标签: android google-app-engine google-maps

这个问题可能重复,但没有得到满意的答案所以我在这里抛出我的问题。我正在尝试使用地理编码器获取当前位置纬度和经度,在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");
           }

我的项目版本是谷歌api 4.0  我正在测试我的项目在三星选项卡有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 个答案:

答案 0 :(得分: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);
    }