android GoogleMap自动完成返回空的地点

x8diyxa7  于 2022-12-21  发布在  Android
关注(0)|答案(1)|浏览(201)

我试图从谷歌Map的自动完成获取一个地方,然后使用它的'longitudelatitude,但每次我得到一个地方与正确的名称和所有其他属性得到了null在他们。有人知道如何修复吗?一个地方的例子,我得到:
I/系统输出:地点{地址=空,地址组件=空,业务状态=空,属性=[],ID=ChIJOwg_06VPwokRYv534QaPC8g,latLng=空,名称=纽约,营业时间=空,电话号码=空,照片元数据=空,加号代码=空,价格级别=空,评级=空,类型=空,用户评级总计=空,utcOffsetMinutes=空,视口=空,网站Uri =空}
代码[问题在“setTargetPlace”(方法在页面下方)]:

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    if (requestCode == AUTOCOMPLETE_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            Place newTargetPlace = Autocomplete.getPlaceFromIntent(data);
            System.out.println(newTargetPlace);
            dataCenter.setTargetPlace(newTargetPlace);
            dataCenter.createParty();
            String response = dataCenter.getNextResponse();
            System.out.println(response);
            String[] commandAndPara = dataCenter.getCommandFromMsg(response);
            String command = commandAndPara[0];
            String para = commandAndPara[1];
            if(command.equals(DataCenter.MSG_OK)) {
                dataCenter.setPartyCode(para);
                moveToParty();
            }
            else {
                if(command.equals(DataCenter.MSG_FULL)) {
                    System.out.println("FULL");
                }
                else { //ERROR
                    System.out.println("ERROR");
                }
            }
        } else if (resultCode == AutocompleteActivity.RESULT_ERROR) {
            // TODO: Handle the error.
            Status status = Autocomplete.getStatusFromIntent(data);
            Log.i(TAG, status.getStatusMessage());
        } else if (resultCode == RESULT_CANCELED) {
            // The user canceled the operation.
        }
        return;
    }
    super.onActivityResult(requestCode, resultCode, data);
}

方法:

public void setTargetPlace(Place targetPlace) {
    this.targetPlace = targetPlace;
    this.target = this.targetPlace.getName();
    LatLng targetCords = this.targetPlace.getLatLng();
    this.targetX = targetCords.longitude;
    this.targetY = targetCords.latitude;
}
eulz3vhy

eulz3vhy1#

纬度和经度为空的原因是您没有将LAT_LNG作为要检索的字段的一部分。请参阅下面的代码片段,了解如何将其包含在内

val fields = listOf(Place.Field.ID, Place.Field.NAME, Place.Field.LAT_LNG)

因此,当您创建Intent并传递field变量,然后像这样启动它时

val intent = Autocomplete.IntentBuilder(AutocompleteActivityMode.FULLSCREEN, fields)
                .build(requireContext())
            startActivityForResult(intent, AUTOCOMPLETE_REQUEST_CODE)

它将返回onActivityResult()中的纬度和经度

相关问题