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

更多相关文章

  1. Android(安卓)APK 反编译工具
  2. android.util.Log常用的方法
  3. Android(安卓)获取当前语言的方法1
  4. Android的setTag
  5. Android中使用AndroidTestCase的方法实例
  6. Android(安卓)market:// 链接到Google Play 商店
  7. 箭头函数的基础使用
  8. NPM 和webpack 的基础使用
  9. Python list sort方法的具体使用

随机推荐

  1. Android(安卓)Studio支持Java8方法
  2. android全局dialog
  3. Android应用程序之间共享文字和图片(二)
  4. Android之渐变动画
  5. Caused by: java.lang.NoSuchMethodExcep
  6. Android(安卓)监听网络变化然后刷新页面
  7. android 资料文档共享
  8. android 常用代码
  9. android 打开摄像头的代码
  10. Android将bitmap保存到自定义路径