首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >网络提供商生成一个位置的两个副本。

网络提供商生成一个位置的两个副本。
EN

Stack Overflow用户
提问于 2013-01-14 00:12:34
回答 1查看 115关注 0票数 5

我试图首先通过gps提供者生成一个位置,如果我通过网络提供者生成一个null。我的问题是,网络提供商两次生成相同的位置。我将信息发送到db,它以相同的时间(相同的秒!)发送相同的位置。以下是一些代码:

代码语言:javascript
复制
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:

代码语言:javascript
复制
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;
}

下面是updateUILocation:

代码语言:javascript
复制
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);
}

下面是处理程序:

代码语言:javascript
复制
    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正在做它应该做的正确的事情,所以您不应该注意它。

编辑

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

EN

回答 1

Stack Overflow用户

发布于 2014-06-30 18:50:50

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14309799

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档