Android GPS 使用 备忘

来源:互联网 发布:虎嗅商学院源码 编辑:程序博客网 时间:2024/05/16 06:07

1) AndroidManifest.xml中:

需要加入权限

    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <!--
     The following two permissions are not required to use
     Google Maps Android API v2, but are recommended.
    -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATIO"/>

其中,ACCESS_COARSE_LOCATION在developer.android.com中的参考文档中Allows an app to access approximate location derived from network location sources such as cell towers and Wi-Fi.而,ACCESS_FINE_LOCATION则是:

Allows an app to access precise location from location sources such as GPS, cell towers, and Wi-Fi.因此,ACCESS_FINE_LOCATION(GPS,数据,wifi)包含了ACCESS_CORSE_LOCATION(数据,wifi)权限。

另外,如果要使用Android模拟器调试位置信息,则需要加入权限ACCESS_MOCK_LOCATION。

看到两种在模拟器中设置当前位置信息的方式:

a). 通过Eclipse的ADT提供的工具,右上角的 Open perspective --> DDMS --> Manual,在这里设置经纬度信息,点击send;

b). 通过Emulator的控制端口(console port)设置,一个Emulator占用两个端口:一个控制端口(console port),一个abd端口,同一个模拟器的abd的端口号比console端口号大 1,在启动模拟器的时候,看到的就是console port,可以注意,第二个开启的模拟器的console端口号总是比前一个大 2。 为了连接上一个emulator的控制器,使用命令 telnet localhost <console-port>,比如: telnet localhost 5554. 然后使用命令 geo fix <longitude value> <latitude value> 进行位置信息的设定:

telnet localhost <console-port>

geo fix <longitude value> <latitude value>


2)编程中,使用LocationManager得到GEO location,具体比如:

通过 this.locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE)设定LocationManager,然后可以设定一些基本的参数:

this.locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);

这里,requestLocationUpdates方法设定了在位置信息如何更新和处理更新的方法:第一个参数表示位置信息的来源,可以使用NETWORK_PROVIDER,也可以使用GPS_PROVIDER,两者相比,network可以在室内使用,但它们是根据一些已知固定站点来推算位置,位置信息不精确;而GPS只能在室外使用,耗电量大,但是较为精确;第二个参数表示两次位置更新的最短时间;第三个参数表示两次位置更新的最短距离;第四个参数表示监听这个位置更新的对象,这个对象需要实现LocationListener接口,由于这里当前类已经实现了LocationListener接口,使用的是this。

当然,实现LocationListener接口需要完成方法:

public void onLocationChanged(Location location) {
/**

*/
}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
public IBinder onBind(Intent arg0) {
return null;
}

最受关注的一般是onLocaitonChanged方法,这里可以得到新的位置。


3) 有时候,用户并没有开启对于得到位置的权限,则需要我们确保这个权限已经打开:

Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);

将用户带到设置GPS权限的Activity当中;比如,在使用中,发现用户没有开启GPS和数据流量,则带用户进入打开的界面,代码如下:

/**
* Function to show settings alert dialog
* On pressing Settings button will lauch Settings Options
* */
public void showSettingsAlert(){
AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
     
        // Setting Dialog Title
        alertDialog.setTitle("GPS is settings");
 
        // Setting Dialog Message
        alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");
 
        // On pressing Settings button
        alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int which) {
            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            mContext.startActivity(intent);
            }
        });
 
        // on pressing cancel button
        alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
            }
        });
 
        // Showing Alert Message
        alertDialog.show();
}

4) 一般来说,当前得到的位置信息不一定是最好的(原因不详述),更多的时候,使用的是上一次得到的最好的位置:

location = getLocationManager().getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

原创粉丝点击