【代码】Android 客户端GPS定位源码
2013-09-13 13:45
591 查看
AndroidManifest.xml文件配置
Main.java文件
注1:如果你的手机上没有gps定位系统,会有以下错误
08-10 13:04:55.610:ERROR/AndroidRuntime(492): java.lang.RuntimeException: Unable to start activityComponentInfo{cn.itcast.main/cn.itcast.main.Main}: java.lang.IllegalArgumentException:provider==null
注2:记得去google官网
http://code.google.com/intl/zh-CN/android/maps-api-signup.html
申请自己机器的apikey
C:\Documents and Settings\Administrator\.android>keytool -list-keystore debug.kEystore
默认密码android
我机器的apikey,你用不好使
最终实现效果:随着android客户端得移动,在地图上提供精确的当前所在位置
以上是本文关于Android 客户端GPS定位的源码,希望本文对广大安卓开发者有所帮助,感谢阅读本文。更多安卓技术问题欢迎加群探讨:278744577,验证码:csl,不写验证不予通过哟~
<?xml version="1.0"encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="cn.itcast.main" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon"android:label="@string/app_name"> <activity android:name=".Main" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <uses-library android:name="com.google.android.maps"/> </application> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_FIND_LOCATION"/> </manifest>
Main.java文件
package cn.itcast.main; import java.util.List; import com.google.android.maps.GeoPoint; import com.google.android.maps.MapActivity; import com.google.android.maps.MapController; import com.google.android.maps.MapView; import com.google.android.maps.Overlay; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Point; import android.location.Address; import android.location.Criteria; import android.location.Geocoder; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.view.MotionEvent; import android.widget.TextView; public class Main extends MapActivity{ private MapController mapController; private GeoPoint geoPoint; private String msg; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //获得mapview MapView mapView=(MapView) this.findViewById(R.id.mapview); //地图的显示格式为交通图 mapView.setTraffic(true); //设置可控 mapView.setClickable(true); mapView.setEnabled(true); mapView.setBuiltInZoomControls(true); //得到gps设备的访问 LocationManager locationManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE); //设置gps定位配置 Criteria criteria=new Criteria(); //设置显示精度 criteria.setAccuracy(Criteria.ACCURACY_COARSE); //是否获得海拔数据 criteria.setAltitudeRequired(false); //是否获得方向数据 criteria.setBearingRequired(false); //是否允许运营商计费 criteria.setCostAllowed(true); //设置耗电程度 criteria.setPowerRequirement(Criteria.POWER_LOW); //获得服务供应商 Stringprovider=locationManager.getBestProvider(criteria, true); //获取上一个定位点 Locationlocation=locationManager.getLastKnownLocation(provider); //获得gps定位坐标信息 Double latitude=location.getLatitude()*1E6; Double longitude=location.getLongitude()*1E6; //获得卫星定位点 geoPoint=new GeoPoint(latitude.intValue(),longitude.intValue()); //获得地图控制器 mapController=mapView.getController(); //设置地图显示初始化精度 mapController.setZoom(12); mapController.animateTo(geoPoint); //实例化自定义绘图层 MyOverlay myOverlay=new MyOverlay(); //为mapview添加绘图层 mapView.getOverlays().add(myOverlay); //定义一个final,TextView,以备子类引用 final TextView textView=(TextView) findViewById(R.id.textview); LocationListener locationListener=new LocationListener(){ @Override public void onStatusChanged(String provider, int status, Bundle extras) { } @Override public void onProviderEnabled(String provider) { // TODO Auto-generatedmethod stub } @Override public void onProviderDisabled(String provider) { // TODO Auto-generatedmethod stub } @Override public void onLocationChanged(Location location) { Double latitude=location.getLatitude()*1E6; Double longitude=location.getLongitude()*1E6; try { //获得精度纬度字符串 msg = "经度:" + location.getLongitude() + "\n"; msg += "纬度:" + location.getLatitude() + "\n"; //根据经纬度获得改点地址信息 Geocoder gc=new Geocoder(Main.this); List<Address> addresses=gc.getFromLocation(latitude, longitude,1); if (addresses.size()>0) { //获得地址信息 msg+="AddressLine:"+addresses.get(0).getAddressLine(0)+"\n"; //获得国家名 msg += "CountryName:" + addresses.get(0).getCountryName()+"\n"; msg += "Locality:" + addresses.get(0).getLocality() + "\n"; msg += "FeatureName:" + addresses.get(0).getFeatureName(); } textView.setText(msg); } catch (Exception e) { e.printStackTrace(); } } }; //注册位置监听器,1秒钟扫描1次 locationManager.requestLocationUpdates(provider, 1000, 0, locationListener); } class MyOverlay extends Overlay{ //保证触控事件不重复操作 private int count=0; @Override public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) { //定义画笔 Paint paint=new Paint(); paint.setColor(Color.RED); //定义屏幕点 Point screenPoint=new Point(); //gps点转屏幕点 mapView.getProjection().toPixels(geoPoint, screenPoint); //获得gps标志点图片 Bitmap bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.flag); //绘制gps点图片 canvas.drawBitmap(bitmap,screenPoint.x,screenPoint.y, paint); //绘制文字说明 canvas.drawText("当前位置", screenPoint.x, screenPoint.y, paint); return super.draw(canvas, mapView, shadow, when); } @Override public boolean onTouchEvent(MotionEvent e, MapView mapView) { //定义一个屏幕点 Point screenPoint=new Point(); //把gps点变成屏幕点 mapView.getProjection().toPixels(geoPoint, screenPoint); //获得触点坐标 int currentX=(int) e.getX(); int currentY=(int) e.getY(); //在50,30范围内触碰,显示当前经纬度 if ((currentX-screenPoint.x)>=0&&(currentX-screenPoint.x)<50 &&(currentY-screenPoint.y>=0)&&(currentY-screenPoint.y)<30) { if (count==0) { new AlertDialog.Builder(Main.this).setMessage(msg) .setPositiveButton("确定",new OnClickListener(){ @Override public void onClick(DialogInterface dialog, int which) { count=0; } }).show(); } count++; } return super.onTouchEvent(e, mapView); } } @Override protected boolean isRouteDisplayed() { // TODO Auto-generatedmethod stub return false; } }
注1:如果你的手机上没有gps定位系统,会有以下错误
08-10 13:04:55.610:ERROR/AndroidRuntime(492): java.lang.RuntimeException: Unable to start activityComponentInfo{cn.itcast.main/cn.itcast.main.Main}: java.lang.IllegalArgumentException:provider==null
注2:记得去google官网
http://code.google.com/intl/zh-CN/android/maps-api-signup.html
申请自己机器的apikey
C:\Documents and Settings\Administrator\.android>keytool -list-keystore debug.kEystore
默认密码android
我机器的apikey,你用不好使
<com.google.android.maps.MapView android:layout_width="fill_parent" android:layout_height="fill_parent" android:apiKey="0EITsWoXq7NC_mgKW6yIxIViNLhMGsIW4dbmlsg />
最终实现效果:随着android客户端得移动,在地图上提供精确的当前所在位置
以上是本文关于Android 客户端GPS定位的源码,希望本文对广大安卓开发者有所帮助,感谢阅读本文。更多安卓技术问题欢迎加群探讨:278744577,验证码:csl,不写验证不予通过哟~
相关文章推荐
- Android客户端GPS定位源码
- Android客户端GPS定位源码
- Android GPS定位详解及实例代码
- android GPS定位和卫星个数(源码)
- 最新基于高德地图的android进阶开发(4)GPS定位之POI(Point of interesting)兴趣点源码简介
- android GPS定位代码
- android定位之GPS,WIFI和GPRS(NETWORK)方式的定位(含实现源码)
- Android客户端GPS定位
- Android移动开发-通过自定义算法代码来纠偏地图由GPS定位到的经纬度在地图上显示或解析位置时出现偏移的问题
- Android实现GPS定位代码实例
- [代码分享] 乐淘Android客户端源码
- Android 2.1 GPS定位和拍照功能代码
- android GPS定位和卫星个数(源码)
- android使用GPS定位及在googlemap添加标记
- android客户端和java服务端之间用socket来传输图片(示例代码)
- Android入门之GPS定位详解
- Android 热修复补丁管理,后端代码+客户端 sdk
- Android全球定位系统GPS邻近警报-刘宇
- android项目源码解析04:新浪微博客户端源码解析
- 和菜鸟一起学android4.0.3源码之硬件gps简单移植