简体   繁体   English

网络提供商生成2个位置的副本

[英]Network provider generates a 2 copies of a location

I'm trying to generate a location first through the gps provider if I get a null I'm generating through the network provider. 我正在尝试通过gps提供程序首先生成一个位置,如果我得到一个null,我正在通过网络提供程序生成。 My problem is that the network provider generates the same location twice.. I send the information to the db and it sends the same location with the same time(same seconds!). 我的问题是网络提供商两次生成相同的位置..我将信息发送到数据库,并在同一时间发送相同的位置(相同的秒数!)。 Here is some code: 这是一些代码:

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);

Here is the requestUpdateFromProvider: 这是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;
}

Here is the updateUILocation: 这是updateUILocation:

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);
}

Here is the handler: 这是处理程序:

    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.
            }
        }
    };

The SendLocation is doing what it suppose to do correctly so you shouldn't pay attention to it. SendLocation正在执行它假设正确执行的操作,因此您不应该注意它。

EDIT 编辑

I forgot to say that the first piece of code is a method called from the constructor. 我忘了说第一段代码是从构造函数调用的方法。

If its not your fault that dupplicate locations are created, then just call a syncronized forwardLocation(Location loc) method, that stored the timestamp of the last received location. 如果创建了重复位置不是您的错,那么只需调用同步的forwardLocation(Location loc)方法,该方法存储最后接收位置的时间戳。 This methods shall ignore a location when its last stored timestamp is equal to the previous one. 当最后存储的时间戳等于前一个时间戳时,此方法应忽略该位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM