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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android下实现GPS定位服务

發(fā)布時(shí)間:2023/12/10 Android 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android下实现GPS定位服务 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1.申請(qǐng)Google API Key,參考前面文章

2.實(shí)現(xiàn)GPS的功能需要使用模擬器進(jìn)行經(jīng)緯度的模擬設(shè)置,請(qǐng)參考前一篇文章進(jìn)行設(shè)置

3.創(chuàng)建一個(gè)Build Target為Google APIs的項(xiàng)目

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值請(qǐng)相應(yīng)修改為自己的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實(shí)例??
  • ????????LocationManager?locationManager;??
  • ????????String?context=Context.LOCATION_SERVICE;??
  • ????????locationManager=(LocationManager)getSystemService(context);??
  • ????????myMapView=(MapView)findViewById(R.id.MapView01);??
  • ????????//取得MapController實(shí)例,控制地圖??
  • ????????mapController=myMapView.getController();??
  • ????????//設(shè)置顯示模式為街景模式??
  • ????????myMapView.setStreetView(true);??
  • ??????????
  • ????????//*************使用系統(tǒng)自帶的控件放大縮小視圖***************************??
  • ????????//取得MapController對(duì)象(控制MapView)??
  • ????????mapController?=?myMapView.getController();???
  • ????????//設(shè)置地圖支持設(shè)置模式??
  • ????????myMapView.setEnabled(true);??
  • ????????//設(shè)置地圖支持點(diǎn)擊??
  • ????????myMapView.setClickable(true);?????
  • ????????//設(shè)置縮放控制,這里我們自己實(shí)現(xiàn)縮放菜單??
  • ????????myMapView.displayZoomControls(true);????
  • ????????myMapView.setBuiltInZoomControls(true);???
  • ????????//*******************************************************************???????
  • ????????設(shè)置設(shè)置地圖目前縮放大小倍數(shù)(從1到21)??
  • ????????mapController.setZoom(17);??
  • ????????//設(shè)置使用MyLocationOverlay來繪圖??
  • ????????myPosition=new?MyLocationOverlay();??
  • ??????????
  • ????????List<Overlay>?overlays=myMapView.getOverlays();??
  • ????????overlays.add(myPosition);??
  • ????????//設(shè)置Criteria(標(biāo)準(zhǔn)服務(wù)商)的信息??
  • ????????Criteria?criteria?=new?Criteria();??
  • ????????//*****設(shè)置服務(wù)商提供的精度要求,以供篩選提供商************************??
  • ????????criteria.setAccuracy(Criteria.POWER_HIGH);//表明所要求的經(jīng)緯度的精度??????????????
  • ????????criteria.setAltitudeRequired(false);?//高度信息是否需要提供??
  • ????????criteria.setBearingRequired(false);??//壓力(氣壓?)信息是否需要提供??
  • ????????criteria.setCostAllowed(false);??//是否會(huì)產(chǎn)生費(fèi)用??
  • ????????criteria.setPowerRequirement(Criteria.POWER_MEDIUM);//最大需求標(biāo)準(zhǔn)??
  • ????????//*****************************************************??
  • ????????//取得效果最好的criteria??
  • ????????String?provider=locationManager.getBestProvider(criteria,?true);??
  • ????????//得到坐標(biāo)相關(guān)的信息??
  • ????????Location?location=locationManager.getLastKnownLocation(provider);??
  • ????????//更新位置信息??
  • ????????updateWithNewLocation(location);??
  • ????????//注冊(cè)一個(gè)周期性的更新,3000ms更新一次,0代表最短距離??
  • ????????//locationListener用來監(jiān)聽定位信息的改變(OnLocationChanged)??
  • ????????locationManager.requestLocationUpdates(provider,?3000,?0,locationListener);??
  • ????}??
  • ??????
  • ????//更新位置信息??
  • ????private?void?updateWithNewLocation(Location?location)???
  • ????{??
  • ????????String?latLongString;?//聲明經(jīng)緯度的字符串??
  • ????????TextView?myLocationText?=?(TextView)findViewById(R.id.TextView01);??
  • ????????//初始化地址為沒有找到,便于處理特殊情況??
  • ????????String?addressString="沒有找到地址/n";??
  • ????????if(location!=null)??
  • ????????{??
  • ????????????//****************獲取當(dāng)前的經(jīng)緯度,并定位到目標(biāo)*************************??
  • ????????????//為繪制標(biāo)志的類設(shè)置坐標(biāo)??
  • ????????????myPosition.setLocation(location);??
  • ????????????//取得經(jīng)度和緯度??
  • ????????????Double?geoLat=location.getLatitude()*1E6;??
  • ????????????Double?geoLng=location.getLongitude()*1E6;??
  • ????????????//將其轉(zhuǎn)換為int型??
  • ????????????GeoPoint?point=new?GeoPoint(geoLat.intValue(),geoLng.intValue());??
  • ????????????//定位到指定坐標(biāo)??
  • ????????????mapController.animateTo(point);??
  • ????????????//*********************************************************************??
  • ????????????double?lat=location.getLatitude();??//獲得經(jīng)緯度??
  • ????????????double?lng=location.getLongitude();??
  • ????????????latLongString="經(jīng)度:"+lat+"/n緯度:"+lng;???//設(shè)置經(jīng)緯度字符串??
  • ??????????????
  • ???????????//?double?latitude=location.getLatitude();??
  • ????????????//double?longitude=location.getLongitude();??
  • ????????????//根據(jù)地理位置來確定編碼??
  • ????????????Geocoder?gc=new?Geocoder(this,Locale.getDefault());??
  • ????????????try??
  • ????????????{??
  • ????????????????//取得地址相關(guān)的一些信息:經(jīng)度、緯度??
  • ????????????????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="沒有找到坐標(biāo)./n";??
  • ????????}??
  • ????????//顯示??
  • ????????myLocationText.setText("您當(dāng)前的位置如下:/n"+latLongString+"/n"+addressString);??
  • ????}??
  • ????//監(jiān)聽位置信息的改變??
  • ????private?final?LocationListener?locationListener=new?LocationListener()??
  • ????{??
  • ????????//當(dāng)坐標(biāo)改變時(shí)觸發(fā)此函數(shù)??
  • ????????public?void?onLocationChanged(Location?location)??
  • ????????{??
  • ????????????updateWithNewLocation(location);??
  • ????????}??
  • ????????//Provider被disable時(shí)觸發(fā)此函數(shù),比如GPS被關(guān)閉???
  • ????????public?void?onProviderDisabled(String?provider)??
  • ????????{??
  • ????????????updateWithNewLocation(null);??
  • ????????}??
  • ????????//Provider被enable時(shí)觸發(fā)此函數(shù),比如GPS被打開??
  • ????????public?void?onProviderEnabled(String?provider){}??
  • ????????//Provider的轉(zhuǎn)態(tài)在可用、暫時(shí)不可用和無服務(wù)三個(gè)狀態(tài)直接切換時(shí)觸發(fā)此函數(shù)??
  • ????????public?void?onStatusChanged(String?provider,int?status,Bundle?extras){}??
  • ????};??
  • ????//方法默認(rèn)是true,服務(wù)器所知的狀態(tài)列信息是否需要顯示??
  • ????protected?boolean?isRouteDisplayed()??
  • ????{??
  • ????????return?false;??
  • ????}??
  • ??????
  • ????class?MyLocationOverlay?extends?Overlay??
  • ????{??
  • ????????Location?mLocation;??
  • ????????//在更新坐標(biāo),以便畫圖??
  • ????????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();??
  • ????????????//?將經(jīng)緯度轉(zhuǎn)換成實(shí)際屏幕坐標(biāo)??
  • ????????????GeoPoint?tmpGeoPoint?=?new?GeoPoint((int)(mLocation.getLatitude()*1E6),(int)(mLocation.getLongitude()*1E6));??????
  • ????????????mapView.getProjection().toPixels(tmpGeoPoint,?myScreenCoords);??
  • ????????????//*********paint相關(guān)屬性設(shè)置*********??
  • ????????????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;??
  • ????????}??
  • ????}??
  • }??
  • ?

    代碼參考網(wǎng)絡(luò),加以修改優(yōu)化,謝謝

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

    轉(zhuǎn)載于:https://www.cnblogs.com/Free-Thinker/p/3606475.html

    總結(jié)

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

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。