当前位置: 首页 > news >正文

Android下实现GPS定位服务

1.申请Google API Key,参考前面文章

2.实现GPS的功能需要使用模拟器进行经纬度的模拟设置,请参考前一篇文章进行设置

3.创建一个Build Target为Google APIs的项目

4.修改Androidmanifest文件:

<uses-library android:name="com.google.android.maps" /> <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

5.修改main.xml文件

<com.google.android.maps.MapView android:id="@+id/MapView01" android:layout_width="fill_parent" android:layout_height="fill_parent" android:apiKey="0f8FBFJliR7j_7aNwDxClBv6VW8O12V2Y21W_CQ"/>

注意:这里的apiKey值请相应修改为自己的key值

6.代码清单:

package com.hoo.android.LocationMap; import java.io.IOException; import java.util.List; import java.util.Locale; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; 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.widget.TextView; 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; public class ActivityLocationMap extends MapActivity { public MapController mapController; public MyLocationOverlay myPosition; public MapView myMapView; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //取得LocationManager实例 LocationManager locationManager; String context=Context.LOCATION_SERVICE; locationManager=(LocationManager)getSystemService(context); myMapView=(MapView)findViewById(R.id.MapView01); //取得MapController实例,控制地图 mapController=myMapView.getController(); //设置显示模式为街景模式 myMapView.setStreetView(true); //*************使用系统自带的控件放大缩小视图*************************** //取得MapController对象(控制MapView) mapController = myMapView.getController(); //设置地图支持设置模式 myMapView.setEnabled(true); //设置地图支持点击 myMapView.setClickable(true); //设置缩放控制,这里我们自己实现缩放菜单 myMapView.displayZoomControls(true); myMapView.setBuiltInZoomControls(true); //******************************************************************* 设置设置地图目前缩放大小倍数(从1到21) mapController.setZoom(17); //设置使用MyLocationOverlay来绘图 myPosition=new MyLocationOverlay(); List<Overlay> overlays=myMapView.getOverlays(); overlays.add(myPosition); //设置Criteria(标准服务商)的信息 Criteria criteria =new Criteria(); //*****设置服务商提供的精度要求,以供筛选提供商************************ criteria.setAccuracy(Criteria.POWER_HIGH);//表明所要求的经纬度的精度 criteria.setAltitudeRequired(false); //高度信息是否需要提供 criteria.setBearingRequired(false); //压力(气压?)信息是否需要提供 criteria.setCostAllowed(false); //是否会产生费用 criteria.setPowerRequirement(Criteria.POWER_MEDIUM);//最大需求标准 //***************************************************** //取得效果最好的criteria String provider=locationManager.getBestProvider(criteria, true); //得到坐标相关的信息 Location location=locationManager.getLastKnownLocation(provider); //更新位置信息 updateWithNewLocation(location); //注册一个周期性的更新,3000ms更新一次,0代表最短距离 //locationListener用来监听定位信息的改变(OnLocationChanged) locationManager.requestLocationUpdates(provider, 3000, 0,locationListener); } //更新位置信息 private void updateWithNewLocation(Location location) { String latLongString; //声明经纬度的字符串 TextView myLocationText = (TextView)findViewById(R.id.TextView01); //初始化地址为没有找到,便于处理特殊情况 String addressString="没有找到地址/n"; if(location!=null) { //****************获取当前的经纬度,并定位到目标************************* //为绘制标志的类设置坐标 myPosition.setLocation(location); //取得经度和纬度 Double geoLat=location.getLatitude()*1E6; Double geoLng=location.getLongitude()*1E6; //将其转换为int型 GeoPoint point=new GeoPoint(geoLat.intValue(),geoLng.intValue()); //定位到指定坐标 mapController.animateTo(point); //********************************************************************* double lat=location.getLatitude(); //获得经纬度 double lng=location.getLongitude(); latLongString="经度:"+lat+"/n纬度:"+lng; //设置经纬度字符串 // double latitude=location.getLatitude(); //double longitude=location.getLongitude(); //根据地理位置来确定编码 Geocoder gc=new Geocoder(this,Locale.getDefault()); try { //取得地址相关的一些信息:经度、纬度 List<Address> addresses=gc.getFromLocation(lat, lng,1); StringBuilder sb=new StringBuilder(); if(addresses.size()>0) { Address address=addresses.get(0); for(int i=0;i<address.getMaxAddressLineIndex()-1;i++) sb.append(address.getAddressLine(i)).append(","); //获得地址sb.append(address.getLocality()).append("/n"); //获得邮编sb.append(address.getPostalCode()).append("/n"); sb.append(address.getCountryName()); addressString=sb.toString(); } }catch(IOException e){} } else { latLongString="没有找到坐标./n"; } //显示 myLocationText.setText("您当前的位置如下:/n"+latLongString+"/n"+addressString); } //监听位置信息的改变 private final LocationListener locationListener=new LocationListener() { //当坐标改变时触发此函数 public void onLocationChanged(Location location) { updateWithNewLocation(location); } //Provider被disable时触发此函数,比如GPS被关闭 public void onProviderDisabled(String provider) { updateWithNewLocation(null); } //Provider被enable时触发此函数,比如GPS被打开 public void onProviderEnabled(String provider){} //Provider的转态在可用、暂时不可用和无服务三个状态直接切换时触发此函数 public void onStatusChanged(String provider,int status,Bundle extras){} }; //方法默认是true,服务器所知的状态列信息是否需要显示 protected boolean isRouteDisplayed() { return false; } class MyLocationOverlay extends Overlay { Location mLocation; //在更新坐标,以便画图 public void setLocation(Location location) { mLocation = location; } @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(); // 将经纬度转换成实际屏幕坐标 GeoPoint tmpGeoPoint = new GeoPoint((int)(mLocation.getLatitude()*1E6),(int)(mLocation.getLongitude()*1E6)); mapView.getProjection().toPixels(tmpGeoPoint, myScreenCoords); //*********paint相关属性设置********* paint.setStrokeWidth(0);//文 paint.setARGB(255, 255, 0, 0); paint.setStyle(Paint.Style.STROKE); //*********************************** Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.green_dot); canvas.drawBitmap(bmp, myScreenCoords.x, myScreenCoords.y, paint); canvas.drawText("您目前的位置", myScreenCoords.x, myScreenCoords.y, paint); return true; } } }

代码参考网络,加以修改优化,谢谢

7.程序运行截图,前提是在命令行下输入geo fix 121.5 31.24(定位到上海东方明珠),在命令行下可以输入其他坐标,系统会根据坐标显示其他位置,如接着输入geo fix 113.325 23.113(定位到广州海心沙),不知为什么输入坐标的时候经常会不识别,有时能够成功而有时不行,郁闷,求解……

相关文章:

  • 数字转化为汉字,如5-五
  • 用Thread做点自动化的事
  • 201521123042 《Java程序设计》 第10周学习总结
  • Mysql全文索引
  • 如何不用组件实现Ajax效果
  • SQL Server中关于基数估计如何计算预估行数的一些探讨
  • NodeJS安装第一个工程.
  • 丢失了'LINQ to SQL类’模板怎么办?
  • Linq O/R设计器的使用
  • 【linux】之内核升级
  • Tomcat端口被占用解决方法
  • SQL Server 不能创建数据库了,发生错误:1807 未能获得数据库 'model' 上的排它锁。请稍后重试操作。...
  • Linux中如何配置sudo用户
  • Drop it(去除元素)
  • 杂记
  • [case10]使用RSQL实现端到端的动态查询
  • [译] 怎样写一个基础的编译器
  • const let
  • ECMAScript6(0):ES6简明参考手册
  • es6要点
  • HTTP传输编码增加了传输量,只为解决这一个问题 | 实用 HTTP
  • Magento 1.x 中文订单打印乱码
  • STAR法则
  • Vim Clutch | 面向脚踏板编程……
  • 从零到一:用Phaser.js写意地开发小游戏(Chapter 3 - 加载游戏资源)
  • 更好理解的面向对象的Javascript 1 —— 动态类型和多态
  • 关于 Linux 进程的 UID、EUID、GID 和 EGID
  • 计算机常识 - 收藏集 - 掘金
  • 爬虫进阶 -- 神级程序员:让你的爬虫就像人类的用户行为!
  • 如何优雅的使用vue+Dcloud(Hbuild)开发混合app
  • 深入浅出Node.js
  • 十年未变!安全,谁之责?(下)
  • 算法---两个栈实现一个队列
  • 通过git安装npm私有模块
  • 如何正确理解,内页权重高于首页?
  • ​学习一下,什么是预包装食品?​
  • #《AI中文版》V3 第 1 章 概述
  • #pragma 指令
  • (2009.11版)《网络管理员考试 考前冲刺预测卷及考点解析》复习重点
  • (2022版)一套教程搞定k8s安装到实战 | RBAC
  • (39)STM32——FLASH闪存
  • (Demo分享)利用原生JavaScript-随机数-实现做一个烟花案例
  • (Java岗)秋招打卡!一本学历拿下美团、阿里、快手、米哈游offer
  • (MIT博士)林达华老师-概率模型与计算机视觉”
  • (ZT)北大教授朱青生给学生的一封信:大学,更是一个科学的保证
  • (二)Eureka服务搭建,服务注册,服务发现
  • (附源码)ssm高校升本考试管理系统 毕业设计 201631
  • (附源码)计算机毕业设计ssm高校《大学语文》课程作业在线管理系统
  • (免费领源码)Java#ssm#MySQL 创意商城03663-计算机毕业设计项目选题推荐
  • (十八)三元表达式和列表解析
  • (五)网络优化与超参数选择--九五小庞
  • (学习日记)2024.01.19
  • (转)Linux NTP配置详解 (Network Time Protocol)
  • .Net IOC框架入门之一 Unity
  • .Net Memory Profiler的使用举例