【问题标题】:Network provider generates a 2 copies of a location网络提供商生成 2 个位置副本
【发布时间】:2012-12-27 21:39:43
【问题描述】:

如果我通过网络提供商生成的值为空,我会尝试首先通过 gps 提供商生成位置。 我的问题是网络提供商两次生成相同的位置。我将信息发送到数据库,它以相同的时间(相同的秒数!)发送相同的位置。 这是一些代码:

gpsLocation = requestUpdatesFromProvider(LocationManager.GPS_PROVIDER, R.string.not_support_gps);
networkLocation = requestUpdatesFromProvider(LocationManager.NETWORK_PROVIDER, R.string.not_support_gps);
    // Update the UI immediately if a location is obtained.
if (gpsLocation != null) 
    updateUILocation(gpsLocation);
else
    updateUILocation(networkLocation);

这里是 requestUpdateFromProvider:

private Location requestUpdatesFromProvider(final String provider, final int errorResId) 
{
    Location location = null;
    if (mLocationManager.isProviderEnabled(provider)) 
    {
        mLocationManager.requestLocationUpdates(provider, TEN_SECONDS, TEN_METERS*2, listener);
        location = mLocationManager.getLastKnownLocation(provider);
    } 
    else
    {
        Toast.makeText(this, errorResId, Toast.LENGTH_LONG).show();
    }
    return location;
}

这里是更新UILocation:

private void updateUILocation(Location location) 
{
    // We're sending the update to a handler which then updates the UI with the new location.
    Message.obtain(mHandler,
            UPDATE_LATLNG,
            location.getLatitude() + ", " + location.getLongitude()).sendToTarget();

    // Bypass reverse-geocoding only if the Geocoder service is available on the device.
    if (mGeocoderAvailable) 
        doReverseGeocoding(location);
}

这里是处理程序:

    mHandler = new Handler() 
    {
        public void handleMessage(Message msg) 
        {
            if(msg.what == UPDATE_ADDRESS) //UPDATE_ADDRESS = 1
            {
                LocationService.this.address = (String) msg.obj; // update a string that show the user address
                new SendLocation(LocationService.this.id,(String)msg.obj);//send the info to the db.
            }
        }
    };

SendLocation 正在做它应该正确做的事情,所以你不应该关注它。

编辑

我忘了说第一段代码是从构造函数调用的方法。

【问题讨论】:

    标签: java android gps geolocation


    【解决方案1】:

    如果创建重复位置不是您的错,那么只需调用同步的forwardLocation(Location loc) 方法,该方法存储最后接收到的位置的时间戳。 当最后存储的时间戳等于前一个时,此方法将忽略位置。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多