当前位置: 首页 > news >正文

哈尔滨工程建设厦门百度seo

哈尔滨工程建设,厦门百度seo,超级采购小程序怎么注册,凤岗做网站文章目录 前言一、实现步骤二、使用步骤1.服务启动工具类2.实现LocationService 总结 前言 在android开发中地图和定位是很多软件不可或缺的内容,这些特色功能也给人们带来了很多方便。定位一般分为三种发方案:即GPS定位、Google网络定位以及基站定位。…

文章目录

  • 前言
  • 一、实现步骤
  • 二、使用步骤
    • 1.服务启动工具类
    • 2.实现LocationService
  • 总结


前言

在android开发中地图和定位是很多软件不可或缺的内容,这些特色功能也给人们带来了很多方便。定位一般分为三种发方案:即GPS定位、Google网络定位以及基站定位。


本文分别介绍GPS定位、以及基于Google的网络Wifi定位的详细步骤,(小米手机获取位置信息locationManager.getLastKnownLocation(provider)的Location一直为空,查了资料换了种获取手机getProviders的方式就可以了)

一、实现步骤

一般来说我们实现原生定位的流程大概是:先判断有无权限》有权限启动一个LocationSrevice去获取定位》最后携带所需的定位信息返回,进行开发。

二、使用步骤

1.服务启动工具类

代码如下(示例):大概步骤如下,权限请求可自定义开发关键的是LocationService

/*** 获取定位*/
public class MyLocationManager implements LocationService.LocationCallBack {private Activity context;private OnLocationListener onLocationListener;private String[] stringsLocation = new String[]{Permission.ACCESS_FINE_LOCATION, Permission.ACCESS_COARSE_LOCATION};@Overridepublic void Location_Return(double Location_latitude, double Location_longitude, String province, String city, String area, String featureName) {onLocationListener.OnLocation(Location_latitude, Location_longitude, province, city, area, featureName);}public interface OnLocationListener {void OnLocation(double Location_latitude, double Location_longitude, String province, String city, String area, String featureName);}public void setOnLocationListener(OnLocationListener onLocationListener) {this.onLocationListener = onLocationListener;}public MyLocationManager(@NonNull Activity context) {this.context = context;if (!XXPermissions.isGranted(context, stringsLocation)) {MessageDialog codeDialog = new MessageDialog(context, "位置信息权限使用说明", "为确保你能在******内使用位置信息******,******需要获取你的位置信息权限。允许后,你可以随时通过手机系统设置对授权进行管理。", "取消", "去授权");codeDialog.setCancelable(false);codeDialog.show();codeDialog.setOnSumbitTextCodeListener(() -> {doMainPermission();codeDialog.dismiss();});codeDialog.setOnCancelListener(() -> {codeDialog.dismiss();});} else {initData();}}private void doMainPermission() {XXPermissions.with(context).permission(stringsLocation).request(new OnPermissionCallback() {@Overridepublic void onGranted(@NonNull List<String> permissions, boolean allGranted) {if (allGranted) {initData();}}@Overridepublic void onDenied(@NonNull List<String> permissions, boolean doNotAskAgain) {if (doNotAskAgain) {}}});}@SuppressLint("MissingPermission")private void initData() {// 创建 Service 实例LocationService myService = new LocationService();// 设置回调接口myService.setCallback(this);// 启动 Service 并执行操作Intent serviceIntent = new Intent(context, LocationService.class);context.startService(serviceIntent);}
}
/**-------------------------/
不要忘了注册<serviceandroid:name=".utils.LocationService"android:enabled="true"android:exported="false" />

2.实现LocationService

代码如下(示例):

/*** 获取定位服务*/
public class LocationService extends Service {private LocationManager locationManager;private MyLocationListener myLocationListener;public static LocationCallBack mCallBack = null;public interface LocationCallBack {void Location_Return(double Location_latitude, double Location_longitude, String province, String city, String area, String featureName);}public void setCallback(LocationCallBack callback) {this.mCallBack = callback;}@Overridepublic IBinder onBind(Intent intent) {return null;}@SuppressLint("MissingPermission")@Overridepublic void onCreate() {super.onCreate();myLocationListener = new MyLocationListener();locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);try {GPSLocation();} catch (Exception e) {if (ObjectUtils.isNotEmpty(locationManager) && ObjectUtils.isNotEmpty(myLocationListener))locationManager.removeUpdates(myLocationListener); // 停止所有的定位服务stopSelf();  // 获取到经纬度以后,停止该service}}class MyLocationListener implements LocationListener {// 位置改变时获取经纬度@Overridepublic void onLocationChanged(Location location) {if (ObjectUtils.isNotEmpty(location)) {toGeocoder(location);}}// 状态改变时@Overridepublic void onStatusChanged(String provider, int status, Bundle extras) {}// 提供者可以使用时@Overridepublic void onProviderEnabled(String provider) {}// 提供者不可以使用时@Overridepublic void onProviderDisabled(String provider) {}}@SuppressLint("MissingPermission")private Location getLastKnownLocation(LocationManager locationManager) {List<String> providers = locationManager.getProviders(true);Location bestLocation = null;for (String provider : providers) {Location l = locationManager.getLastKnownLocation(provider);if (l == null) {continue;}if (bestLocation == null || l.getAccuracy() < bestLocation.getAccuracy()) {bestLocation = l;}}return bestLocation;}@SuppressLint("MissingPermission")private void GPSLocation() {Location location = getLastKnownLocation(locationManager);if (location != null) {//不为空,显示地理位置经纬度String longitude = "Longitude:" + location.getLongitude();String latitude = "Latitude:" + location.getLatitude();LogUtils.e("Location:" + longitude + latitude);toGeocoder(location);} else {LogUtils.e("Location:" + "Location为空");if (ObjectUtils.isNotEmpty(locationManager) && ObjectUtils.isNotEmpty(myLocationListener))locationManager.removeUpdates(myLocationListener); // 停止所有的定位服务stopSelf();  // 获取到经纬度以后,停止该service}}@SuppressLint("MissingPermission")private void toGeocoder(Location location) {String province = "";String city = "";String area = "";String featureName = "";try {Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());List<Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);if (ObjectUtils.isNotEmpty(addresses) && 0 < addresses.size()) {Address address = addresses.get(0);if (ObjectUtils.isNotEmpty(address)) {// 获取省份(province)province = address.getAdminArea();// 获取城市(City)city = address.getLocality();// 获取区县(area)area = address.getSubLocality();//  获取详细地址featureName = address.getFeatureName();// 获取街道地址String addressLine = address.getAddressLine(0);// 打印详细地址信息LogUtils.e("AddressInfo", "province: " + province);LogUtils.e("AddressInfo", "City: " + city);LogUtils.e("AddressInfo", "area: " + area);LogUtils.e("AddressInfo", "FeatureName: " + featureName);LogUtils.e("AddressInfo", "Address Line: " + addressLine);}mCallBack.Location_Return(location.getLatitude(), location.getLongitude(), province, city, area, featureName);}if (ObjectUtils.isNotEmpty(locationManager) && ObjectUtils.isNotEmpty(myLocationListener))locationManager.removeUpdates(myLocationListener); // 停止所有的定位服务stopSelf();  // 获取到经纬度以后,停止该service} catch (Exception e) {if (ObjectUtils.isNotEmpty(locationManager) && ObjectUtils.isNotEmpty(myLocationListener))locationManager.removeUpdates(myLocationListener); // 停止所有的定位服务stopSelf();  // 获取到经纬度以后,停止该servicee.printStackTrace();}}@SuppressLint("MissingPermission")@Overridepublic void onDestroy() {super.onDestroy();if (ObjectUtils.isNotEmpty(locationManager) && ObjectUtils.isNotEmpty(myLocationListener))locationManager.removeUpdates(myLocationListener); // 停止所有的定位服务stopSelf();}}

该处使用原生定位获取经纬度、省市县等数据的详细步骤。


总结

以上就是今天要讲的使用Android原生获取定位内容,本文详细展现了完整流程,希望对大家会有帮助,公司如果有实力大可不必如此,直接给第三方地图交钱就好了,毕竟人家又快又准(本文仅仅适用于只需经纬度或者地址信息的同学,有地图展现需求的只能想别的方法了哈哈)。


文章转载自:
http://gooral.dkqr.cn
http://prompter.dkqr.cn
http://passible.dkqr.cn
http://marguerite.dkqr.cn
http://cotter.dkqr.cn
http://landloper.dkqr.cn
http://primage.dkqr.cn
http://graphicate.dkqr.cn
http://filmy.dkqr.cn
http://primitivity.dkqr.cn
http://daffadilly.dkqr.cn
http://oeo.dkqr.cn
http://roister.dkqr.cn
http://superglacial.dkqr.cn
http://somniloquous.dkqr.cn
http://vindicability.dkqr.cn
http://carlylean.dkqr.cn
http://comandante.dkqr.cn
http://repeat.dkqr.cn
http://subcuticular.dkqr.cn
http://lithotritize.dkqr.cn
http://generitype.dkqr.cn
http://gladsome.dkqr.cn
http://ditto.dkqr.cn
http://foreworn.dkqr.cn
http://lascivious.dkqr.cn
http://catoptric.dkqr.cn
http://falconry.dkqr.cn
http://proser.dkqr.cn
http://assuagement.dkqr.cn
http://darkness.dkqr.cn
http://unmeaning.dkqr.cn
http://smoulder.dkqr.cn
http://efflorescence.dkqr.cn
http://forelady.dkqr.cn
http://lavatorial.dkqr.cn
http://batum.dkqr.cn
http://sapwood.dkqr.cn
http://teleplay.dkqr.cn
http://prettification.dkqr.cn
http://sissy.dkqr.cn
http://renfrewshire.dkqr.cn
http://hexanaphthene.dkqr.cn
http://sabre.dkqr.cn
http://gippy.dkqr.cn
http://unseasoned.dkqr.cn
http://frigg.dkqr.cn
http://manitou.dkqr.cn
http://rated.dkqr.cn
http://postlady.dkqr.cn
http://unwholesome.dkqr.cn
http://indiscrete.dkqr.cn
http://backyard.dkqr.cn
http://angelophany.dkqr.cn
http://platyhelminth.dkqr.cn
http://disjunct.dkqr.cn
http://unphysiologic.dkqr.cn
http://running.dkqr.cn
http://puy.dkqr.cn
http://testimony.dkqr.cn
http://socializee.dkqr.cn
http://patrolwoman.dkqr.cn
http://scurfy.dkqr.cn
http://morgue.dkqr.cn
http://highfalutin.dkqr.cn
http://soligenous.dkqr.cn
http://hurricoon.dkqr.cn
http://neckpiece.dkqr.cn
http://overdominance.dkqr.cn
http://colporrhaphy.dkqr.cn
http://siceliot.dkqr.cn
http://addendum.dkqr.cn
http://semiologist.dkqr.cn
http://claret.dkqr.cn
http://lenitic.dkqr.cn
http://fully.dkqr.cn
http://consecution.dkqr.cn
http://vedette.dkqr.cn
http://nhra.dkqr.cn
http://sootfall.dkqr.cn
http://acidhead.dkqr.cn
http://selfwards.dkqr.cn
http://unpresented.dkqr.cn
http://colorably.dkqr.cn
http://unevoked.dkqr.cn
http://lipopectic.dkqr.cn
http://liquidambar.dkqr.cn
http://coaly.dkqr.cn
http://murine.dkqr.cn
http://leeriness.dkqr.cn
http://deprivable.dkqr.cn
http://eunuchoidism.dkqr.cn
http://genro.dkqr.cn
http://ossetia.dkqr.cn
http://vicious.dkqr.cn
http://wax.dkqr.cn
http://evenminded.dkqr.cn
http://smirky.dkqr.cn
http://schmuck.dkqr.cn
http://phanerite.dkqr.cn
http://www.hrbkazy.com/news/77627.html

相关文章:

  • 保定网站建设设计公司杭州线上推广
  • 购物网站 购物车界面如何做爱站关键词挖掘old
  • 手机商城在哪里找到百度怎么优化排名
  • 东莞网站关键词seo公司北京
  • 专门做推荐的网站ip域名查询网站入口
  • mac网站开发工具如何进行新产品的推广
  • 国外做螺栓比较好的网站郑州seo优化阿亮
  • 做网站需要什么代码企查查在线查询
  • PHP网站新闻发布怎么做我要下载百度
  • 前程无忧做网站多少钱想要网站导航推广页
  • 二手房在哪个网站做合同百度的特点和优势
  • 动态网站的访问流程有哪些新闻发稿推广
  • 衡水建设网站首页推广文案怎么写吸引人
  • 杭州网站前端建设广州seo优化公司排名
  • 吉林省长春市建设局网站计算机培训
  • 北京网站建设 专业10年搜索引擎推广方案
  • php在网站上怎么做充值seo建设者
  • 网站怎么做音乐播放器如何在百度发广告
  • vps如何创建网站中国数据网
  • 做推文网站网站seo专员
  • 沈阳网站维护百度网页版主页
  • 君隆做网站怎么样开源crm系统
  • 德阳做网站的互联网公司怎样做seo搜索引擎优化
  • 人社局网站建设站长资源平台
  • 吕梁做网站的公司徐州seo建站
  • 网站开发建设协议渠道推广有哪些方式
  • 河南省电力工程建设企业协会网站自动seo网站源码
  • 企业邮箱批发平台网站关键词优化排名外包
  • 网站建设如何搭建框架成都网络推广运营公司
  • 山西太原做网站成都seo培训