Android客户端GPS定位源码_第1页
Android客户端GPS定位源码_第2页
Android客户端GPS定位源码_第3页
Android客户端GPS定位源码_第4页
Android客户端GPS定位源码_第5页
已阅读5页,还剩1页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、AndroidManifest.xml文件配置 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

2、.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.Pain

3、t;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

4、;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 map

5、View=(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定位配

6、置 Criteria criteria=new Criteria(); /设置显示精度 criteria.setAccuracy(Criteria.ACCURACY_COARSE); /是否获得海拔数据 criteria.setAltitudeRequired(false); /是否获得方向数据 criteria.setBearingRequired(false); /是否允许运营商计费 criteria.setCostAllowed(true); /设置耗电程度 criteria.setPowerRequirement(Criteria.POWER_LOW); /获得服务供应商 String

7、 provider=locationManager.getBestProvider(criteria, true); /获取上一个定位点 Location location=locationManager.getLastKnownLocation(provider); /获得gps定位坐标信息 Double latitude=location.getLatitude()*1E6; Double longitude=location.getLongitude()*1E6; /获得卫星定位点 geoPoint=new GeoPoint(Value(),longitude.i

8、ntValue(); /获得地图控制器 mapController=mapView.getController(); /设置地图显示初始化精度 mapController.setZoom(12); mapController.animateTo(geoPoint); /实例化自定义绘图层 MyOverlay myOverlay=new MyOverlay(); /为mapview添加绘图层 mapView.getOverlays().add(myOverlay); /定义一个final,TextView,以备子类引用 final TextView textView=(TextView) fin

9、dViewById(R.id.textview); LocationListener locationListener=new LocationListener() Overridepublic void onStatusChanged(String provider, int status, Bundle extras) Overridepublic void onProviderEnabled(String provider) / TODO Auto-generated method stubOverridepublic void onProviderDisabled(String pro

10、vider) / TODO Auto-generated method stubOverridepublic void onLocationChanged(Location location) Double latitude=location.getLatitude()*1E6;Double longitude=location.getLongitude()*1E6;try /获得精度纬度字符串msg = 经度: + location.getLongitude() + n;msg += 纬度: + location.getLatitude() + n;/根据经纬度获得改点地址信息Geocode

11、r gc=new Geocoder(Main.this);List 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 += Fea

12、tureName: + 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;Overridepublic boolean draw(Can

13、vas 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);/绘

14、制gps点图片canvas.drawBitmap(bitmap, screenPoint.x,screenPoint.y, paint);/绘制文字说明canvas.drawText(当前位置, screenPoint.x, screenPoint.y, paint);return super.draw(canvas, mapView, shadow, when);Overridepublic 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();/

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论