1. 程式人生 > >手機定位:獲取我的位置經緯度

手機定位:獲取我的位置經緯度

最近做一個關於 手機定位的功能,自己記錄一下,一來加深記憶,二來希望能給大家一點思路,程式碼親測有效。

直接貼程式碼:

清單檔案要加的許可權,程式碼中還有動態許可權申請

<!--定位許可權-->
<uses-permission android:name="android.permission.ACCESS_FIND_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name=
"android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

佈局不在給出只有一個button和一個textView

public class MainActivity extends AppCompatActivity {
    private Button getLocation;
private TextView myLocation;
private double latitudeGps
; private double longitudeGps; private double latitudeNet; private double longitudeNet; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); setListener(); } private void setListener() { getLocation
.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { myLocation.setText("您現在的位置經度是:"+longitudeGps+"維度是:"+latitudeGps); } }); } private void initView() { getLocation = (Button) findViewById(R.id.getLocation_btn); myLocation = (TextView) findViewById(R.id.myLocation_tv); getMyLocation(); } public void getMyLocation() { //要加動態許可權,清單檔案也要加許可權 //被拒絕情況 if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_DENIED) { //申請 ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION}, 0); initLocation(); } else { initLocation(); } } //載入本地位置 private void initLocation() { LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); Criteria criteria = new Criteria(); criteria.setAccuracy(Criteria.ACCURACY_FINE); criteria.setAltitudeRequired(false); criteria.setBearingRequired(false); criteria.setCostAllowed(false); criteria.setPowerRequirement(Criteria.POWER_LOW); // if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { // TODO: Consider calling // ActivityCompat#requestPermissions // here to request the missing permissions, and then overriding // int[] grantResults) // to handle the case where the user grants the permission. See the documentation // for ActivityCompat#requestPermissions for more details. return; } Location locationGps = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); Location locationNet = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); if (locationGps!=null){ latitudeGps = locationGps.getLatitude(); longitudeGps = locationGps.getLongitude(); choseLocationType(); }else if (locationNet!=null){ latitudeNet = locationNet.getLatitude(); longitudeNet = locationGps.getLongitude(); choseLocationType(); }else { Toast.makeText(MainActivity.this,"獲取位置失敗,請檢查網路",Toast.LENGTH_SHORT).show(); } LocationListener locationListener = new LocationListener() { @Override public void onLocationChanged(Location location) {} @Override public void onStatusChanged(String s, int i, Bundle bundle) {} @Override public void onProviderEnabled(String s) {} @Override public void onProviderDisabled(String s) {} }; //更新(間隔自己定義) locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,10,1000,locationListener); } private void choseLocationType() { if (latitudeNet>0&&latitudeGps<=0){ latitudeGps=latitudeNet; }else if (longitudeNet>0&&longitudeGps<=0){ longitudeGps=longitudeNet; } } }

請注意以下幾點:

一:第一次開啟app可能獲取到為0.0,關閉app再代開就可以,以後都正常,因為這裡用了getLastKnowLocation 獲得最後一次的

二:本文是獲取經緯度,如果您還需要獲取具體位置,可以嘗試一下Goolge的location包提供的Geocoder類,他可以把經緯度轉成具體位置還可以把具體位置轉為經緯度,但鑑於Google服務的穩定性,您可以考慮一下藉助百度的服務來獲取具體位置資訊。

三:關於Criteria類,這裡引用一位大神的翻譯我就直接貼過來了

在使用android lcoation的時候,可能不希望自己去硬性的選擇是GPS服務還是NETWORK服務,可能是我們考慮的因素有很多,自己很難決定,Android SDK提供了一個類Criteria,直譯為標準。

在使用android lcoation的時候,可能不希望自己去硬性的

public String getBestProvider (Criteria criteria, boolean enabledOnly)

Since: API Level 1

Returns the name of the provider that best meets the given criteria. Only providers that are permitted to be accessed by the calling activity will be returned. If several providers meet the criteria, the one with the best accuracy is returned. If no provider meets the criteria, the criteria are loosened in the following sequence:

//翻譯:返回滿足給定的criteria(標準)的最佳provider,(前面一篇文章提到過兩種重要provider的區別)。只有當provider許可權允許的時候才會返回,如果幾個provider均滿足criteria設定的標準,最貼近標準的provider將會被返回。如果沒有provider滿足,標準將按照下面的序列放寬鬆。

    power requirement

    accuracy

    bearing

    speed

    altitude

選擇是GPS服務還是NETWORK服務,可能是我們考慮的因素有很多,自己很難決定,Android SDK提供了一個類Criteria,直譯為標準。

相關推薦

手機定位獲取位置經緯度

最近做一個關於 手機定位的功能,自己記錄一下,一來加深記憶,二來希望能給大家一點思路,程式碼親測有效。 直接貼程式碼: 清單檔案要加的許可權,程式碼中還有動態許可權申請 <!--定位許可權--> <uses-permission android:name

iOS獲取當前位置經緯度

最近在做一個公交車查詢的專案,需要定位到當前位置以便進行附近站點查詢,和大家分享一下怎樣獲取自己當前位置的經緯度 首先新增CoreLocation.framework庫: 引用標頭檔案並宣告CLLocationManagerDelegate代理: 接下來宣告要用到的變數:

高德地圖和百度地圖獲取當前位置經緯度

高德 匯入高德js mapObj = new AMap.Map('iCenter'); mapObj.plugin('AMap.Geolocation', function () { geolocation = new AMap.Geolocation({ e

使用百度定位SDK獲取當前位置的資訊

                1、AndroidManifext.xml<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"   

微信公眾號、地圖定位獲取地理位置

本內容目標是在微信公眾號中開啟網頁獲取使用者的定位資訊。 1.騰訊地圖定位沒有將地圖頁面調出來,只是定位後返回json形式的地址 2.使用百度地圖定位,調出地圖介面,獲取經緯度,alert地址 <!DOCTYPE html> <html>   &l

activiti自定義流程之整合(六)獲取的申請任務

流程啟動後,流程節點便進入到了任務相關的部分。可以看到我之前的做法是在啟動節點就綁定了form表單,啟動時就填寫相關的資料。實際上在之前我的做法是不對開始節點做任何操作,知道任務節點的時候再填寫相關的資料進行設定。 至於這兩種方式的優劣,我暫時還不太確定,單獨從功能上來說

手機定位獲取使用者位置資訊

很多時候,我們需要獲取手機端使用者的位置資訊,當然這需要使用者的同意授權才能進行獲取,下面介紹下我在專案中使用的定位 由於本專案web端使用的是高德地圖,因此手機端定位也使用高德的定位介面 <html> <head> <meta ch

根據HTML5 獲取當前位置經緯度

yun .get rip 變量 src ted call location gets 是想讓地圖的定位用戶位置更準確一些。 查看了介紹: http://www.w3school.com.cn/html5/html_5_geolocation.asp 看介紹中拿數據挺簡單。

微信小程序獲取地理定位和顯示相應的城市名稱。

~~ 域名 get 參考 轉換成 round .com 選擇 不想   最近在看微信小程序,遇到地理定位顯示城市名稱的問題。本文就是記錄這一過程。 解決方案

xpath的使用定位獲取文本和屬性值

world src @class foo posit on() .text value oot myPage = ‘‘‘<html><title>TITLE</title><body><h1></h1>

Shell-case獲取執行的指令碼所在目錄的位置

     今天和大家分享一個,大家shell指令碼中常常想用,但卻很難完成的一個功能的shell指令碼,如何獲取當前執行shell指令碼所在目錄位置。      很多時候,我們寫一個shell指令碼在實現一個功能的時候不單單是一個shell

獲取經緯度&根據經緯度獲取地理位置

package com.example.ceshi; import android.Manifest; import android.content.Context; import android.content.pm.PackageManager; import android.location

java api從高德地圖獲取某個位置經緯度

import com.fasterxml.jackson.databind.JsonNode; import com.ning.http.client.AsyncHttpClient; import com.ning.http.client.AsyncHttpClientConfig; impor

js獲取當前位置的地理座標(經緯度

由於專案需求,需要獲取手機的當前位置,,,,,問度娘,然後做了一個小demo if(navigator.geolocation) { navigator.geolocation.getCurrentPosition( function (position) {

微信獲取好友真實ip, qq獲取ip,經緯度定位 隱蔽獲取 微信查ip

微信好友ip查詢獲取 QQ好友ip查詢獲取 隱蔽方式 ip獲取不需要好友任何操作 不需要點選任何東西 即可獲取真實的ip地址 經緯度跳轉定位 可利用XML欺騙對方點選 header("Location: url:xxxxxx.png "); 以下是成品 只要你會搭建

H5獲取使用者當前位置獲取使用者的經緯度

H5獲取使用者當前位置,粗略分為3種方法: 1、H5自帶的方法,獲取經緯度 2、通過地圖提供的JS。獲取位置 3、通過微信的API(這個需要公眾號 / 小程式) 1、通過H5自帶的獲取經緯度的方法 優點: 需要引用的資源較少,H5自帶的方法 缺點: 1、獲取的經緯度偏差較大,如果需

html5實現獲取地理位置資訊並定位

這裡主要講h5實現獲取地理位置資訊並定位功能,本文講解了原生h5,百度地圖,谷歌地圖等三種獲取地理資訊並定位的方法,需要的朋友可以參考下: h5提供了地理位置功能(Geolocation API),能確定使用者位置,我們可以藉助h5的該特性開發基於地理位置資訊的應用,本文集合實力給大家分享下如何

兩點GPS經緯度獲取區域性位置的理論

1、地球赤道上環繞地球一週走一圈共40075.04公里,而@一圈分成360°,而每1°(度)有60,每一度一秒在赤道上的長度計算如下: 40075.04km/360°=111.31955km 111.3

AngularJS進階 二十 HTML5實現獲取地理位置資訊並定位功能

HTML5實現獲取地理位置資訊並定位功能 注:請點選此處進行充電! 前言      這篇文章主要介紹了HTML5實現獲取地理位置資訊並定位功能,本文講解了原生HTML5、百度地圖、谷歌地圖等三種獲取理位置資訊並定位的方法,需要的朋友可以參考下。 H

PHP根據經緯度獲取地理位置-高德api

本次使用的高德地圖web服務端api中的逆地理編碼 逆地理編碼:將經緯度轉換為詳細結構化的地址,且返回附近周邊的POI、AOI資訊。 例如:116.480881,39.989410 轉換地址描述後:北京市朝陽區阜通東大街6號 逆地理編碼API服務地址 https://restapi.