Android GPS 使用 备忘

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

修复三星9100,、7100、9500......乃至sony等等智能手机的gps定位慢搜星慢的秒定gps修复, 方法来自互联网,亲自体验效果非常好:root后用re文件管理器替换自己手机里面的内容。位置在:根目录/system/etc下面。权限和原来相同。如果想自己修改可参照下面内容:打开RE管理器,在system下找到etc文件夹,再找到gps.conf(gps配置文件),将RE管理器挂载为读写,点击(长按)gps.conf文件,选择以文本编辑器方式打开,将里面内容修改为以下内容即可: NTP_SERVER=0.cn.pool.ntp.org NTP_SERVER=1.cn.pool.ntp.org NTP_SERVER=2.cn.pool.ntp.org NTP_SERVER=3.cn.pool.ntp.org NTP_SERVER=cn.pool.ntp.org XTRA_SERVER_1=http://xtra2.gpsonextra.net/xtra.bin XTRA_SERVER_2=http://xtra1.gpsonextra.net/xtra.bin XTRA_SERVER_3=http://xtra3.gpsonextra.net/xtra.bin SUPL_HOST=suplcn.sirf.com SUPL_HOST=221.176.0.55 SUPL_PORT=7275 更改完后,点击手机菜单键,保存并退出,重启手机,打开GPS,启动导航软件,10秒内定位!!! 真正有技术含量的内容 解释: NTP_SERVER=0.cn.pool.ntp.org(为中国0号授时子服务器,实时变动) NTP_SERVER=3.cn.pool.ntp.org(为中国3号授时子服务器,实时变动) NTP_SERVER=1.asia.pool.ntp.org(为亚洲1号授时子服务器,实时变动) NTP_SERVER=2.asia.pool.ntp.org(为亚洲2号授时子服务器,实时变动) SUPL_HOST=221.176.0.55(中国移动定位服务器) SUPL_HOST=suplcn.sirf.com(上海电信定位服务器)也可改为:supl.google.com(谷歌定位服务器)或supl.nokia.com(诺基亚定位服务器) SUPL_PORT=7275 (服务器端口;也可用7276) 每天中国对时服务器变动信息可查看网址:http://www.pool.ntp.org/zone/cn(如追求极致,可根据实际变动调整) 1、授时服务器,也就是NTP Server,对于搜星后快速定位很重要。有的时候搜到的卫星很多但是定位很慢,就是因为和授时服务器连接不顺畅,快速和NTP Server同步,有助于提高GPS定位速度。建议:优先使用国内的NTP Server。 NTP全称是Network Time Protocol,是用来让计算机之间实现时间同步的协议,而发布这种校对时间的服务器,就是NTP Server。我们的操作系统XP、win7上都会用这个东西对时间。 2、supl服务器,没有统一的译名,似乎也没有统一的标准,主要是通过网络提供GPS卫星星历图,提高搜星速度;另外通过信号基站等其他辅助手段提高定位精度。建议:要提高速度,依然是尽量使用国内服务器 3、以下部分为服务器地址,身在国内的用户优先考虑的 国内的NTP Server: 0.cn.pool.ntp.org 1.cn.pool.ntp.org 2.cn.pool.ntp.org 3.cn.pool.ntp.org cn.pool.ntp.org 这是一组服务器集群,服务器位于上海电信和浙江台州电信,经实践证明这些网址是等效的。任意一个网址都可能对应这两地中的一个,台州电信和上海电信这两组服务器的ping值都在30~50ms左右(有线宽带和GSM网络均测试了)。 ntp.api.bz 这个是一组NTP服务器集群,目前有6台服务器,位于上海电信,速度很快 210.72.145.44 中国国家授时中心服务器IP,在西安,狗日的禁ping,但是实际上速度飞快 AGPS supl服务器地址: suplcn.sirf.com 114.80.208.5:7275 上海电信 221.176.0.55:7275 北京移动机房,中国移动的SUPL服务器,没查到域名,速度很快,建议移动用户选择 sls1.sirf.com 66
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值