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

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

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

4.修改Androidmanifest文件:

view plain
  1. <uses-library android:name="com.google.android.maps" />
  2. <uses-permission android:name="android.permission.INTERNET"/>
  3. <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
  4. <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

5.修改main.xml文件

view plain
  1. <com.google.android.maps.MapView
  2. android:id="@+id/MapView01"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:apiKey="0f8FBFJliR7j_7aNwDxClBv6VW8O12V2Y21W_CQ"/>

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

6.代码清单:

view plain
  1. package com.hoo.android.LocationMap;
  2. import java.io.IOException;
  3. import java.util.List;
  4. import java.util.Locale;
  5. import android.content.Context;
  6. import android.graphics.Bitmap;
  7. import android.graphics.BitmapFactory;
  8. import android.graphics.Canvas;
  9. import android.graphics.Paint;
  10. import android.graphics.Point;
  11. import android.location.Address;
  12. import android.location.Criteria;
  13. import android.location.Geocoder;
  14. import android.location.Location;
  15. import android.location.LocationListener;
  16. import android.location.LocationManager;
  17. import android.os.Bundle;
  18. import android.widget.TextView;
  19. import com.google.android.maps.GeoPoint;
  20. import com.google.android.maps.MapActivity;
  21. import com.google.android.maps.MapController;
  22. import com.google.android.maps.MapView;
  23. import com.google.android.maps.Overlay;
  24. public class ActivityLocationMap extends MapActivity
  25. {
  26. public MapController mapController;
  27. public MyLocationOverlay myPosition;
  28. public MapView myMapView;
  29. public void onCreate(Bundle savedInstanceState) {
  30. super.onCreate(savedInstanceState);
  31. setContentView(R.layout.main);
  32. //取得LocationManager实例
  33. LocationManager locationManager;
  34. String context=Context.LOCATION_SERVICE;
  35. locationManager=(LocationManager)getSystemService(context);
  36. myMapView=(MapView)findViewById(R.id.MapView01);
  37. //取得MapController实例,控制地图
  38. mapController=myMapView.getController();
  39. //设置显示模式为街景模式
  40. myMapView.setStreetView(true);
  41. //*************使用系统自带的控件放大缩小视图***************************
  42. //取得MapController对象(控制MapView)
  43. mapController = myMapView.getController();
  44. //设置地图支持设置模式
  45. myMapView.setEnabled(true);
  46. //设置地图支持点击
  47. myMapView.setClickable(true);
  48. //设置缩放控制,这里我们自己实现缩放菜单
  49. myMapView.displayZoomControls(true);
  50. myMapView.setBuiltInZoomControls(true);
  51. //*******************************************************************
  52. 设置设置地图目前缩放大小倍数(从1到21)
  53. mapController.setZoom(17);
  54. //设置使用MyLocationOverlay来绘图
  55. myPosition=new MyLocationOverlay();
  56. List<Overlay> overlays=myMapView.getOverlays();
  57. overlays.add(myPosition);
  58. //设置Criteria(标准服务商)的信息
  59. Criteria criteria =new Criteria();
  60. //*****设置服务商提供的精度要求,以供筛选提供商************************
  61. criteria.setAccuracy(Criteria.POWER_HIGH);//表明所要求的经纬度的精度
  62. criteria.setAltitudeRequired(false); //高度信息是否需要提供
  63. criteria.setBearingRequired(false);  //压力(气压?)信息是否需要提供
  64. criteria.setCostAllowed(false);  //是否会产生费用
  65. criteria.setPowerRequirement(Criteria.POWER_MEDIUM);//最大需求标准
  66. //*****************************************************
  67. //取得效果最好的criteria
  68. String provider=locationManager.getBestProvider(criteria, true);
  69. //得到坐标相关的信息
  70. Location location=locationManager.getLastKnownLocation(provider);
  71. //更新位置信息
  72. updateWithNewLocation(location);
  73. //注册一个周期性的更新,3000ms更新一次,0代表最短距离
  74. //locationListener用来监听定位信息的改变(OnLocationChanged)
  75. locationManager.requestLocationUpdates(provider, 3000, 0,locationListener);
  76. }
  77. //更新位置信息
  78. private void updateWithNewLocation(Location location)
  79. {
  80. String latLongString; //声明经纬度的字符串
  81. TextView myLocationText = (TextView)findViewById(R.id.TextView01);
  82. //初始化地址为没有找到,便于处理特殊情况
  83. String addressString="没有找到地址/n";
  84. if(location!=null)
  85. {
  86. //****************获取当前的经纬度,并定位到目标*************************
  87. //为绘制标志的类设置坐标
  88. myPosition.setLocation(location);
  89. //取得经度和纬度
  90. Double geoLat=location.getLatitude()*1E6;
  91. Double geoLng=location.getLongitude()*1E6;
  92. //将其转换为int型
  93. GeoPoint point=new GeoPoint(geoLat.intValue(),geoLng.intValue());
  94. //定位到指定坐标
  95. mapController.animateTo(point);
  96. //*********************************************************************
  97. double lat=location.getLatitude();  //获得经纬度
  98. double lng=location.getLongitude();
  99. latLongString="经度:"+lat+"/n纬度:"+lng;   //设置经纬度字符串
  100. // double latitude=location.getLatitude();
  101. //double longitude=location.getLongitude();
  102. //根据地理位置来确定编码
  103. Geocoder gc=new Geocoder(this,Locale.getDefault());
  104. try
  105. {
  106. //取得地址相关的一些信息:经度、纬度
  107. List<Address> addresses=gc.getFromLocation(lat, lng,1);
  108. StringBuilder sb=new StringBuilder();
  109. if(addresses.size()>0)
  110. {
  111. Address address=addresses.get(0);
  112. for(int i=0;i<address.getMaxAddressLineIndex()-1;i++)
  113. sb.append(address.getAddressLine(i)).append(",");
  114. //获得地址sb.append(address.getLocality()).append("/n");
  115. //获得邮编sb.append(address.getPostalCode()).append("/n");
  116. sb.append(address.getCountryName());
  117. addressString=sb.toString();
  118. }
  119. }catch(IOException e){}
  120. }
  121. else
  122. {
  123. latLongString="没有找到坐标./n";
  124. }
  125. //显示
  126. myLocationText.setText("您当前的位置如下:/n"+latLongString+"/n"+addressString);
  127. }
  128. //监听位置信息的改变
  129. private final LocationListener locationListener=new LocationListener()
  130. {
  131. //当坐标改变时触发此函数
  132. public void onLocationChanged(Location location)
  133. {
  134. updateWithNewLocation(location);
  135. }
  136. //Provider被disable时触发此函数,比如GPS被关闭
  137. public void onProviderDisabled(String provider)
  138. {
  139. updateWithNewLocation(null);
  140. }
  141. //Provider被enable时触发此函数,比如GPS被打开
  142. public void onProviderEnabled(String provider){}
  143. //Provider的转态在可用、暂时不可用和无服务三个状态直接切换时触发此函数
  144. public void onStatusChanged(String provider,int status,Bundle extras){}
  145. };
  146. //方法默认是true,服务器所知的状态列信息是否需要显示
  147. protected boolean isRouteDisplayed()
  148. {
  149. return false;
  150. }
  151. class MyLocationOverlay extends Overlay
  152. {
  153. Location mLocation;
  154. //在更新坐标,以便画图
  155. public void setLocation(Location location)
  156. {
  157. mLocation = location;
  158. }
  159. @Override
  160. public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when)
  161. {
  162. super.draw(canvas, mapView, shadow);
  163. Paint paint = new Paint();
  164. Point myScreenCoords = new Point();
  165. // 将经纬度转换成实际屏幕坐标
  166. GeoPoint tmpGeoPoint = new GeoPoint((int)(mLocation.getLatitude()*1E6),(int)(mLocation.getLongitude()*1E6));
  167. mapView.getProjection().toPixels(tmpGeoPoint, myScreenCoords);
  168. //*********paint相关属性设置*********
  169. paint.setStrokeWidth(0);//文
  170. paint.setARGB(255, 255, 0, 0);
  171. paint.setStyle(Paint.Style.STROKE);
  172. //***********************************
  173. Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.green_dot);
  174. canvas.drawBitmap(bmp, myScreenCoords.x, myScreenCoords.y, paint);
  175. canvas.drawText("您目前的位置", myScreenCoords.x, myScreenCoords.y, paint);
  176. return true;
  177. }
  178. }
  179. }

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

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

转载于:https://www.cnblogs.com/Free-Thinker/p/3606475.html

Android下实现GPS定位服务相关推荐

  1. 超 60 万 GPS 定位服务被曝漏洞,用户信息或将暴露!

    作者 | 刘静 出品 | CSDN(ID:CSDNnews) GPS系统定位所暴露的一系列问题 近日,各大新闻媒体曝出了令百姓们大跌眼镜的爆炸性新闻:超60万GPS定位服务被曝漏洞,用户信息面临暴露风 ...

  2. 华为P9怎么修改AGPS服务器,如何提高华为P9的GPS定位服务 提高手机GPS定位服务教程...

    2016-11-09 17:06:09 如何提高华为P9的GPS定位服务 提高手机GPS定位服务教程 GPS全球定位系统,大家因该都知道手机都有这个功能,rom基地小编今天就来跟大家讲解一下如何提高G ...

  3. android 自带gps定位Location相关知识

    android自带gps定位功能相信大家都不会太陌生了,都有所涉及.简单的写了一个示例程序,获取经纬度还有其它相关数据的代码,还有其他相关的知识,比如直接跳转到打开系统gps设置的界面.还有一个bug ...

  4. android 强制打开gps定位_Android 6.0动态权限及跳转GPS设置界面的方法

    1.动态权限申请 模糊的位置信息android.permission.ACCESS_COARSE_LOCATION权限为例 在AndroidManifest文件中加入权限 然后java代码中动态申请 ...

  5. android 强制打开gps定位_Android判断GPS是否开启和强制帮用户打开GPS

    引子:在我们的应用为用户提供定位服务时,通常想为用户提供精确点的定位服务,这是需要用户配合的.我们必须先检测用户手机的GPS当前是否打开,若没打开则弹出对话框提示.用户若不配合我们也没办法,只能采用基 ...

  6. android gps 差分定位,基于Android的高精度GPS定位与土地测量应用设计

    摘要: 随着移动平台技术的飞速发展,Android作为一款新兴的嵌入式操作系统,基于Android系统的移动终端应用设计成为当今应用开发的一个主流.而GPS(Global Positioning Sy ...

  7. uni-app调手机系统的GPS定位服务

    做个记录,在做签到功能时需要用到,强制用户开启GPS定位.目前在vue页面的安卓上面是没有问题的,在wexx上面的页面还不行,也就是nuve. handleNotice(e){let system = ...

  8. android中实现GPS定位功能,Android中实现GPS定位的简单例子

    今天弄了一个多小时,写了一个GPS获取地理位置代码的小例子,包括参考了网上的一些代码,并且对代码进行了一些修改,希望对大家的帮助.具体代码如下:  要实用Adnroid平台的GPS设备,首先需要添加上 ...

  9. android 强制打开gps定位_Android 6.0 默认关闭定位和GPS,开启后默认选省电

    默认关闭定位和GPS 修改位置 frameworks/base/packages/SettingsProvider/res/values/defaults.xml network,gps 修改为 默认 ...

最新文章

  1. Table doesn't have a primary key
  2. poj1002(map的使用)
  3. 数仓dw怎么建_网易严选如何打造数仓规范和评价体系
  4. layui鼠标放上图片局部放大_花卉图片后期这5招,不美都不行!
  5. JVM笔记详解之垃圾回收器
  6. 实现Linux select IO复用C/S服务器代码
  7. linux 查看emmc分区表_如何查看闪存类型?UFS与F2FS简易科普
  8. day05 数据类型
  9. 程序员35岁辞职后都做了什么工作三位过来人透露了实情,引热议
  10. 力扣--260只出现一次的数字I-III
  11. TC SRM683 Div1 250
  12. [BootStrap学习随笔] 起步、布局容器和栅格式布局
  13. 【新手必看】C语言开发环境,请查收!
  14. 马尔科夫matlab程序,马尔科夫链matlab代码
  15. mysql 1114_mysql返回#1114 - The table 'xxxx'is full解决方法_mysql_爱周末
  16. WebDAV之葫芦儿·派盘 + CloudBeats
  17. Word文档TXT文档chm手册背景色设为护眼色
  18. windows NTFS文件系统手动数据恢复
  19. marquee图片无缝拼接滚动
  20. Spring Boot项目的打包和解压

热门文章

  1. 仿微信朋友圈项目梳理
  2. JavaScript从入门到精通之入门篇(二)函数和数组
  3. Java List 分页
  4. 2017-10-03 前端日报
  5. Chrome指令/追踪Http请求相关
  6. Flink – JobManager.submitJob
  7. hdoj3351-stack
  8. mysql开启慢查询方法(转)
  9. C程序设计语言--第五章:指针与数组
  10. linux驱动中地址空间转换