Android GPS
来源:互联网 发布:手机电子狗软件哪个好 编辑:程序博客网 时间:2024/05/19 16:35
要实用Adnroid平台的GPS设备,首先需要添加上权限,所以需要添加如下权限:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission首先判断GPS模块是否存在或者是开启:
private void openGPSSettings() {
LocationManager alm = (LocationManager) this
.getSystemService(Context.LOCATION_SERVICE);
if (alm
.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) {
Toast.makeText(this, "GPS模块正常", Toast.LENGTH_SHORT)
.show();
return;
}
Toast.makeText(this, "请开启GPS!", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS);
startActivityForResult(intent,0); //此为设置完成后返回到获取界面
}
如果开启正常,则会直接进入到显示页面,如果开启不正常,则会进行到GPS设置页面:
获取代码如下:
private void getLocation()
{
// 获取位置管理服务
LocationManager locationManager;
String serviceName = Context.LOCATION_SERVICE;
locationManager = (LocationManager) this.getSystemService(serviceName);
// 查找到服务信息
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE); // 高精度
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW); // 低功耗
String provider = locationManager.getBestProvider(criteria, true); // 获取GPS信息
Location location = locationManager.getLastKnownLocation(provider); // 通过GPS获取位置
updateToNewLocation(location);
// 设置监听器,自动更新的最小时间为间隔N秒(1秒为1*1000,这样写主要为了方便)或最小位移变化超过N米
locationManager.requestLocationUpdates(provider, 100 * 1000, 500,
locationListener);}
到这里就可以获取到地理位置信息了,但是还是要显示出来,那么就用下面的方法进行显示:
private void updateToNewLocation(Location location) {
TextView tv1;
tv1 = (TextView) this.findViewById(R.id.tv1);
if (location != null) {
double latitude = location.getLatitude();
double longitude= location.getLongitude();
tv1.setText("维度:" + latitude+ "/n经度" + longitude);
} else {
tv1.setText("无法获取地理信息");
}
}
这样子就能获取到当前使用者所在的地理位置了,至少如何下地图上实现,在下面将进行获取,并显示出来!
要在Android客户端显示Google地图,就要使用google 的API,这次使用的的不是Android 的SDK而是直接使得的Google的,因为没有细究,所以他们之间具体有多少区别,现在还不太清楚,等有时间了,再仔细看一看,显示地图使用的是View为:com.google.android.maps.MapView 但是要使用的话,还得去google 申请一个Map的Key去,具体怎么申请,这里不再细说,不过个人感觉如果是测试的话,随便找一个Key也是可以的没有感觉到什么不同,不知道发布的时候怎么样。
以面的做完之后,因为地图使用的是网络,因此必须得有网络的访问权限,这个需要添加上去。否则无法得到地图信息。
下面 就是实现代码:
首先定义地图的控制器:
private MapController mapController;
private GeoPoint geoPoint; //这个表示的是定位中心点在代码中有实现
然后就可以实现了!
下面是实现代码:
setContentView(R.layout.main);
MapView mapView = (MapView)findViewById(R.id.mapview1);
mapController = mapView.getController();
mapView.setEnabled(true);
mapView.setClickable(true);
mapView.setBuiltInZoomControls(true);
geoPoint = new GeoPoint((int)40.9166666666667*1000000,(int)116.816666666667*1000000);
mapController.animateTo(geoPoint);
mapController.setZoom(12);
如果要在某点显示信息的话,可以通过下面的函数进行设置:
class MyLocationOverlay extends Overlay
{
@Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when)
{
super.draw(canvas, mapView, shadow);
Paint paint = new Paint();
Point myScreenCoords = new Point();
// 将经纬度转换成实际屏幕坐标
mapView.getProjection().toPixels(geoPoint, myScreenCoords);
paint.setStrokeWidth(1);
paint.setARGB(255, 255, 0, 0);
paint.setStyle(Paint.Style.STROKE);
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.home);
canvas.drawBitmap(bmp, myScreenCoords.x, myScreenCoords.y, paint);
canvas.drawText("要显示的名称", myScreenCoords.x, myScreenCoords.y, paint);
return true;
}
}
当然要显示地图,如果我们继承的是Activity的话,是无法进行显示的,所以要改为MapActivity,这也就是为什么要使用google Api的原因了!
好了,这个地图,结合前面讲的GPS信息获取都已经实现了,再下面就可以实现通过GPS定位了,其实你只要把他们两个结合起来就能实现定位了!
转自:http://www.cnblogs.com/fly_binbin/archive/2010/12/16/1908518.html
- Android GPS
- Android GPS
- android GpS
- android gps
- android GPS
- android GPS
- Android GPS
- android GPS
- android GPS
- Android GPS
- Android GPS
- Android Gps
- Android GPS
- android gps
- Android GPS
- Android GPS
- Android GPS (当前位置 & GPS信息更新)
- Android GPS (当前位置 & GPS信息更新)
- 有关薪资的转载
- Android Button应用法则
- 网页中获取客户端的ip(防攻击,提高安全性的第一道屏障)
- C语言系统资源控制(getrlimit && setrlimit)
- 记住今天这一天
- Android GPS
- SWT中如何判断平台?
- 想知道你的网页的访问量吗?用以下代码
- js写的二级级联选项框
- Unix/Linux下 getrusage 的用法
- 小白成长记
- Template 第一个 还不知道怎么调用
- 解除SWT不允许继承的限制
- java连接DB2数据库以及jsp中文问题