Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jccartwright/6b7311809a149117b39c7895fd1ec53b to your computer and use it in GitHub Desktop.
Save jccartwright/6b7311809a149117b39c7895fd1ec53b to your computer and use it in GitHub Desktop.
// sample JSON response
var jsonResponse = {"spatialReference":{"wkid":4326,"latestWkid":4326},"candidates":[{"address":"80305, Boulder, Colorado","location":{"x":-105.24280489999995,"y":39.983092300000067},"score":100,"attributes":{},"extent":{"xmin":-105.28580489999996,"ymin":39.940092300000067,"xmax":-105.19980489999995,"ymax":40.026092300000066}},{"address":"080305","location":{"x":70.684671556000069,"y":42.633023284000046},"score":100,"attributes":{},"extent":{"xmin":70.671671556000064,"ymin":42.620023284000048,"xmax":70.697671556000074,"ymax":42.646023284000044}},{"address":"80305","location":{"x":-107.45293038699998,"y":24.909200000000055},"score":100,"attributes":{},"extent":{"xmin":-107.88393038699998,"ymin":24.478200000000054,"xmax":-107.02193038699998,"ymax":25.340200000000056}},{"address":"080305","location":{"x":25.969188087000077,"y":43.900496861000079},"score":100,"attributes":{},"extent":{"xmin":25.968188087000076,"ymin":43.899496861000081,"xmax":25.970188087000079,"ymax":43.901496861000076}},{"address":"80305","location":{"x":23.74637722600005,"y":50.135623723000037},"score":100,"attributes":{},"extent":{"xmin":23.728377226000049,"ymin":50.117623723000037,"xmax":23.76437722600005,"ymax":50.153623723000038}},{"address":"80-305","location":{"x":18.55720020800004,"y":54.408705009000073},"score":100,"attributes":{},"extent":{"xmin":18.556200208000039,"ymin":54.407705009000075,"xmax":18.558200208000041,"ymax":54.409705009000071}},{"address":"803","location":{"x":120.28383000000008,"y":22.626259687000072},"score":98,"attributes":{},"extent":{"xmin":120.27383000000007,"ymin":22.61625968700007,"xmax":120.29383000000009,"ymax":22.636259687000074}}]};
//return Observable with single element containing the candidates array
geocode(location: string): Observable {
const url = `${this.geocodeUrl}?f=json&SingleLine=${location}`;
return (this.http.get(url)
.pipe(
pluck('candidates'),
tap(data => console.log('data: ', data)),
catchError(this.handleError('geocode', null))
)
);
}
//return Observable with element for each candidate
geocode(location: string): Observable<AddressCandidate[]> {
const url = `${this.geocodeUrl}?f=json&SingleLine=${location}`;
return (this.http.get(url)
.pipe(
switchMap(x => from(x['candidates'])),
tap(data => console.log('data: ', data)),
catchError(this.handleError('geocode', null))
)
);
}
//return Observable with element for each candidate's location
geocode(location: string): Observable<AddressCandidate[]> {
const url = `${this.geocodeUrl}?f=json&SingleLine=${location}`;
return (this.http.get(url)
.pipe(
switchMap(x => from(x['candidates'])),
pluck('location'),
tap(data => console.log('data: ', data)),
catchError(this.handleError('geocode', null))
)
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment