让你的app可以定位

用户带和他们的移动设备与他们几乎无处不在。移动应用程序的独特功能之一是位置意识。明确的位置定位,并明智地使用信息,可以让使用者体验带来了更多的便利。

这篇文章将告诉你,如何在你的Andr​​oid应用程序将基于位置的服务。您会学到一些方法,以接收位置更新和最佳做法。本文的重点分为下面三点,下面会一一讲到,并指出其中的重点内容:

1.使用LocationManager(学习如何配置你的app,在它能接受到android的位置更新之前)

2.获取当前位置(学习如何使用底层位置技术平台上可用来获得当前位置)

3.显示位置地址(学习如何翻译为地址位置坐标对用户是可读的)

.使用LocationManager

manifest中声明网络权限:

 

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
 

获得位置管理的引用:

LocationManager是主要的类,通过这个应用程序可以访问的位置服务在Android上。类似于其他系统服务,可以得到一个引用从调用getSystemService()方法。如果你的应用程序准备接收位置更新前景(在一个活动),您应该执行该步骤通常在onCreate()方法。代码如下:

 

LocationManager locationManager =
        (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
 

获得位置的提供者:

取决于您的应用程序的用例,你必须选择一个特定位置,或多个提供者、提供者基于类似的权衡。例如,一个的兴趣点签入应用程序将需要更高的定位精度比说,零售商店定位器,一个城市水平位置修正就足够了。下面的代码片段要求一个提供者支持的GPS代码如下:

 

LocationProvider provider =
        locationManager.getProvider(LocationManager.GPS_PROVIDER);
 

添加条件增加精确度:

你可以提供一些输入标准如精度、电力需求,货币成本等等,Android决定一个最接近的匹配位置提供者。下面的代码片段要求一个位置提供者与细准确性和没有货币成本。注意,标准可能没有解决任何提供者,在这种情况下,将返回null。代码如下:

 

// Retrieve a list of location providers that have fine accuracy, no monetary cost, etc
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setCostAllowed(false);
...
String providerName = locManager.getBestProvider(criteria, true);

// If no suitable provider is found, null is returned.
if (providerName != null) {
   ...
}
 

验证位置提供者是否可以:

如果位置提供者是禁用的,您可以为用户提供一个机会,使它在设置一个Intent

ACTION_LOCATION_SOURCE_SETTINGS代码如下:

 

@Override
protected void onStart() {
    super.onStart();

    // This verification should be done during onStart() because the system calls
    // this method when the user returns to the activity, which ensures the desired
    // location provider is enabled each time the activity resumes from the stopped state.
    LocationManager locationManager =
            (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    final boolean gpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

    if (!gpsEnabled) {
        // Build an alert dialog here that requests that the user enable
        // the location services, then when the user clicks the "OK" button,
        // call enableLocationSettings()
    }
}

private void enableLocationSettings() {
    Intent settingsIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
    startActivity(settingsIntent);
}

 

 

.获取当前位置

设置位置的监听

LocationManager类公开的一些方法来应用程序接收位置更新。在其最简单的形式,你注册一个事件侦听器,确定位置的经理,你想接收位置更新,并指定最小间隔时间和距离,接收位置更新。这个onLocationChanged()回调将调用的频率与时间和距离的间隔。在样例代码片段,下面的位置侦听器被设置为接收通知至少每10,如果设备移动超过10米。其他的回调方法通知应用程序状态更改任何来自提供者的位置。代码如下:

 

private final LocationListener listener = new LocationListener() {
@Override
    public void onLocationChanged(Location location) {
        // A new location update is received.  Do something useful with it.  In this case,
        // 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();
...
        }
    ...
};
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
        10000,          // 10-second interval.
        10,             // 10 meters.
        listener);
 

停止位置更新:

如果用户不再使用导航或者使用别的应用时,你应该停止位置更新通过调用removeUpdates()onStop()(onStop()时调用活动不再是可见的,代码如下:

 

protected void onStop() {
    super.onStop();
    mLocationManager.removeUpdates(listener);
}
 

 

 

 

.显示位置地址

执行反向地址编码:

逆向地理编码是翻译的过程,一个人类可读的纬度经度坐标地址注意,在幕后,API是依赖于web服务。如果这样的服务不可用的设备,API将抛出一个“Service not Available exception”或者返回一个空列表的地址

一个helper方法称为isPresent()添加于Android 2.3(API级别9)来检查服务的存在,

下面的代码片段演示了如何使用该API来执行反向地理编码Geocoder代码如下:

 

private final LocationListener listener = new LocationListener() {
public void onLocationChanged(Location location) {
        // Bypass reverse-geocoding if the Geocoder service is not available on the
        // device. The isPresent() convenient method is only available on Gingerbread or above.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD && Geocoder.isPresent()) {
            // Since the geocoding API is synchronous and may take a while.  You don't want to lock
            // up the UI thread.  Invoking reverse geocoding in an AsyncTask.
            (new ReverseGeocodingTask(this)).execute(new Location[] {location});
        }
    }
    ...
};
// AsyncTask encapsulating the reverse-geocoding API.  Since the geocoder API is blocked,
// we do not want to invoke it from the UI thread.
private class ReverseGeocodingTask extends AsyncTask<Location, Void, Void> {
    Context mContext;
public ReverseGeocodingTask(Context context) {
        super();
        mContext = context;
    }
@Override
    protected Void doInBackground(Location... params) {
        Geocoder geocoder = new Geocoder(mContext, Locale.getDefault());
Location loc = params[0];
        List<Address> addresses = null;
        try {
            // Call the synchronous getFromLocation() method by passing in the lat/long values.
            addresses = geocoder.getFromLocation(loc.getLatitude(), loc.getLongitude(), 1);
        } catch (IOException e) {
            e.printStackTrace();
            // Update UI field with the exception.
            Message.obtain(mHandler, UPDATE_ADDRESS, e.toString()).sendToTarget();
        }
        if (addresses != null &s;&s; addresses.size() > 0) {
            Address address = addresses.get(0);
            // Format the first line of address (if available), city, and country name.
            String addressText = String.format("%s, %s, %s",
                    address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : "",
                    address.getLocality(),
                    address.getCountryName());
            // Update the UI via a message handler.
            Message.obtain(mHandler, UPDATE_ADDRESS, addressText).sendToTarget();
        }
        return null;
    }
}

以上就是详细的对app定位的一些讲解和事例代码,只有部分代码,为的是突出重点和帮助大家理解,下面我贴上该例子全部的源代码,代码也不多,注释也很清楚,希望能帮到大家。

猜你喜欢

转载自phantomes.iteye.com/blog/1703263