0

I'm trying to get a Place from the Autocomplete of Google maps and then using its' longitude and latitude but every time I get a Place with the right name and all the other attributes got null in them. Does anyone know how to fix that? Example of a Place I get:

I/System.out: Place{address=null, addressComponents=null, businessStatus=null, attributions=[], id=ChIJOwg_06VPwokRYv534QaPC8g, latLng=null, name=New York, openingHours=null, phoneNumber=null, photoMetadatas=null, plusCode=null, priceLevel=null, rating=null, types=null, userRatingsTotal=null, utcOffsetMinutes=null, viewport=null, websiteUri=null}

The code [the problem is at "setTargetPlace" (method is down the page)]:

@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);
}

The method:

    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;
}
Ab Cd
  • 1
  • Maybe https://stackoverflow.com/questions/56321208/how-to-get-city-and-state-name-in-autocomplete-places-places-api or https://stackoverflow.com/questions/55428111/places-getlatlng-returning-null-though-places-getname-is-not-returning-null? – CoolMind May 14 '21 at 10:42
  • 1
    Well it's close but in that case he got the LatLng and in my case I'm getting a null and not LatLng object. – Ab Cd May 14 '21 at 10:46

1 Answers1

0

The reason why you are getting the latitude and longitude as null is because you didn't include LAT_LNG as part of the fields you want to retrieve. See the code snippet below to see how you can include it

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

So when you create the intent and pass the field variable, and then go ahead to launch it like this

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

it will return with the latitude and longitude in onActivityResult()

Sirqoharastus
  • 11
  • 1
  • 1