LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if(location == null)
        {
         location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        }
        
        Log.i(TAG, "location= " + location +" locationManager= " + locationManager);
        String addressString = null;
        double latitude=location.getLatitude();
        double longitude=location.getLongitude();
        Geocoder gc=new Geocoder(this,Locale.getDefault());
        try
        {
            List<Address> addresses=gc.getFromLocation(latitude, longitude,1);
            StringBuilder sb=new StringBuilder();
            
            Log.i(TAG, "addresses= " + addresses);
            
            if(addresses.size()>0)
            {
                Address address=addresses.get(0);
                for(int i=0;i<address.getMaxAddressLineIndex();i++)
                    sb.append(address.getAddressLine(i)).append("\n");
                    
                    sb.append(address.getLocality()).append("\n");
                    sb.append(address.getPostalCode()).append("\n");
                    sb.append(address.getCountryName());
                    addressString=sb.toString();
                    Log.i(TAG, "latitude= " + latitude +" longitude= " + longitude + " city= " + addressString);
            }
        }
        catch(IOException e)
1. 为什么获得的location对象都是null啊?
2. 把经纬度写死后,用Geocoder解析时,返回的是一个 空list。哪位大侠能解释一下,权限也设置了,不知道问题出在哪,求解答。

解决方案 »

  1.   

    1.如果用的是虚拟机,先send一下location就好了,在Emolato下面找。2.你看看你的Locale.getDefault()是否包括你写的经纬度。
      

  2.   

    getLastKnownLocation这个东东貌似常常取不到location,建议试一下requestLocationUpdates。另模拟器需要自己模拟GPS信号(DDMS有这个功能),还有手机在室内是取不到GPS信号。
      

  3.   


    send过了,经纬度没有问题,我直接搜的城市经纬度,是在范围内的,而且是没有报相关的异常。另外,
    我直接把经纬度写到参数中,
    addresses=gc.getFromLocation(latitude, longitude,1);
    为什么返回的也是空列表啊?
    不知道什么原因?
      

  4.   


    "getLastKnownLocation常常取不到location“是什么原因啊,需要设置什么东西吗?"requestLocationUpdates",我是不想频繁的更新的,最好是程序启动时更新一次位置就行。自己模拟gps信号是什么意思,能不能说的详细一点。
    我就是在建模拟器的时候,把支持gps属性加上了
    其它就是在DDMS中Emulator Control中,send一下经纬度。