使用网络和GPS提供商获取谷歌纵横的问题

时间:2012-01-30 09:38:48

标签: android gps provider

您好我在我的应用中实现了一个位置监听器,它使用网络提供商来获取GPS值。它工作正常,但现在我想使用网络提供商和GPS提供商获得GPS。我正在尝试,但我得到了同样的价值。

这里是我的代码

   Location networkLoc = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
    Location gpsLoc = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

我必须使用networkLoc和gpsLoc吗?

    @Override
    public void onLocationChanged(Location location) {

        double lat1 = (double) (location.getLatitude());//here i get the latitude value..how to know these values are from different provider
        double lng1= (double) (location.getLongitude());
        latituteField.setText(Double.toString(lat));
        longitudeField.setText(Double.toString(lng));


    }

1 个答案:

答案 0 :(得分:1)

可能是这样的情况,如果用户已启用GPS线路

provider = locationManager.getBestProvider(criteria, false);

已经返回LocationManager.GPS_PROVIDER,因此您只需使用相同的提供商查询getLastKnownLocation(provider)两次。试试这个:

    Location networkLoc = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
    Location gpsLoc = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

或者如果你真的不在乎使用哪一个

List<String> providers = locationManager.getProviders(false);
Location[] loc = new Location[providers.size];
int i = 0;
for (String provider: providers){
    loc[i++] = locationManager.getLastKnownLocation(provider);
}

编辑:由于重构问题

//query provider from Location
public void onLocationChanged(Location loc){
    String provider = loc.getProvider();
    if (provider.equals(LocationManager.GPS_PROVIDER)){
        //GPS Location
    } else if (provider.equals(LocationManager.NETWORK_PROVIDER)){
        //Network Location
    }
    ....
}