Exception when using a google-map-marker without position
timeu opened this issue · comments
In a recent commit the initailization of the marker position was changed from: position: new google.maps.LatLng(this.latitude, this.longitude),
to position: {lat: this.latitude, lng: this.longitude},
.
This will cause a Assertion failed: InvalidValueError: setPosition: not a LatLng or LatLngLiteral: in property lat: not a number
when I add a marker without a position to a map:
<google-map latitude="39.91" map="{{map}}" id="mapelement" longitude="116.38" zoom="2" >
<google-map-marker id="accessionMarker" z-index="9999" ></google-map-marker>
</google-map>
@brendankenny, can you help fix? The change was your suggestion. Also, should we not allow
markers without positions?
I can't assign issues, but I'll take a look
So there are three solutions to this:
- default
latitude
andlongitude
to0
instead ofnull
. This will match the old behavior because thegoogle.maps.LatLng
constructor coercesnull
to0
, so markers with missing positions were located at0, 0
and would be displayed on the equator at the prime meridian. - add
parseFloat
toposition: {lat: this.latitude, lng: this.longitude}
in the Marker constructor, similar to what's done in_updatePosition
. This will coerce the defaultnull
values toNaN
which the Marker constructor will accept. Markers with missing positions will be located atNaN, NaN
, so will not be displayed, but no exception will be thrown. - slightly more complicated logic of not constructing a marker until a position is specified.
I think the second solution is arguably the best one. Defaulting to a position when there is no position specified doesn't make much sense, and the second solution would be equivalent to the third solution, but the Maps API would handle keeping the marker hidden until a valid position is specified.
Downside: Not giving a position can be a totally valid use case (if you specify the latitude and longitude later in a script, for instance), but it's probably more often due to an error, and the only real issue is that it's a silent error, with no feedback to the developer that something is wrong. It's easily seen in DevTools that the values are still null (especially since they're reflectToAttribute
), but developer mileage may vary.
PR for option 2 attached.
I was slightly wrong about debuggability: when the property is null
or undefined
reflectToAttribute
removes the attribute, so missing positions would mean no attributes, which will be much less of a signal than latitude="null" longitude="null"
.
Thanks for looking into this.
+1 for second solution.