Android实现地理定位功能

2020-10-22 09:58:06 浏览数 (1)

最近试着写了一下Android的定位,代码在下面,比较简单 首先需要的权限有

代码语言:javascript复制
<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"/  

然后是里面的代码

代码语言:javascript复制
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(); 
} 
} 

最后上图:

以上就是本文的全部内容,希望对大家的学习有所帮助。

0 人点赞