Android GPS

 

要实用Adnroid平台的GPS设备,首先需要添加上权限,所以需要添加如下权限:  

 

  
  
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> < 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也是可以的没有感觉到什么不同,不知道发布的时候怎么样。

以面的做完之后,因为地图使用的是网络,因此必须得有网络的访问权限,这个需要添加上去。否则无法得到地图信息。

下面 就是实现代码:

首先定义地图的控制器:

 

      
      
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> 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定位了,其实你只要把他们两个结合起来就能实现定位了!

使用GPS 定位,首先,需要在清单文件(AndroidManifest.xml)中注册获取定位的权限: **1.获取位置管理器对象LocationManager** ``` import android.location.LocationManager; LocationManager lm; // lm =(LocationManager) this.getSystemService(Context`.LOCATION_SERVICE); // ``` **2.一般使用LocationManager的getLastKnownLocation(LocationManager.GPS_PROVIDER);方法获取Location对象** ``` String provider = LocationManager.GPS_PROVIDER;// 指定LocationManager的定位方法 Location location = locationManager.getLastKnownLocation(provider);// 调用getLastKnownLocation()方法获取当前的位置信息 ``` 不过不建议用这种方法,有几点原因: 一,在很多提供定位服务的应用程序中,不仅需要获取当前的位置信息,还需要监视位置的变化,在位置改变时调用特定的处理方法 ,其中LocationManager提供了一种便捷、高效的位置监视方法requestLocationUpdates(),可以根据位置的距离变化和时间间隔设定,产生位置改变事件的条件,这样可以避免因微小的距离变化而产生大量的位置改变事件 。 二,当你开启GPS,provider的值为GPS。这时的定位方式为GPS,由于GPS定位慢,所以它不可能立即返回你一个Location对象,所以就返回null了。 **3.推荐locationManager.requestLocationUpdates();方法** LocationManager中设定监听位置变化的代码如下: ``` lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10,new MyLocationListener()); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值