Android GPS按计算平均值设置位置

时间:2014-03-24 17:56:27

标签: android gps location locationlistener

我有一个实现LocationListener的类,因此在onLocationChanged方法中,一旦gps获取了信息,我就会得到当前位置。

我的问题是我想在列表中保存所需的所有坐标(例如5个坐标),并在得到第五个坐标后计算平均值并将结果更新为我的位置。

我唯一的问题是如何将我的计算坐标设置为我的位置。我是否必须使用模拟位置或有更好的方法?

谢谢。

编辑:我在onCreate中设置了位置管理器:

//GPS sensor
    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    criteria.setPowerRequirement(Criteria.NO_REQUIREMENT);
    criteria.setCostAllowed(false);   

    bestProvider = locationManager.getBestProvider(criteria, true);

    Log.d(TAG, "bestProvider: " + String.valueOf(bestProvider));

    locationManager.requestLocationUpdates(bestProvider, 2000, 2, locationListener);

这是Listner:

LocationListener locationListener = new LocationListener(){

    @Override
    public void onLocationChanged(Location location) {

        latitude = location.getLatitude();
        latitudeList.add(latitude);
        longitude = location.getLongitude();
        longitudeList.add(longitude);
        altitude = location.getAltitude();
        altitudeList.add(altitude);

        Log.d(TAG, "latitude: " + String.valueOf(latitude));
        Log.d(TAG, "longitude: "+ String.valueOf(longitude));
        Log.d(TAG, " altitude " + String.valueOf(altitude));

        locationCounter++;
        Toast.makeText(getApplicationContext(), ""+locationCounter,
                Toast.LENGTH_LONG).show();

        if(locationCounter == COUNTERRESET){

            for(int i = 0; i < latitudeList.size();i++){
                latitudeAverage = latitudeAverage + latitudeList.get(i);
                longitudeAverage = longitudeAverage + longitudeList.get(i);
                altitudeAverage = altitudeAverage + altitudeList.get(i);
            }

            latitudeAverage = latitudeAverage / latitudeList.size();
            longitudeAverage = longitudeAverage / longitudeList.size();
            altitudeAverage = altitudeAverage / altitudeList.size();

            locationUpdate = new Location(bestProvider);
            location.setLatitude(latitudeAverage);
            location.setLongitude(longitudeAverage);
            location.setAltitude(altitudeAverage);

            **HERE I WANT TO SET LOCATION UPDATE AS MY GPS POSITION**

            locationCounter = 0;
            latitudeAverage = 0;
            longitudeAverage = 0;
            altitudeAverage = 0;
            latitudeList.clear();
            longitudeList.clear();
            altitudeList.clear();
        }


    }

    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub

    }

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

    }

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

    }

};

0 个答案:

没有答案