这篇文章给大家分享的是有关如何使用Android实现地理定位功能的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
首先需要的权限有
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <uses-permission android:name="android.permission.INTERNET"/>
然后是里面的代码
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) { //获取位置服务 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); //是否允许付费 criteria.setCostAllowed(true); //对电量的要求 criteria.setPowerRequirement(Criteria.POWER_LOW); //速度 criteria.setSpeedRequired(true); //获取最佳服务 String provider = locationManager.getBestProvider(criteria, true); //权限检查 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); //开启地理位置监听定位类型、毫秒、米、监听时间 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经度" + String.valueOf(location.getLongitude()) + "\n速度" + String.valueOf(location.getSpeed()) + "\n高度" + String.valueOf(location.getAltitude()) + "\n朝向" + String.valueOf(location.getBearing()) ) .setNegativeButton("取消",null) .show(); } }
最后上图:
感谢各位的阅读!关于“如何使用Android实现地理定位功能”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!