Android下實現GPS定位服務

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. ??????
  30. ????public?void?onCreate(Bundle?savedInstanceState)?{??
  31. ????????super.onCreate(savedInstanceState);??
  32. ????????setContentView(R.layout.main);??
  33. ????????//取得LocationManager實例??
  34. ????????LocationManager?locationManager;??
  35. ????????String?context=Context.LOCATION_SERVICE;??
  36. ????????locationManager=(LocationManager)getSystemService(context);??
  37. ????????myMapView=(MapView)findViewById(R.id.MapView01);??
  38. ????????//取得MapController實例,控制地圖??
  39. ????????mapController=myMapView.getController();??
  40. ????????//設置顯示模式為街景模式??
  41. ????????myMapView.setStreetView(true);??
  42. ??????????
  43. ????????//*************使用系統自帶的控件放大縮小視圖***************************??
  44. ????????//取得MapController對象(控制MapView)??
  45. ????????mapController?=?myMapView.getController();???
  46. ????????//設置地圖支持設置模式??
  47. ????????myMapView.setEnabled(true);??
  48. ????????//設置地圖支持點擊??
  49. ????????myMapView.setClickable(true);?????
  50. ????????//設置縮放控制,這里我們自己實現縮放菜單??
  51. ????????myMapView.displayZoomControls(true);????
  52. ????????myMapView.setBuiltInZoomControls(true);???
  53. ????????//*******************************************************************???????
  54. ????????設置設置地圖目前縮放大小倍數(從1到21)??
  55. ????????mapController.setZoom(17);??
  56. ????????//設置使用MyLocationOverlay來繪圖??
  57. ????????myPosition=new?MyLocationOverlay();??
  58. ??????????
  59. ????????List<Overlay>?overlays=myMapView.getOverlays();??
  60. ????????overlays.add(myPosition);??
  61. ????????//設置Criteria(標準服務商)的信息??
  62. ????????Criteria?criteria?=new?Criteria();??
  63. ????????//*****設置服務商提供的精度要求,以供篩選提供商************************??
  64. ????????criteria.setAccuracy(Criteria.POWER_HIGH);//表明所要求的經緯度的精度??????????????
  65. ????????criteria.setAltitudeRequired(false);?//高度信息是否需要提供??
  66. ????????criteria.setBearingRequired(false);??//壓力(氣壓?)信息是否需要提供??
  67. ????????criteria.setCostAllowed(false);??//是否會產生費用??
  68. ????????criteria.setPowerRequirement(Criteria.POWER_MEDIUM);//最大需求標準??
  69. ????????//*****************************************************??
  70. ????????//取得效果最好的criteria??
  71. ????????String?provider=locationManager.getBestProvider(criteria,?true);??
  72. ????????//得到坐標相關的信息??
  73. ????????Location?location=locationManager.getLastKnownLocation(provider);??
  74. ????????//更新位置信息??
  75. ????????updateWithNewLocation(location);??
  76. ????????//注冊一個周期性的更新,3000ms更新一次,0代表最短距離??
  77. ????????//locationListener用來監聽定位信息的改變(OnLocationChanged)??
  78. ????????locationManager.requestLocationUpdates(provider,?3000,?0,locationListener);??
  79. ????}??
  80. ??????
  81. ????//更新位置信息??
  82. ????private?void?updateWithNewLocation(Location?location)???
  83. ????{??
  84. ????????String?latLongString;?//聲明經緯度的字符串??
  85. ????????TextView?myLocationText?=?(TextView)findViewById(R.id.TextView01);??
  86. ????????//初始化地址為沒有找到,便于處理特殊情況??
  87. ????????String?addressString="沒有找到地址/n";??
  88. ????????if(location!=null)??
  89. ????????{??
  90. ????????????//****************獲取當前的經緯度,并定位到目標*************************??
  91. ????????????//為繪制標志的類設置坐標??
  92. ????????????myPosition.setLocation(location);??
  93. ????????????//取得經度和緯度??
  94. ????????????Double?geoLat=location.getLatitude()*1E6;??
  95. ????????????Double?geoLng=location.getLongitude()*1E6;??
  96. ????????????//將其轉換為int型??
  97. ????????????GeoPoint?point=new?GeoPoint(geoLat.intValue(),geoLng.intValue());??
  98. ????????????//定位到指定坐標??
  99. ????????????mapController.animateTo(point);??
  100. ????????????//*********************************************************************??
  101. ????????????double?lat=location.getLatitude();??//獲得經緯度??
  102. ????????????double?lng=location.getLongitude();??
  103. ????????????latLongString="經度:"+lat+"/n緯度:"+lng;???//設置經緯度字符串??
  104. ??????????????
  105. ???????????//?double?latitude=location.getLatitude();??
  106. ????????????//double?longitude=location.getLongitude();??
  107. ????????????//根據地理位置來確定編碼??
  108. ????????????Geocoder?gc=new?Geocoder(this,Locale.getDefault());??
  109. ????????????try??
  110. ????????????{??
  111. ????????????????//取得地址相關的一些信息:經度、緯度??
  112. ????????????????List<Address>?addresses=gc.getFromLocation(lat,?lng,1);??
  113. ????????????????StringBuilder?sb=new?StringBuilder();??
  114. ????????????????if(addresses.size()>0)??
  115. ????????????????{??
  116. ????????????????????Address?address=addresses.get(0);??
  117. ????????????????????for(int?i=0;i<address.getMaxAddressLineIndex()-1;i++)??
  118. ????????????????????????sb.append(address.getAddressLine(i)).append(",");???????????????????????
  119. ????????????????????????//獲得地址sb.append(address.getLocality()).append("/n");??
  120. ????????????????????????//獲得郵編sb.append(address.getPostalCode()).append("/n");??
  121. ????????????????????????sb.append(address.getCountryName());??
  122. ????????????????????????addressString=sb.toString();??
  123. ????????????????}??
  124. ????????????}catch(IOException?e){}??
  125. ????????}??
  126. ????????else??
  127. ????????{??
  128. ????????????latLongString="沒有找到坐標./n";??
  129. ????????}??
  130. ????????//顯示??
  131. ????????myLocationText.setText("您當前的位置如下:/n"+latLongString+"/n"+addressString);??
  132. ????}??
  133. ????//監聽位置信息的改變??
  134. ????private?final?LocationListener?locationListener=new?LocationListener()??
  135. ????{??
  136. ????????//當坐標改變時觸發此函數??
  137. ????????public?void?onLocationChanged(Location?location)??
  138. ????????{??
  139. ????????????updateWithNewLocation(location);??
  140. ????????}??
  141. ????????//Provider被disable時觸發此函數,比如GPS被關閉???
  142. ????????public?void?onProviderDisabled(String?provider)??
  143. ????????{??
  144. ????????????updateWithNewLocation(null);??
  145. ????????}??
  146. ????????//Provider被enable時觸發此函數,比如GPS被打開??
  147. ????????public?void?onProviderEnabled(String?provider){}??
  148. ????????//Provider的轉態在可用、暫時不可用和無服務三個狀態直接切換時觸發此函數??
  149. ????????public?void?onStatusChanged(String?provider,int?status,Bundle?extras){}??
  150. ????};??
  151. ????//方法默認是true,服務器所知的狀態列信息是否需要顯示??
  152. ????protected?boolean?isRouteDisplayed()??
  153. ????{??
  154. ????????return?false;??
  155. ????}??
  156. ??????
  157. ????class?MyLocationOverlay?extends?Overlay??
  158. ????{??
  159. ????????Location?mLocation;??
  160. ????????//在更新坐標,以便畫圖??
  161. ????????public?void?setLocation(Location?location)??
  162. ????????{??
  163. ????????????mLocation?=?location;??
  164. ????????}??
  165. ????????@Override??
  166. ????????public?boolean?draw(Canvas?canvas,?MapView?mapView,?boolean?shadow,?long?when)??
  167. ????????{??
  168. ????????????super.draw(canvas,?mapView,?shadow);??????????????
  169. ????????????Paint?paint?=?new?Paint();??
  170. ????????????Point?myScreenCoords?=?new?Point();??
  171. ????????????//?將經緯度轉換成實際屏幕坐標??
  172. ????????????GeoPoint?tmpGeoPoint?=?new?GeoPoint((int)(mLocation.getLatitude()*1E6),(int)(mLocation.getLongitude()*1E6));??????
  173. ????????????mapView.getProjection().toPixels(tmpGeoPoint,?myScreenCoords);??
  174. ????????????//*********paint相關屬性設置*********??
  175. ????????????paint.setStrokeWidth(0);//文??
  176. ????????????paint.setARGB(255,?255,?0,?0);??
  177. ????????????paint.setStyle(Paint.Style.STROKE);??
  178. ????????????//***********************************??
  179. ????????????Bitmap?bmp?=?BitmapFactory.decodeResource(getResources(),?R.drawable.green_dot);??
  180. ????????????canvas.drawBitmap(bmp,?myScreenCoords.x,?myScreenCoords.y,?paint);??
  181. ????????????canvas.drawText("您目前的位置",?myScreenCoords.x,?myScreenCoords.y,?paint);??
  182. ????????????return?true;??
  183. ????????}??
  184. ????}??
  185. }??

?

代碼參考網絡,加以修改優化,謝謝

7.程序運行截圖,前提是在命令行下輸入geo fix 121.5 31.24(定位到上海東方明珠),在命令行下可以輸入其他坐標,系統會根據坐標顯示其他位置,如接著輸入geo fix 113.325 23.113(定位到廣州海心沙),不知為什么輸入坐標的時候經常會不識別,有時能夠成功而有時不行,郁悶,求解……

轉載于:https://www.cnblogs.com/Free-Thinker/p/3606475.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/273826.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/273826.shtml
英文地址,請注明出處:http://en.pswp.cn/news/273826.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

python 鏈表 【測試題】

文章目錄注意&#xff1a;實例講解1 .鏈表基本功能2. 根據值刪除鏈表中的節點信息答案&#xff1a;3.反轉一個單鏈表信息答案4.合并兩個有序鏈表信息答案5.刪除排序鏈表中的重復元素信息答案6.移除鏈表元素信息7.環形鏈表信息進階思路答案注意&#xff1a; 這里的head是只存儲…

WebService應用一例,帶有安全驗證

1、創建WEB項目&#xff0c;添加WEB服務WebService1.asmx&#xff0c;代碼如下&#xff1a; 1 using System;2 using System.Collections.Generic;3 using System.Linq;4 using System.Web;5 using System.Web.Services;6 7 namespace WebService8 {9 /// <summary> …

linux集成開發環境

Linux操作系統的種種集成開發環境隨著Linux的逐漸興起&#xff0c;已經有為數眾多的程序在上面馳騁了&#xff0c;許多開發環境(Development Environment)也應運而生。好的開發環境一定是集成了編輯、編譯和調試等多項功能并且易于使用。本文介紹了一些在Linux上流行的開發環境…

mysql技術內幕《讀書筆記》

文章目錄1. mysql 體系結構和存儲引擎1.5 連接mysql1.5.11. mysql 體系結構和存儲引擎 1.5 連接mysql 連接mysql操作是一個連接進程和mysql數據庫實例進行通信。 本質是進程通信&#xff0c;常用的進程通信方式有管道&#xff0c;命名管道&#xff0c;命名字&#xff0c;TCP/…

DEDECMS全版本gotopage變量XSS ROOTKIT 0DAY

影響版本&#xff1a; DEDECMS全版本 漏洞描敘&#xff1a; DEDECMS后臺登陸模板中的gotopage變量未效驗傳入數據&#xff0c;導致XSS漏洞。 \dede\templets\login.htm 65行左右 <input type"hidden" name"gotopage" value"<?php if(!empty($g…

Android開源庫loopj的android-async-http的 JsonHttpResponseHandler 存在死循環GC_CONCURRENT

我現在用的是 AndroidAsyncHttp 1.4.4 版本&#xff0c;之前遇到一個很奇怪的問題&#xff0c; 當使用 JsonHttpResponseHandler 解析請求的頁面出現服務器錯誤或其他情況返回的內容不是 JSON 字符串時不會調用自己復寫實現的 onSuccess 或者 onFailure 方法&#xff0c;將會出…

python【進階】4.文本和字節序列

文章目錄1. 字符、碼位和字節表述4.1字符問題2. bytes、bytearray 和 memoryview 等二進制序列的獨特特性3. 全部 Unicode 和陳舊字符集的編解碼器4.避免和處理編碼錯誤5.處理文本文件的最佳實踐6.默認編碼的陷阱和標準 I/O 的問題7.規范化 Unicode 文本,進行安全的比較8.規范化…

C#序列化和反序列化

序列化和反序列化我們可能經常會聽到&#xff0c;其實通俗一點的解釋&#xff0c;序列化就是把一個對象保存到一個文件或數據庫字段中去&#xff0c;反序列化就是在適當的時候把這個文件再轉化成原來的對象使用。我想最主要的作用有&#xff1a; 1、在進程下次啟動時讀取上次保…

python【進階】5.一等函數(注銷)

在 Python 中,函數是一等對象。編程語言理論家把“一等對象”定義為滿足下述條件的程 序實體: 在運行時創建能賦值給變量或數據結構中的元素能作為參數傳給函數能作為函數的返回結果 在 Python 中,所有函數都是一等對象。 5.1 把函數視作對象 >>> def d(n): ... …

進程狀態轉換(了解)

進程三個基本狀態&#xff1a;就緒、阻塞、運行 這個比較簡單&#xff0c;進程創建后進入就緒狀態、然后若CPU空閑或能打斷CPU正在執行的進程&#xff08;優先級低的&#xff09;&#xff0c;那么就緒狀態轉換成運行態&#xff0c;運行時&#xff0c;進程需要用到其他資源&…

rebuild online意外終止導致ora-8104錯誤的實驗

rebuild online意外終止導致ora-8104錯誤的實驗 SQL> !oerr ora 810408104, 00000, "this index object %s is being online built or rebuilt"// *Cause: the index is being created or rebuild or waited for recovering // from the online (re)build // *Act…

關于range方法,如果你覺得python很簡單就錯了

前言&#xff1a;在系統學習迭代器之前&#xff0c;我一直以為 range() 方法也是用于生成迭代器的&#xff0c;現在卻突然發現&#xff0c;它生成的只是可迭代對象&#xff0c;而并不是迭代器&#xff01; 1、range() 是什么&#xff1f; 對于 range() 函數&#xff0c;有幾個注…

centos下crontab的使用

1.作用使用crontab命令可以修改crontab配置文件&#xff0c;然后該配置由cron公用程序在適當的時間執行&#xff0c;該命令使用權限是所有用戶。2.格式crontab [-u user] {-l | -r | -e}3.crontab命令選項: -u指定一個用戶, -l列出某個用戶的任務計劃, -r刪除某個用戶的任務, -…

關于python3中的包operator(支持函數式編程的包)

文章目錄1.functools2.operator.itemgetter3.operator.attrgetter雖然 Guido 明確表明,Python 的目標不是變成函數式編程語言,但是得益于 operator 和 functools 等包的支持,函數式編程風格也可以信手拈來。接下來的兩節分別介紹這兩 個包。 1.functools 示例1 使用 reduce 函…

collections 中的namedtuple

文章目錄namedtuple 基本用法namedtuple特性_make(iterable)_asdict()_replace(**kwargs)_fields_fields_defaults參考&#xff1a;namedtuple 基本用法 Tuple還有一個兄弟&#xff0c;叫namedtuple。雖然都是tuple&#xff0c;但是功能更為強大。對于namedtuple&#xff0c;你…

abap 中modify 的使用

1、modify table itab from wa Transporting f1 f2 ... 表示表itab中符合工作區wa 中關鍵字的一條數據的 f1 f2字段會被wa中對應的字段值更新。 modify用于更新和新增數據&#xff0c;當表中沒有數據時就新增&#xff0c;有就修改。 2、在使用binary search 時一定要先排序&am…

python[進階] 6.使用一等函數實現設計模式

文章目錄6.1.1 經典的“策略”模式6.1.2 使用函數實現“策略”模式6.1.3 選擇最佳策略&#xff1a;簡單的6.1.4 找出模塊中的全部6.2 “命令”模式6.1.1 經典的“策略”模式 為抽象基類&#xff08;Abstract Base Class&#xff0c;ABC&#xff09;&#xff0c;這么做是為了使…

2014阿里巴巴校園招聘筆試題 - 中南站

轉載于:https://www.cnblogs.com/gotodsp/articles/3530329.html

python中一些特殊方法的作用

我們先暫且稱呼為特殊方法。 單下劃線開頭&#xff08;_foo&#xff09;雙下劃線開頭的&#xff08;__foo&#xff09;雙下劃線開頭和結尾的&#xff08; __foo__&#xff09;代表不能直接訪問的類屬性&#xff0c;需通過類提供的接口進行訪問&#xff0c;不能用“from xxx im…

Spring的IOC原理[通俗解釋一下]

1. IoC理論的背景 我們都知道&#xff0c;在采用面向對象方法設計的軟件系統中&#xff0c;它的底層實現都是由N個對象組成的&#xff0c;所有的對象通過彼此的合作&#xff0c;最終實現系統的業務邏輯。 圖1&#xff1a;軟件系統中耦合的對象 如果我們打開機械式手表的后蓋&am…