Find Coffee Shops in New York City

๐Ÿ”‘

Do You Have Your Access Token?

Before you proceed, ensure you have your access token at hand. Can't find your token? No worries โ€” our guide will assist you in either creating a new one or finding your existing token.

Step 1: Retrieve Coordinates using Geocoding API

Suppose you intend to locate nearby coffee shops in New York City, using Times Square as a reference point. To acquire the latitude and longitude coordinates for Times Square, employ the Geocoding API as follows:

https://us1.locationiq.com/v1/search?key=YOUR_ACCESS_TOKEN&format=json&q=Times%20Square,%20New%20York,%20USa

The response will include Times Square's coordinates:

{
    "place_id": "348475193",
    "licence": "https://locationiq.com/attribution",
    "osm_type": "relation",
    "osm_id": "14942838",
    "boundingbox": [
        "40.7558313",
        "40.7591362",
        "-73.9870666",
        "-73.9845108"
    ],
    "lat": "40.75700945",
    "lon": "-73.98597241931907",
    "display_name": "Times Square, Times Square, Manhattan, New York County, New York, New York, 10036, USA",
    "class": "highway",
    "type": "pedestrian",
    "importance": 0.9439494846055839
}

Step 2: Use coordinates to request Nearby-POI Data

Following the retrieval of coordinates, you can proceed to send an API request to the Nearby-POI endpoint, specifying the coordinates, search radius (e.g., 2000 meters), and the desired POI type (e.g., coffee shops):

https://api.locationiq.com/v1/nearby?key=YOUR_ACCESS_TOKEN&lat=40.7566798&lon=-73.9869369&radius=2000&tag=cafe

Let's break down the components of this URL:

  • https://api.locationiq.com/v1/nearby: This is the base URL of LocationIQ's Nearby-POI API.

  • key=YOUR_ACCESS_TOKEN: Replace YOUR_ACCESS_TOKEN with the actual API Access Token you've obtained from LocationIQ. The API key is essential for authentication and authorization to make API requests.

  • lat=40.7566798: This parameter specifies the latitude coordinate of the central point around which you want to find nearby POIs.

  • lon=-73.9869369: This parameter specifies the longitude coordinate of the central point around which you want to find nearby POIs.

  • radius=2000: This parameter defines the radius (in meters) within which the API should search for nearby POIs. In this case, a radius of 2000 meters (2 kilometers) is specified.

  • tag=cafe: This parameter filters the search results to only include POIs of a specific type. Here, the type "cafe" is used, indicating that you're interested in finding nearby cafes.

When you send this API request, LocationIQ's Nearby-POI API will respond with a list of cafes located within a 2000-meter radius of the specified latitude and longitude coordinates. Each entry in the response will include details about each cafe, such as its name, address, latitude, longitude, and other relevant information.

This API call will return a list of nearby coffee shops, such as:

[
    {
        "place_id": "420615862231",
        "osm_type": "node",
        "osm_id": "1426275725",
        "lat": "40.7566921",
        "lon": "-73.9867194",
        "class": "amenity",
        "type": "cafe",
        "tag_type": "cafe",
        "name": "Europa Cafe",
        "display_name": "Europa Cafe, 3, Times Square, New York, New York, 10036, United States",
        "address": {
            "name": "Europa Cafe",
            "house_number": "3",
            "road": "Times Square",
            "city": "New York",
            "state": "New York",
            "postcode": "10036",
            "country": "United States",
            "country_code": "US"
        },
        "boundingbox": ["40.7566921", "40.7566921", "-73.9867194", "-73.9867194"],
        "distance": 18
    },
    {
        "place_id": "421425835742",
        "osm_type": "node",
        "osm_id": "1426250385",
        "lat": "40.7566173",
        "lon": "-73.9858991",
        "class": "amenity",
        "type": "cafe",
        "tag_type": "cafe",
        "name": "Starbucks",
        "display_name": "Starbucks, Times Square, New York, New York, 10036, United States",
        "address": {
            "name": "Starbucks",
            "road": "Times Square",
            "city": "New York",
            "state": "New York",
            "postcode": "10036",
            "country": "United States",
            "country_code": "US"
        },
        "boundingbox": ["40.7566173", "40.7566173", "-73.9858991", "-73.9858991"],
        "distance": 87
    },
    // ... (other entries)
]

Let's break down the key pieces of information in this output:

  • place_id: A unique identifier for the place in the LocationIQ database.

  • osm_type: The type of the OpenStreetMap (OSM) element associated with the place. In this case, it's a "node," which is a specific point on the map.

  • osm_id: The unique identifier of the OpenStreetMap element.

  • lat and lon: The latitude and longitude coordinates of the cafe's location. These coordinates pinpoint the exact geographic position of the cafe on the Earth's surface.

  • class: The classification of the place. In this case, it's classified as an "amenity," which generally refers to a place that provides a specific service or facility.

  • type: The specific type of amenity. Here, it's categorized as a "cafe," indicating that it's a coffee shop or similar establishment.

  • tag_type: Further specifies the type of the place, which, in this case, is also "cafe."

  • name: The name of the cafe, which is "Europa Cafe."

  • display_name: A human-readable representation of the cafe's location. It includes the name of the cafe, its address details (house number, road, city, state, postcode, country), and additional context (such as Times Square, New York City, and the United States).

  • address: A more structured breakdown of the cafe's address, including its name, house number, road, city, state, postcode, country, and country code.