1

I want to display a map (Google maps initially) in a website (e.g. the position of some runners in an event/race), alongisde some markers with the initials of each runner. That is, depending on the data I get in JSON format:

data = [
    {
        "lat": 44.363,
        "lng": 3.044,
        "full_name": "Paul Cardiff"
    }
    {
        "lat": 44.473,
        "lng": 3.144,
        "full_name": "Mark Zein"
    }
    ...
]

I would like to represent the current position of each runner in the map with a marker identified by its initials (Mark Zein --> M.Z.). For instance (forgive me for this representation):

                            -----
                            |M.Z|       _______________________road
      -----                 --|--      /
      |P.C|          _________v________| 
      --|--         /
     ___v__________/   

I know I can create a google.maps.Marker with a custom icon, but I am finding hard to create these icons dinamically based on data I receive (and that might change over time).

Is there a way to dynamically create images/icons from data? Or can you think of another way of generating these icons?

I've been doing some research but so far I didn't find something, so any help will be much appreciated!

Edited:

I am currently mocking the way I get the data, but the idea is to get the data from a socket. So what I have in my code right now is:

var json_socket = {
    "lat": 44.363,
    "lng": 3.044,
    "full_name": "Paul Cardiff"
};

And how I add the markers:

var live_user = {lat: json_socket["lat"], lng: json_socket["lng"]};
var marker = new google.maps.Marker({
    position: live_user,
    map: map,
    icon: "icon.png"
});
vabada
  • 1,738
  • 4
  • 29
  • 37
  • 1
    Could you show us what you have tried to create those markers ? And where you get those data ? – Weedoze Nov 03 '16 at 07:27
  • I added what I have in my code right now (just a mock, since I expect the actual data from a socket I will connect to). Perhaps I should create the markers in the server? – vabada Nov 03 '16 at 07:31
  • 1
    A mock is ok for the moment. You should also show us the markers code – Weedoze Nov 03 '16 at 07:32
  • Added, it's just the normal way for `google.maps.Marker`. Thanks! – vabada Nov 03 '16 at 07:37

1 Answers1

1

You can iterate over the array of markers with simple loop

I used the label to display the initials

var data = [{
        "lat": 44.363,
        "lng": 3.044,
        "full_name": "Paul Cardiff"
    } {
        "lat": 44.473,
        "lng": 3.144,
        "full_name": "Mark Zein"
    }
    ...
];

for (var i = 0; i < data.length; i++) {
    var item = data[i];
    var latLng = new google.maps.LatLng(item.lat, item.lng);

    var initials = item.full_name.match(/\b(\w)/g).join('');

    // Creating a marker and putting it on the map
    var marker = new google.maps.Marker({
        position: latLng,
        map: map,
        label: initials,
        icon: "icon.png"
    });
}
Weedoze
  • 13,683
  • 1
  • 33
  • 63
  • Nice, thank you for the answer. However, this sets the title of the icon to the initials, but I would like to see the initials in the map, without having to mouseover or click over a marker. That's why I thought of "dynamically create the icons", but I might have taken the wrong approach. – vabada Nov 03 '16 at 07:49
  • 1
    Ok, sorry I have never worked with google map markers. I changed `title` to `label`. From the doc, it seems that label will be displayed directly in the marker. Could you try ? – Weedoze Nov 03 '16 at 07:51
  • Yep, that might work if I manage to set a proper background icon, and format the label. Thanks! I'll write back if I manage to do it – vabada Nov 03 '16 at 07:54
  • 1
    Ok cool ! I can still try to help you if you need even if I don't really know how to code with google markers hehe – Weedoze Nov 03 '16 at 07:55
  • I finally managed, changing the label properties as I saw in http://stackoverflow.com/questions/32467212/google-maps-marker-label-with-multiple-characters Thank you very much anyway! – vabada Nov 03 '16 at 10:24
  • No problem ! Have fun with your map – Weedoze Nov 03 '16 at 10:30