日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android下实现GPS定位服务

發布時間:2023/12/10 Android 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android下实现GPS定位服务 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.申請Google API Key,參考前面文章

2.實現GPS的功能需要使用模擬器進行經緯度的模擬設置,請參考前一篇文章進行設置

3.創建一個Build Target為Google APIs的項目

4.修改Androidmanifest文件:

?

view plain
  • <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文件

    ?

    view plain
  • <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.代碼清單:

    ?

    ?

    view plain
  • 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(定位到廣州海心沙),不知為什么輸入坐標的時候經常會不識別,有時能夠成功而有時不行,郁悶,求解……

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

    總結

    以上是生活随笔為你收集整理的Android下实现GPS定位服务的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。