Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Allan Hawkey 232 posts 303 karma points
    Mar 05, 2015 @ 22:30
    Allan Hawkey
    0

    Multiple markers on Google Map

    I've downloaded this package and got it working - love it!

    I'm struggling with one aspect though - how can I display the map with multiple markers and infowindows?

    I've set this up so that there is a "Map" node and a series of "Map Marker" child nodes - so a map may have zero or many markers.

    I can get multiple markers to display, but they all take the info window of the last one in the foreach loop that I'm using in the code.

    Any suggestions for how best to set that up?

    I've had this working in XSLT files in v4.11.10, but am now trying to replicate this in a Razor Partial View Macro in v7.2 and can't see how to do it, being a newbie when it comes to Razor!

    Also, I'd like to add the map to a Grid Text Page similar to the one on the Fanoe starter kit.  Is it best to add this as a macro, or is there a way to set up a map editor directly within the grid?

    Thanks
    Allan

  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Mar 06, 2015 @ 00:40
    Alex Skrypnyk
    0

    Hi Allan,

    Show your map code please. Are you using Google Map API ?

  • Allan Hawkey 232 posts 303 karma points
    Mar 06, 2015 @ 09:19
    Allan Hawkey
    0

    Hi Alex

    Here's the code as it currently stands:

    @inherits Umbraco.Web.Macros.PartialViewMacroPage

    @{
    var nodeId = Model.MacroParameters["mapNode"];
    var node = Umbraco.Content(nodeId);
    string mapLocation = node.mapLocation.ToString().Trim();
    string[] mapLocationInfo = mapLocation.Split( ',' );
    string latitude = mapLocationInfo[0];
    string longitude = mapLocationInfo[1];
    string zoom = mapLocationInfo[2];
    var markerCount = node.Children.Where("Visible").Count();
    var markers = node.Children.Where("Visible");
    }

    <style>
    #map-canvas {
    height: @Model.MacroParameters["mapHeight"];
    margin: 0px;
    padding: 0px
    }
    </style>

    <script>
    function initialize() {
    var myLatlng = new google.maps.LatLng(@latitude, @longitude);
    var mapOptions = {
    zoom: @zoom, center: myLatlng
    };
    var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

    @foreach (var item in markers) {
    string markerLocation = @item.mapLocation.ToString().Trim();
    string[] markerLocationInfo = @markerLocation.Split( ',' );
    <text>
    var markerLatitude = @markerLocationInfo[0];
    var markerLongitude = @markerLocationInfo[1];
    var contentString = '@item.markerInfo';
    var markerName = '@item.pageName';

    var infowindow = new google.maps.InfoWindow({
    content: contentString,
    maxWidth: 200
    });
    var markerLatlng = new google.maps.LatLng(markerLatitude, markerLongitude);
    var marker = new google.maps.Marker({
    position: markerLatlng,
    map: map,
    title: markerName
    });

    google.maps.event.addListener(marker, 'click', function() {
    infowindow.open(map,this);
    });
    </text>
    }
    }

    google.maps.event.addDomListener(window, 'load', initialize);

    </script>
    <div id="map-canvas"></div>

    Thanks
    Allan

  • Jonathan Richards 288 posts 1742 karma points MVP
    Mar 06, 2015 @ 17:01
    Jonathan Richards
    1

    Looks to me you have a closure problem with your addListerner(), which will use the last value of infowindow.

    http://stackoverflow.com/questions/7044587/adding-multiple-markers-with-infowindows-google-maps-api

  • Allan Hawkey 232 posts 303 karma points
    Mar 06, 2015 @ 22:46
    Allan Hawkey
    100

    Hi Jonathan

    Thank you for that - got it sorted now using that help.

    Allan

  • Abhishek Pokhrel 23 posts 163 karma points
    Feb 16, 2017 @ 08:46
    Abhishek Pokhrel
    0

    Can you please provide the full description on how you added multiple marker on Maps using this package Angular Google Maps?

  • Jonathan Richards 288 posts 1742 karma points MVP
    Feb 16, 2017 @ 20:29
    Jonathan Richards
    0

    Hi Abhishek,

    Not too sure why you have added a question to a 2 year old thread, but this package, Angular Google Maps, has been deprecated for Terratype which has superior features and also contains examples in the manual showing exactly how to do this. It comes with its own renderer which takes all the hassle out of displaying maps

    But, if you are intent on continuing to use Angular Google Maps, then like any data type you can place multiple copies of it either across various content nodes or within complex datatypes like Grid Editor, Nested Content or Archetype. Once you have multiple maps, you then have to write your own javascript code to display those multiple maps as one map. The code listed above by Allan does do that except for a small issue to do with closure, which can be rectified using the 'with' command

    with ({
        c : contentString
    }) {
        var infowindow = new google.maps.InfoWindow({
            content: contentString,
            maxWidth: 200
        });
    }
    

    Cheers

    Jonathan

Please Sign in or register to post replies

Write your reply to:

Draft