HMS Core定位服务在生活服务类App中可以自动填写收货地址啦

Posted 华为移动服务

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HMS Core定位服务在生活服务类App中可以自动填写收货地址啦相关的知识,希望对你有一定的参考价值。

在涉及团购、外卖、快递、家政、物流、搬家等生活服务类的App、小程序中,填写收货地址是用户高频使用的功能。这一功能通常采取让用户手动填写的解决方案,例如上下拉动选择浙江省–>杭州市–>西湖区–>西溪街道,再切换到姓名输入框输入姓名–>电话输入框输入电话等一系列的操作。从中我们不难发现手动输入地址不仅费时费力,而且一不小心还会出现选错地址的现象。

那有没有什么方法能帮助用户又快又准确的填写地址呢?借助HMS Core 定位服务的融合定位+地理编码能力,生活服务类App可以自动定位获取用户当前位置信息或某地的行政区划信息、街道信息,并准确填写至地址栏中。用户无需自己手动输入,减少了操作时间,并且不用再担心填错地址。以下是我们提供的示例代码,开发者们按照步骤尝试,就可以轻松体验这个功能啦~

效果展示

开发实战

开发步骤

一、 开发准备

1. 登录AppGallery Connect网站,点击“我的项目”。找到您的项目在API管理开启location开关,在项目中点击需要配置签名证书指纹的应用。在“项目设置 > 常规”页面的“应用”区域,点击“SHA256证书指纹”后的“添加证书指纹”,输入生成的SHA256指纹。

2. 在“项目设置 > 常规”页面的“应用”区域,点击“agconnect-services.json”下载配置文件。将“agconnect-services.json”文件拷贝到应用级根目录下。

3. 项目级“build.gradle”文件

buildscript 
    repositories 
        google()
        jcenter()
        maven  url 'https://developer.huawei.com/repo/' 
        mavenCentral()
    
    dependencies 
        classpath 'com.android.tools.build:gradle:4.1.2'
        classpath 'com.huawei.agconnect:agcp:1.6.0.300'
    


allprojects 
    repositories 
        maven  url 'https://developer.huawei.com/repo/' 
        google()
        jcenter()
        mavenCentral()
    

应用级的“build.gradle”文件

plugins 
    id 'com.android.application'
    id 'com.huawei.agconnect'

在“dependencies ”中添加如下编译依赖

implementation 'com.huawei.hms:location:6.3.0.300'

二、 权限检查

1. 在“AndroidManifest.xml”文件中配置权限ACCESS_COARSE_LOCATION(粗略的位置权限),ACCESS_FINE_LOCATION(精确的位置权限)和ACCESS_BACKGROUND_LOCATION权限

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />

2. 动态申请定位相关权限(Android 6.0及以上版本危险权限要求)

    Log.i(TAG, "android sdk < 28 Q");
    if (ActivityCompat.checkSelfPermission(this,
            Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
            && ActivityCompat.checkSelfPermission(this,
            Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) 
        String[] strings =
                Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION;
        ActivityCompat.requestPermissions(this, strings, 1);
    
 else 
    if (ActivityCompat.checkSelfPermission(this,
            Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
            && ActivityCompat.checkSelfPermission(this,
            Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED
            && ActivityCompat.checkSelfPermission(this,
            "android.permission.ACCESS_BACKGROUND_LOCATION") != PackageManager.PERMISSION_GRANTED) 
        String[] strings = Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.ACCESS_COARSE_LOCATION,
                "android.permission.ACCESS_BACKGROUND_LOCATION";
        ActivityCompat.requestPermissions(this, strings, 2);
    

三、 获取定位结果

1. 设置定位参数,包括位置更新的间隔,定位类型等

mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
mSettingsClient = LocationServices.getSettingsClient(this);
mLocationRequest = new LocationRequest();
// Sets the interval for location update (unit: Millisecond)
mLocationRequest.setInterval(5000);
// Sets the priority
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

2. 调用getSettingsClient()接口获取SettingsClient实例。调用调用checkLocationSettings()检查设备开关设置

LocationSettingsRequest locationSettingsRequest = builder.build();
// Before requesting location update, invoke checkLocationSettings to check device settings.
Task<LocationSettingsResponse> locationSettingsResponseTask =
        mSettingsClient.checkLocationSettings(locationSettingsRequest);

检查开关开启之后 ,调用requestLocationUpdates()进行定位

locationSettingsResponseTask.addOnSuccessListener(new OnSuccessListener<LocationSettingsResponse>() 
    @Override
    public void onSuccess(LocationSettingsResponse locationSettingsResponse) 
        Log.i(TAG, "check location settings success");
        mFusedLocationProviderClient
                .requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.getMainLooper())
                .addOnSuccessListener(new OnSuccessListener<Void>() 
                    @Override
                    public void onSuccess(Void aVoid) 
                        Log.i(TAG, "requestLocationUpdatesWithCallback onSuccess");
                    
                )
                .addOnFailureListener(new OnFailureListener() 
                    @Override
                    public void onFailure(Exception e) 
                        Log.e(TAG, "requestLocationUpdatesWithCallback onFailure:" + e.getMessage());
                    
                );
    
).addOnFailureListener(new OnFailureListener() 
    @Override
    public void onFailure(Exception e) 
        Log.e(TAG, "checkLocationSetting onFailure:" + e.getMessage());
        int statusCode = 0;
        if (e instanceof ApiException) 
            statusCode = ((ApiException) e).getStatusCode();
        
        switch (statusCode) 
            case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                try 
                    // When the startResolutionForResult is invoked, a dialog box is displayed, asking you
                    // to open the corresponding permission.
                    if (e instanceof ResolvableApiException) 
                        ResolvableApiException rae = (ResolvableApiException) e;
                        rae.startResolutionForResult(MainActivity.this, 0);
                    
                 catch (IntentSender.SendIntentException sie) 
                    Log.e(TAG, "PendingIntent unable to execute request.");
                
                break;
            default:
                break;
        
    
);

四、 逆地理编码获取当前位置

成功获取定位信息的经纬度之后,将经纬度信息传递给使用地理编码服务(GeocoderService)获得地理编码请求对象,然后调用请求逆地理编码方法(getFromLocation),设置请求(GetFromLocationRequest)参数,获取逆地理编码信息回调。

if (null == mLocationCallback) 
    mLocationCallback = new LocationCallback() 
        @Override
        public void onLocationResult(LocationResult locationResult) 
            if (locationResult != null) 
                List<Location> locations = locationResult.getLocations();
                if (!locations.isEmpty()) 
                    ExecutorUtil.getInstance().execute(new Runnable() 
                        @Override
                        public void run() 
                            Locale locale = new Locale("zh", "CN");
                            GeocoderService geocoderService = LocationServices.getGeocoderService(MainActivity.this, locale);
                            GetFromLocationRequest getFromLocationRequest = new GetFromLocationRequest(locations.get(0).getLatitude(), locations.get(0).getLongitude(), 1);
                            geocoderService.getFromLocation(getFromLocationRequest)
                                    .addOnSuccessListener(new OnSuccessListener<List<HWLocation>>() 
                                        @Override
                                        public void onSuccess(List<HWLocation> hwLocation) 
                                            printGeocoderResult(hwLocation);
                                        
                                    )
                                    .addOnFailureListener(new OnFailureListener() 
                                        @Override
                                        public void onFailure(Exception e) 
                                            Log.i(TAG, e.getMessage());
                                        
                                    );
                        
                    );
                
            
        
        @Override
        public void onLocationAvailability(LocationAvailability locationAvailability) 
            if (locationAvailability != null) 
                boolean flag = locationAvailability.isLocationAvailable();
                Log.i(TAG, "onLocationAvailability isLocationAvailable:" + flag);
            
        
    ;

最后将获取到的结果显示在界面上即可。

了解更多详情>>

访问华为开发者联盟官网
获取开发指导文档
华为移动服务开源仓库地址:GitHubGitee

关注我们,第一时间了解 HMS Core 最新技术资讯~

以上是关于HMS Core定位服务在生活服务类App中可以自动填写收货地址啦的主要内容,如果未能解决你的问题,请参考以下文章

如何用HMS Core位置和地图服务实现附近地点路径规划功能

HMS Core Keyring携手航班管家和高铁管家,打造美好出行体验

HMS Core Keyring携手航班管家和高铁管家,打造美好出行体验

HMS Core Keyring携手航班管家和高铁管家,打造美好出行体验

用HMS Core地图服务自定义地图样式,给你的应用制作专属个性化地图

携手HMS Core统一扫码服务, 兴业证券优理宝App提升用户扫码体验