Skip to content

Google Maps: Add custom data for markers #106

@mandrasch

Description

@mandrasch

Feature Request

Plugin

Google Maps

Description

Often devs need to add custom data (a specific database entry ID, etc.) to markers and receive them when the marker is clicked. E.g. an entry id to load more details from an API for the clicked marker dynamically.

This stackoverflow reply suggests three ways for adding customInfo to markers:

var markerA = new google.maps.Marker({
    map: map,
    position: new google.maps.LatLng(0, 0),
    customInfo: "Marker A"
});

var markerB = new google.maps.Marker({
    map: map,
    position: new google.maps.LatLng(-10, 0)
});
markerB.customInfo = "Marker B";

var markerC = new google.maps.Marker({
    map: map,
    position: new google.maps.LatLng(-20, 0)
});
markerC['customInfo'] = "Marker C";

Platform(s)

All

Preferred Solution

Add option like customData:{}which can be appended to map.addMarker().

When the marker is clicked, it will be transmitted here:

  async setMarkerListeners(
    mapId: string,
    markerId: string,
    marker: google.maps.Marker,
  ): Promise<void> {
    marker.addListener('click', () => {
      this.notifyListeners('onMarkerClick', {
        ...
        title: marker.getTitle(),
        customData: marker.getCustomData()
      });
    });
  }

https://github.com/ionic-team/capacitor-plugins/blob/main/google-maps/src/web.ts#L319

I'm not a typescript pro yet, guess there need to be some modifications on other files as well.

Alternatives

Storing marker IDs and connect them with custom data in JS (marker manager).

Additional Context

Related to ionic-team/capacitor-plugins#1133

Thx very much in advance for looking into this!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions