Baidu Maps WeChat Mini Program JSAPI

repository·master·Indexed 19 days ago

https://github.com/baidumapapi/wxapp-jsapi

A frontend wrapper for Baidu Maps Web Service APIs designed for WeChat Mini Programs. It provides the BMapWX class to simplify POI searching, geocoding, reverse geocoding, and weather retrieval, returning data in formats compatible with the WeChat map component.

Tokens
3.3K
Snippets
14
Records
14
Agent score
17%

What's inside baidumapapi-wxapp-jsapi

  1. Perform POI search with search()

    master

    Use the search() method to perform a nearby POI (Point of Interest) search. This method returns results formatted specifically for the WeChat Mini Program map component's markers.

    map.search({
      location: '39.915,116.404', // Optional: defaults to current location
      iconPath: '/path/to/icon.png', // Required: marker icon path
      query: '美食', // Optional: search keyword
      success: (res) => {
        // res.wxMarkerData: Array of markers ready for WeChat map component
        // res.originalData: Original Place API response
        console.log(res.wxMarkerData);
      },
      fail: (err) => {
        console.error(err.errMsg, err.statusCode);
      }
    });
  2. Geocode addresses with geocoding()

    master

    The geocoding() method converts a text address (e.g., "北京市海淀区上地十街10号") into geographic coordinates.

    map.geocoding({
      address: '北京市海淀区上地十街10号', // Required: the address to parse
      iconPath: '/path/to/icon.png', // Required
      success: (res) => {
        // res.wxMarkerData: Array of markers for the address
        // res.originalData: Original Geocoding API response
        console.log(res.wxMarkerData);
      },
      fail: (err) => {
        console.error(err.errMsg, err.statusCode);
      }
    });
  3. Reverse geocode coordinates with regeocoding()

    master

    The regeocoding() method converts latitude and longitude coordinates into a human-readable geographic description (reverse geocoding).

    map.regeocoding({
      location: '39.915,116.404', // The coordinates to parse
      iconPath: '/path/to/icon.png', // Required
      success: (res) => {
        // res.wxMarkerData: Array of markers for the location
        // res.originalData: Original Geocoding API response
        console.log(res.wxMarkerData);
      },
      fail: (err) => {
        console.error(err.errMsg, err.statusCode);
      }
    });
  4. Use suggestion() for input autocomplete

    master

    The suggestion() method performs fuzzy matching and input completion based on user input, corresponding to the Place Suggestion API.

    map.suggestion({
      success: (res) => {
        // res.originalData: Place Suggestion API response
        console.log(res.originalData);
      },
      fail: (err) => {
        console.error(err.errMsg, err.statusCode);
      }
    });
  5. Reference: BMapWX Methods

    master

    The following methods are available on the BMapWX instance:

    | Method | Return Value | Description |
    | :--- | :--- | :--- |
    | `search(searchParam)` | none | Performs a search for nearby POI information |
    | `suggestion(suggestionParam)` | none | Performs fuzzy matching/input completion |
    | `regeocoding(regeocodingParam)` | none | Converts coordinates to geographic descriptions |
    | `geocoding(geocodingParam)` | none | Converts addresses to coordinates |
  6. Reference: geocodingParam Object Structure

    master

    Configuration object for the geocoding() method.

    | Property | Type | Required | Description |
    | :--- | :--- | :--- | :--- |
    | `address` | string | Yes | The address to be parsed (e.g., '北京市海淀区上地十街10号') |
    | `iconPath` | string | Yes | WeChat Mini Program marker icon path |
    | `iconTapPath` | string | No | Icon path after being tapped |
    | `width` | number | No | Marker width |
    | `height` | number | No | Marker height |
    | `alpha` | number | No | Marker transparency |
    | `success` | Function | No | Callback function receiving `geocodingSuccess` object |
    | `fail` | Function | No | Callback function receiving `geocodingFail` object |
  7. Reference: searchParam Object Structure

    master

    Configuration object for the search() method.

    | Property | Type | Required | Description |
    | :--- | :--- | :--- | :--- |
    | `location` | string | No | Latitude/Longitude (e.g., '39.915,116.404'). Defaults to current location |
    | `iconPath` | string | Yes | WeChat Mini Program marker icon path |
    | `iconTapPath` | string | No | Icon path after being tapped |
    | `width` | number | No | Marker width (defaults to image width) |
    | `height` | number | No | Marker height (defaults to image height) |
    | `alpha` | number | No | Marker transparency (defaults to 1) |
    | `query` | string | No | Search keyword (defaults to life services, food, hotel) |
    | `success` | Function | No | Callback function receiving `searchSuccess` object |
    | `fail` | Function | No | Callback function receiving `searchFail` object |
  8. Get search suggestions with suggestion()

    master

    The suggestion(param) method provides fuzzy search suggestions (autocomplete) based on a query string.

    Parameters:

    • query: The search string.
    • region: The geographic region (e.g., '全国').
    • city_limit: Boolean to limit results to a specific city.
    • success: Callback receiving the suggestion data.
    • fail: Callback for errors.
    bmap.suggestion({
      query: '天安门',
      success: (res) => {
        console.log('Suggestions:', res);
      },
      fail: (err) => console.error(err)
    });
  9. Perform Geocoding with geocoding()

    master

    The geocoding(param) method converts a physical address into geographic coordinates.

    Parameters:

    • address: The address string to convert (required).
    • city: The city name.
    • coordtype: The desired return coordinate type (defaults to 'gcj02ll').
    • success: Callback receiving the result and wxMarkerData containing latitude and longitude.
    • fail: Callback for errors.
    bmap.geocoding({
      address: '北京市天安门',
      success: (res) => {
        console.log('Coordinates:', res.wxMarkerData[0].latitude, res.wxMarkerData[0].longitude);
      },
      fail: (err) => console.error(err)
    });
  10. Perform Reverse Geocoding with regeocoding()

    master

    The regeocoding(param) method converts coordinates (latitude/longitude) into a human-readable address description.

    Parameters:

    • location: A string in 'latitude,longitude' format. If omitted, uses current WeChat location.
    • coordtype: Coordinate type (defaults to 'gcj02ll').
    • success: Callback receiving an object with originalData and wxMarkerData. The wxMarkerData contains address, desc, and business fields.
    • fail: Callback for errors.
    bmap.regeocoding({
      location: '39.9088,116.3975',
      success: (res) => {
        console.log('Address info:', res.wxMarkerData[0].address);
      },
      fail: (err) => console.error(err)
    });
  11. Perform POI Nearby Search with search()

    master

    The search(param) method performs a Place Description (POI) nearby search. If no location is provided in the parameters, the method automatically uses the WeChat getLocation interface to determine the user's current position.

    Parameters:

    • query: Search keywords (e.g., '美食'). Defaults to '生活服务$美食&酒店'.
    • location: A string in 'latitude,longitude' format. If omitted, uses current WeChat location.
    • radius: Search radius in meters. Defaults to 2000.
    • page_size: Number of results. Defaults to 10.
    • success: Callback function receiving an object with originalData (raw API response) and wxMarkerData (formatted for WeChat Mini Program markers).
    • fail: Callback function for errors.
    • iconPath, iconTapPath, width, height, alpha: Visual properties applied to the returned wxMarkerData items.
    bmap.search({
      query: '餐厅',
      radius: 5000,
      success: (res) => {
        console.log('Search results:', res.wxMarkerData);
        // res.wxMarkerData contains objects with id, latitude, longitude, title, etc.
      },
      fail: (err) => console.error(err)
    });