question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

It looks like the component is discarding the placeID instead of using it to retrieve the coordinates via geocode.

I assume that addresses are usually 1:1 with geocode when taken directly from Places results, but would it be safer to geocode on placeID instead?

At the very least, it’d be helpful if placeID was returned along with suggestion

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:4
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
brandonmpcommented, Feb 1, 2017

i edited it for my own local use (sorry, was in a hurry so didn’t wanna mess with a PR/tests etc)

it was a pretty basic change. here are the parts of the main component I changed.

// add fullPrediction to the new state set by the autocomplete callback
  autocompleteCallback(predictions, status) {
    if (status !== this.autocompleteOK) {
      this.props.onError(status)
      if (this.props.clearItemsOnError) { this.clearAutocomplete() }
      return
    }
    this.setState({
      autocompleteItems: predictions.map((p, idx) => ({
        suggestion: p.description,
        fullPrediction: p,
        placeId: p.place_id,
        active: false,
        index: idx
      }))
    })
  }

// change a few handlers to take the fullPrediction instead of 'address'
selectAddress(fullPrediction) {
    this.clearAutocomplete()
    this._handleSelect(fullPrediction)
  }

  _handleSelect(fullPrediction) {
    this.props.onSelect ? this.props.onSelect(fullPrediction) : this.props.onChange(fullPrediction)
  }


// in the render function, change the data passed to selectAddress 
<div
            key={p.placeId}
            onMouseOver={() => this._setActiveItemAtIndex(p.index)}
            onClick={() => this.selectAddress(p.fullPrediction)}
            style={{
              ...defaultStyles.autocompleteItem,
              ...styles.autocompleteItem,
              ...this.autocompleteItemStyle(p.active)
            }}>
            {this.props.autocompleteItem({ suggestion: p.suggestion })}
          </div>

I also changed the geocode-by-address function to take a placeid instead of an address (& return a promise b/c it’s 2017 😛 ). I needed specific fields off of the return value, but for general purpose i think it’s fine to return just the lat, long, and entire geocode results if a user wants to peel-off their own fields. here’s what i came up with


// @flow
import _ from 'lodash'

type GeocodeResults = {
  latitude: ?number,
  longitude: ?number,
  formattedAddress: ?string,
  googlePlaceID: ?string
};


export const geocodeByAddress = ({ placeId }: { placeId: string }): GeocodeResults => {
  const geocoder = new google.maps.Geocoder()
  const OK = google.maps.GeocoderStatus.OK
  return new Promise((resolve, reject) => {
    geocoder.geocode({ placeId }, (results, status) => {
      if (status !== OK) {
        reject(status)
      }
      resolve(makePlace(results))
    })
  })
}

const makePlace = (geocodeResults) => {
  const address = geocodeResults[0]
  const place = {}
  place.latitude = _.get(address, 'geometry.location.lat', () => null)()
  place.longitude = _.get(address, 'geometry.location.lng', () => null)()
  place.formattedAddress = address.formatted_address || null
  place.googlePlaceID = address.place_id || null
  return place
}

1reaction
brandonmpcommented, Feb 1, 2017

@kenny-hibino maybe i can try but we’re trying to launch something in a few weeks so not sure 😕 your lib is the best implementaiton of autocomplete i’ve seen though (primarily b/c it lets u use whatever input component you want), so happy to help if i can

Read more comments on GitHub >

github_iconTop Results From Across the Web

Place IDs | Places API - Google Developers
Retrieving an address for a Place ID in the Geocoding API web service and ... One strategy is to store the original request...
Read more >
Using place IDs - Amazon Location Service
The PlaceId returned in calls that you make is specific to your AWS account, to the AWS Region, and to the data provider...
Read more >
Google Maps API Geocoder doesn't return location by placeId
I think the autocomplete endpoint is not returning valid placeids as of today or yesterday. I'm running into a similar problem.
Read more >
com.google.maps.PlaceDetailsRequest.placeId java code ...
Specifies the field masks of the details to be returned by PlaceDetails. language. Popular in Java. Finding current android device location ...
Read more >
How to Use Places | Trimble Maps RESTful APIs
If a match is found, the geocoding API will return basic details about the Place, ... API using the trimblePlaceId you have just...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found