Google maps script is not loading all markers

gmorales
Kilo Expert

Hi everyone I'm having the following issue, I'm currently creating a Google Map Page in Service Now, the script that I'm currently using is similar to the one at the wiki page (http://wiki.servicenow.com/index.php?title=Using_Map_Pages) I just changed the query in order to find open incidents from a singular user, the Issue is that "counts" are displaying in the correct cities or areas on the map, but there are only 1 marker , if you click on one of the numbers the Google maps,the info windows shows up in the same place, it overlaps with previous info windows, please see my attached image to understand this behavior.(try to zoom it , and you will see 2 info windows)

var uri = gs.getProperty("glide.servlet.uri");

var count = new GlideAggregate('incident');

count.addQuery('state', '1');

count.addQuery('user_id', '=', 'ITOC.admin');

count.addAggregate('COUNT', 'location');

count.query();

while (count.next()) {

      var loc = count.location;

      var locCount = count.getAggregate('COUNT', 'location');

      if (locCount > 0) {

              var item = map.addItem(count);

              item.latitude = loc.latitude;

              item.longitude = loc.longitude;

              item.marker_label = locCount;

              item.label_offset_left = -4;

              item.label_offset_top = -20;

              var link = 'href=' + uri + 'incident_list.do?sysparm_query=active%3Dtrue^location%3D' + loc;      

              item.html = '<a ' + link + '>' + loc.getDisplayValue() + ' (' + locCount + ')</a>';

              item.icon = "images/red_marker.png";

              item.icon_width = 24;

              item.icon_height = 24;

      }

}

This is how actually is being displayed...

ServiceNow Development.png

This is what I was expecting.. .

Capture.PNG

1 ACCEPTED SOLUTION

This is a new issue and related but different:


PRB607063 :Google maps API 3.14 deprecated - Only one marker is placed on map


This affects all versions of the platform.


(Note: This is a separate issue from PRB597310 / KB0538622 which exhibits the same symptom)



Workaround:


------------------


- Navigate to Table cmn_map_page > Open the record you want.


- Add this line to the script in the while loop. Note : if you have more than one while loop in the script logics to show the map icons, then the below line should be added to all the while loops.


item.marker_label = "<img src='https://maps.google.com/mapfiles/marker.png'/>";




There is no fix, only this workaround.


View solution in original post

11 REPLIES 11

Thanks Joel


this solution also worked for our instance


SaschaWildgrube
ServiceNow Employee
ServiceNow Employee

After 10 years.... you might still run into the same issue.

Please note: the "solution" described above DOES NOT OR NO LONGER work.

But here is the real solution:

The problem is that the latitude and longitude values - when using a syntax without getValue are passed as GlideElement classes into the item object. But this causes a REFERENCE stored in the item of the SAME class again and again. While iterating over the GlideRecord using next() - guess what - yes, will result in all markers getting the same (last) value of longitude and latitude - hence all markers are displayed at exactly the same spot!
The solution (to the above code example):

Replace this:

item.latitude = loc.latitude;

item.longitude = loc.longitude;

by this:

item.latitude = loc.latitude + '';

item.longitude = loc.longitude + '';

This converts the values into static string literals - so no class references anymore.

 

We just need to accept the fact that JavaScript is inherently broken and does not behave as expected in many cases.

😄

 

This problem is not specific to map scripts: This may happen anywhere in your code - so watch out!