Conversation
| self.gridsetting = [slat, elat, latin, slng, elng, lngin] | ||
|
|
||
| def marker(self, lat, lng, color='#FF0000', c=None, title="no implementation"): | ||
| def marker(self, lat, lng, color='#FF0000', c=None, title="no implementation", info_window_html=""): |
There was a problem hiding this comment.
Let's rename info_window_html to info_window for brevity.
Let's also have its default value be None instead of "" to avoid mutable default values.
| f.write('\t\tposition: latlng\n') | ||
| f.write('\t\t});\n') | ||
| f.write('\t\tmarker.setMap(map);\n') | ||
| f.write('\t\tmarkers.push(new google.maps.Marker({\n') |
There was a problem hiding this comment.
Instead of pushing all markers to some JavaScript list, I'd recommend giving only those markers that need an InfoWindow a unique name. So something like:
var marker_0 = new google.maps.Marker({ ...And any markers that don't need an InfoWindow can be left alone:
new google.maps.Marker({ ...| mymap.marker(37.429, -122.144, "k") | ||
| lat, lng = mymap.geocode("Stanford University") | ||
| mymap.marker(lat, lng, "red") | ||
| mymap.marker(lat, lng, "red", "<a href='https://www.calvin.edu'>Go to Calvin University</a>") |
There was a problem hiding this comment.
Would this work? The 4th argument is c.
| print("info_window_html transformed to ", info_window_html) | ||
| f.write("\t\t\t\tcontent: '%s',\n" % info_window_html) | ||
| f.write("\t\t\t});\n") | ||
| f.write("\t\t\tinfo_window.open(map, markers[%d]);\n" % self.numMarkers) |
There was a problem hiding this comment.
We can actually avoid naming info_window:
new google.maps.InfoWindow({
content: <string>
}).open(map, <marker>);There was a problem hiding this comment.
Actually, scratch that - we shouldn't be creating a new InfoWindow inside the onClick handler, otherwise each click will create a new InfoWindow on top of the previous one.
We'll have to create the InfoWindow once, give it a unique name (like we do for Markers), then reference it in the onClick handler.
| f.write('\t\t\tposition: latlng\n') | ||
| f.write('\t\t}));\n') | ||
| if info_window_html != "": | ||
| f.write("\t\tgoogle.maps.event.addListener(markers[%d], 'click', function () {\n" % self.numMarkers) |
There was a problem hiding this comment.
Instead of google.maps.event.addListener(), we can call addListener() on the marker directly:
<marker>.addListener('click', function () {
...
});| f.write("\t\t\tinfo_window = new google.maps.InfoWindow({\n") | ||
| # Escape every single quote and every newline in info_window_html. | ||
| info_window_html = info_window_html.replace("'", "\\'").replace("\n", "\\n") | ||
| print("info_window_html transformed to ", info_window_html) |
There was a problem hiding this comment.
We might not need this print statement here. If you think it's worth having, then perhaps we can log it using logging.debug to stop it from cluttering the output window if users don't want to see it.
|
|
||
| class GoogleMapPlotter(object): | ||
|
|
||
| numMarkers = 0 |
There was a problem hiding this comment.
Can we define this closer to where it's used? If the same GoogleMapPlotter() object is used to draw twice, then numMarkers will be twice what it should be (this is just one example).
I'd recommend we add an id parameter to write_point(), then write_points() would define the count locally and pass the next marker ID to write_point(). If no ID is passed in (i.e. id = None), then write_point() would simply not give the rendered marker a name (as in var marker_2 = ...).
b86c79b to
980d04c
Compare
Add support for the user to create an InfoWindow for each marker. The InfoWindow can contain HTML, including images and links, lists, etc.