這篇文章給大家分享的是有關(guān)如何使用Android實(shí)現(xiàn)地理定位功能的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。
在平利等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場(chǎng)前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站 網(wǎng)站設(shè)計(jì)制作按需網(wǎng)站設(shè)計(jì),公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),品牌網(wǎng)站制作,營(yíng)銷型網(wǎng)站,外貿(mào)營(yíng)銷網(wǎng)站建設(shè),平利網(wǎng)站建設(shè)費(fèi)用合理。
首先需要的權(quán)限有
然后是里面的代碼
package com.example.a5.myapplication; import android.Manifest; import android.content.Context; import android.content.pm.PackageManager; import android.location.Criteria; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.support.v4.app.ActivityCompat; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.view.Window; import android.view.WindowManager; import android.webkit.WebView; import android.widget.Button; import android.widget.TextView; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); //全屏 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.activity_main); Button button = (Button) findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //獲取位置服務(wù) LocationManager locationManager = (LocationManager) MainActivity.this.getSystemService(Context.LOCATION_SERVICE); Criteria criteria = new Criteria(); //ACCURACY_HIGH/ACCURACY_LOW精度選擇 criteria.setAccuracy(Criteria.ACCURACY_FINE); //高度 criteria.setAltitudeRequired(true); //方位信息 criteria.setBearingRequired(true); //是否允許付費(fèi) criteria.setCostAllowed(true); //對(duì)電量的要求 criteria.setPowerRequirement(Criteria.POWER_LOW); //速度 criteria.setSpeedRequired(true); //獲取最佳服務(wù) String provider = locationManager.getBestProvider(criteria, true); //權(quán)限檢查 if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { return; } //獲取到了位置 Location location = locationManager.getLastKnownLocation(provider); //開(kāi)啟地理位置監(jiān)聽(tīng)定位類型、毫秒、米、監(jiān)聽(tīng)時(shí)間 locationManager.requestLocationUpdates(provider,3000,1,new listener()); locateResult(location); } }); } public class listener implements LocationListener { @Override public void onLocationChanged(Location location) { //位置變化,獲取最新的位置 locateResult(location); } @Override public void onStatusChanged(String provider, int status, Bundle extras) { } @Override public void onProviderEnabled(String provider) { } @Override public void onProviderDisabled(String provider) { } } public void locateResult(Location location){ new AlertDialog.Builder(MainActivity.this) .setMessage("緯度" + String.valueOf(location.getLatitude()) + "\n經(jīng)度" + String.valueOf(location.getLongitude()) + "\n速度" + String.valueOf(location.getSpeed()) + "\n高度" + String.valueOf(location.getAltitude()) + "\n朝向" + String.valueOf(location.getBearing()) ) .setNegativeButton("取消",null) .show(); } }
最后上圖:
感謝各位的閱讀!關(guān)于“如何使用Android實(shí)現(xiàn)地理定位功能”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!