mapsplugin / cordova-plugin-googlemaps

Google Maps plugin for Cordova
Apache License 2.0
1.66k stars 913 forks source link

Error in `Map.prototype.addMarker` when using a base64 string as the icon #2880

Open CodeWithOz opened 2 years ago

CodeWithOz commented 2 years ago

I'm submitting a ... (check one with "x")

OS: (check one with "x")

cordova information: (run $> cordova plugin list)

N/A

If you use @ionic-native/google-maps, please tell the package.json (only @ionic-native/core and @ionic-native/google-maps are fine mostly)

@ionic-native/core : N/A
@ionic-native/google-maps : N/A

Current behavior:

If the icon of a marker is a base64 string of an image, the callback provided to addMarker is never executed, like this:

map.addMarker({
  icon: 'data:image/png;base64,aVZ...',
}, outcome => {
  console.log(outcome); // this code never executes
});

I traced down the problem to these lines of code, which is where an error occurs: Cannot read property 'width' of undefined. The problem is that markerOptions.icon.size will always be undefined if markerOptions.icon is a string. The solution would be to add an if statement that will not execute those lines of code if the icon is a string. This is what I did and it works for me: map-marker-bug

Also notice that I added a similar if statement for infoWindowAnchor:

markerOptions.infoWindowAnchor = [(isBase64Marker ? result.width : markerOptions.icon.size.width) / 2, 0];

because that logic also relies on markerOptions.icon.size.

Expected behavior:

Invoking .addMarker() with a base64 string as the icon should not trigger an error, and the callback should be executed.

Screen capture or video record:

Related code, data or error log (please format your code or data):

N/A, the problem is in the logic as described above.

Support this plugin activity

I appreicate if you give me a beer :beer: from here