谷歌地图Android的API V2创建一个新的LocationSource [英] Google Maps Android API v2 creating a new LocationSource

查看:522
本文介绍了谷歌地图Android的API V2创建一个新的LocationSource的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

A LocationSource 的<一个定义href="https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/LocationSource">Google地图Android的API V2 。

有用于使用GoogleMap作为位置提供。默认情况下,该位置源由手机上的GPS模块提供的。

It is used for googlemap as the location provider. By default, the location source is provided by the gps module on the phone.

但现在我想用一个又一个的位置源,位置数据将定期发送到Android设备上。

But now I want to use a another Location source, the location data will be sent to android device periodically.

我不知道如何来实现这个接口。是否有任何的例子在那里?谁能帮我呢?该文件并没有说什么。

I have no idea how to implement this interface. Are there any example out there? Can anyone help me with it? The document did not say anything about it.

推荐答案

下面是一个简单的执行 LocationSource 接口。就我而言,我注册了GPS和网络位置提供商。正如刚才@CommonsWare,实施很可能取决于你的需求。我建议阅读有关位置服务的官方文档,以便更好地理解如何利用你的需求,并节省电池电量

Here is a simple implementation of LocationSource interface. In my case I'm registering both GPS and Network location providers. As mentioned by @CommonsWare, implementation may very depending on your needs. I would suggest reading official documentation about Location service in order to better understand how to utilize your needs and save some battery power

public class CurrentLocationProvider implements LocationSource, LocationListener
{
    private OnLocationChangedListener listener;
    private LocationManager locationManager;

    public CurrentLocationProvider(Context context)
    {
        locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    }

    @Override
    public void activate(OnLocationChangedListener listener)
    {
        this.listener = listener;
        LocationProvider gpsProvider = locationManager.getProvider(LocationManager.GPS_PROVIDER);
        if(gpsProvider != null)
        {
            locationManager.requestLocationUpdates(gpsProvider.getName(), 0, 10, this);
        }

        LocationProvider networkProvider = locationManager.getProvider(LocationManager.NETWORK_PROVIDER);;
        if(networkProvider != null) {
            locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000 * 60 * 5, 0, this);
        }
    }

    @Override
    public void deactivate()
    {
        locationManager.removeUpdates(this);
    }

    @Override
    public void onLocationChanged(Location location)
    {
        if(listener != null)
        {
            listener.onLocationChanged(location);
        }
    }

    @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

    }
}

这里是我会怎么使用这个类:

And here is how I would use this class:

protected void setUpMap() {
    //init routine
    .......

    this.map.setLocationSource(new CurrentLocationProvider(this));
    .......       
}

修改请注意,这个解决方案已经过时了!您需要使用<一个href="https://developer.android.com/reference/com/google/android/gms/location/FusedLocationProviderApi.html"相对=nofollow> FusedLocationProviderApi与<一个结合 href="https://developer.android.com/reference/com/google/android/gms/common/api/GoogleApiClient.html"相对=nofollow> GoogleApiClient 跟踪当前位置

EDIT Please not that this solution is obsolete! You need to use FusedLocationProviderApi in conjunction with GoogleApiClient for tracking current location

这篇关于谷歌地图Android的API V2创建一个新的LocationSource的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆