实验02-android-原生定位
程序员文章站
2022-04-12 08:39:46
package com.example.yuansheng;import java.io.IOException;import java.util.List;import java.util.Locale;import android.app.Activity;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import andro....
添加权限
MainActiivty.java
package com.example.yuansheng;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.GpsStatus.Listener;
import android.location.Address;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
LocationManager locationManager;
Location location;
String bestProvider;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.tv);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER) || locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
bestProvider = locationManager.getBestProvider(getCriteria(), true);
Location location = locationManager.getLastKnownLocation(bestProvider);
updateView(location);
locationManager.requestLocationUpdates(bestProvider, 1000, 1, listener);
return;
}
else{
Toast.makeText(this, "请打开定位功能",Toast.LENGTH_LONG ).show();
Intent intent = new Intent(Settings.ACTION_LOCALE_SETTINGS);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivityForResult(intent, 0);
}
}
LocationListener listener = new LocationListener(){
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
updateView(location);
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
updateView(null);
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
Location location = locationManager.getLastKnownLocation(provider);
updateView(location);
}
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub
}
};
private Criteria getCriteria() {
// TODO Auto-generated method stub
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(true);
criteria.setCostAllowed(true);
criteria.setSpeedRequired(true);
criteria.setPowerRequirement(Criteria.NO_REQUIREMENT);
criteria.setBearingAccuracy(criteria.ACCURACY_HIGH);
criteria.setHorizontalAccuracy(criteria.ACCURACY_HIGH);
criteria.setVerticalAccuracy(criteria.ACCURACY_HIGH);
return criteria;
}
private void updateView(Location location) {
// TODO Auto-generated method stub
// Address address = getAddress( this,location);
StringBuilder stringBuilder = new StringBuilder();
if(location !=null){
stringBuilder.append("当前经度是:"+location.getLongitude()+"\n");
stringBuilder.append("当前纬度是"+location.getLatitude()+"\n");
stringBuilder.append("当前海拔是"+location.getAltitude()+"\n");
stringBuilder.append("当前时间是"+location.getTime()+"\n");
// stringBuilder.append("当前国家是"+address.getCountryName()+"\n");
// stringBuilder.append("当前省份是"+address.getAdminArea()+"\n");
textView.setText(stringBuilder.toString());
}else {
textView.setTag("location is null");
}
}
private Address getAddress(Context context,Location location) {
// TODO Auto-generated method stub
Geocoder gecoder= new Geocoder(context,Locale.getDefault());
try {
List<Address> lists = gecoder.getFromLocation(location.getLongitude(), location.getLatitude(), 1);
if(lists.size() >0){
return lists.get(0);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
activity_main.xml略
实验结果
项目需要注意添加权限,Criteria用于找到合适的地理位置提供者,有三种:gps,移动网络,主动发送位置
本文地址:https://blog.csdn.net/weixin_44123412/article/details/107482291