0

I recently downloaded a template code from amcharts.com so that I could put a map on my website. The code puts little pulsating dots on whatever location I set the latitude and longitude, and I can also include a title for each dot. However whenever I try to add another parameter, like a URL, I get an error. At this point I am trying to make it so that the mouse hovering over each dot will create a popup image of that location. I'm having trouble figuring out how to get the popup to work, and to make sure that each dot has a different popup image. Here is a link to the codepen I've been using.

https://codepen.io/ZoeyEllen/pen/yvXvZX?editors=1111

I have done quite a bit of googling and I've found several different suggestions for javascript, html, and css, but nothing I've tried seems to have worked. Here is the code that I have so far:

var map = AmCharts.makeChart( "chartdiv", {
  "type": "map",
  "theme": "light",
  "projection": "mercator",

  "imagesSettings": {
    "rollOverColor": "#089282",
    "rollOverScale": 5,
    "selectedScale": 5,
    "selectedColor": "#089282",
    "color": "#13564e"
  },

  "areasSettings": {
    "unlistedAreasColor": "#15A892"
  },

  "dataProvider": {
    "map": "worldLow",
    "images": [ {
      "zoomLevel": 5,
      "scale": 0.5,
      "title": "Hawaii, USA",
      "latitude": 19.8968,
      "longitude": -155.5828
    }, {
      "zoomLevel": 5,
      "scale": 0.5,
      "title": "Galápagos Islands",
      "latitude": -0.8675,
      "longitude": -89.4364
   }, {
      "zoomLevel": 5,
      "scale": 0.5,
      "title": "Italy",
      "latitude": 41.9028,
      "longitude": 12.4964
    }, {
      "zoomLevel": 5,
      "scale": 0.5,
      "title": "Greece",
      "latitude": 37.9838,
      "longitude": 23.7275
      }, {
      "zoomLevel": 5,
      "scale": 0.5,
      "title": "The Bahamas",
      "latitude": 24.9314,
      "longitude": -76.1900
    }, {
      "zoomLevel": 5,
      "scale": 0.5,
      "title": "South Florida, USA",
      "latitude": 26.1224,
      "longitude": -80.1373
        }, {
      "zoomLevel": 5,
      "scale": 0.5,
      "title": "Canada",
      "latitude": 45.5017,
      "longitude": -73.5673
          }, {
      "zoomLevel": 5,
      "scale": 0.5,
      "title": "Washington State, USA",
      "latitude": 47.6062,
      "longitude": -122.3321
      }, {
      "zoomLevel": 5,
      "scale": 0.5,
      "title": "California, USA",
      "latitude": 34.0522,
      "longitude": -118.2437
          }, {
      "zoomLevel": 5,
      "scale": 0.5,
      "title": "Illinois, USA",
      "latitude": 41.8781,
      "longitude": -87.6298
        }, {
      "zoomLevel": 5,
      "scale": 0.5,
      "title": "Costa Rica",
      "latitude": 9.7489,
      "longitude": -83.7534
           }, {
      "zoomLevel": 5,
      "scale": 0.5,
      "title": "North Carolina, USA",
      "latitude": 35.7596,
      "longitude": -79.0193
    } ]
  }
} );

// add events to recalculate map position when the map is moved or zoomed
map.addListener( "positionChanged", updateCustomMarkers );

// this function will take current images on the map and create HTML elements for them
function updateCustomMarkers( event ) {
  // get map object
  var map = event.chart;

  // go through all of the images
  for ( var x in map.dataProvider.images ) {
    // get MapImage object
    var image = map.dataProvider.images[ x ];

    // check if it has corresponding HTML element
    if ( 'undefined' == typeof image.externalElement )
      image.externalElement = createCustomMarker( image );

    // reposition the element accoridng to coordinates
    var xy = map.coordinatesToStageXY( image.longitude, image.latitude );
    image.externalElement.style.top = xy.y + 'px';
    image.externalElement.style.left = xy.x + 'px';
  }
}

// this function creates and returns a new marker element
function createCustomMarker( image ) {
  // create holder
  var holder = document.createElement( 'div' );
  holder.className = 'map-marker';
  holder.title = image.title;
  holder.style.position = 'absolute';

  // maybe add a link to it?
  if ( undefined != image.url ) {
    holder.onclick = function() {
      window.location.href = image.url;
    };
    holder.className += ' map-clickable';
  }

  // create dot
  var dot = document.createElement( 'div' );
  dot.className = 'dot';
  holder.appendChild( dot );

  // create pulse
  var pulse = document.createElement( 'div' );
  pulse.className = 'pulse';
  holder.appendChild( pulse );

  // append the marker to the map container
  image.chart.chartDiv.appendChild( holder );
holder.onmouseover = function(){
     console.log(image.title);
  }

  return holder;
}
#chartdiv {
  width: 100%;
  height: 500px;
}

.map-marker {
    /* adjusting for the marker dimensions
    so that it is centered on coordinates */
    margin-left: -8px;
    margin-top: -8px;
}
.map-marker.map-clickable {
    cursor: pointer;
}
.pulse {
    width: 10px;
    height: 10px;
    border: 3px solid #FFB6C1;
    -webkit-border-radius: 20px;
    -moz-border-radius: 20px;
    border-radius: 20px;
    background-color: #ff69b4;
    z-index: 10;
    position: absolute;
  }
.map-marker .dot {
    border: 3px solid #ff69b4;
    background: transparent;
    -webkit-border-radius: 40px;
    -moz-border-radius: 40px;
    border-radius: 40px;
    height: 50px;
    width: 50px;
    -webkit-animation: pulse 3s ease-out;
    -moz-animation: pulse 3s ease-out;
    animation: pulse 3s ease-out;
    -webkit-animation-iteration-count: infinite;
    -moz-animation-iteration-count: infinite;
    animation-iteration-count: infinite;
    position: absolute;
    top: -20px;
    left: -20px;
    z-index: 1;
    opacity: 0;
  }
  @-moz-keyframes pulse {
   0% {
      -moz-transform: scale(0);
      opacity: 0.0;
   }
   25% {
      -moz-transform: scale(0);
      opacity: 0.1;
   }
   50% {
      -moz-transform: scale(0.1);
      opacity: 0.3;
   }
   75% {
      -moz-transform: scale(0.5);
      opacity: 0.5;
   }
   100% {
      -moz-transform: scale(1);
      opacity: 0.0;
   }
  }
  @-webkit-keyframes "pulse" {
   0% {
      -webkit-transform: scale(0);
      opacity: 0.0;
   }
   25% {
      -webkit-transform: scale(0);
      opacity: 0.1;
   }
   50% {
      -webkit-transform: scale(0.1);
      opacity: 0.3;
   }
   75% {
      -webkit-transform: scale(0.5);
      opacity: 0.5;
   }
   100% {
      -webkit-transform: scale(1);
      opacity: 0.0;
   }
    
</style>
<script src="https://www.amcharts.com/lib/3/ammap.js"></script>
<script src="https://www.amcharts.com/lib/3/maps/js/worldLow.js"></script>
<script src="https://www.amcharts.com/lib/3/plugins/export/export.min.js"></script>
<link rel="stylesheet" href="https://www.amcharts.com/lib/3/plugins/export/export.css" type="text/css" media="all" />
<script src="https://www.amcharts.com/lib/3/themes/light.js"></script>
<div id="chartdiv"></div>
Zoey Best
  • 3
  • 2

1 Answers1

0

Ok so i hope this is what you are looking for. I added the pop ups to appear center in the map. You can add yor css for the pop up but this demostrates what you want.

Explanation

HTML

Added each popup as a <div> at the bottom of the HTML. They have the individual value to refer in the images objects ant their class popup to give the the style you want

<div id="pop0" class="popup">A</div>

CSS

Added two clases

  1. popup: to style the popups as a whole

    .popup { display: none; position: absolute; top: 0; bottom: 0; right: 0; left: 0; margin: auto; background: red; width: 50px; height: 50px; }

  2. popon: to turn the popup on with the listeners

    .popon { display: block; }

JS

Added two diferent thing here

First I added the pop value in each element to refer to the id of the desired popup for each dot

Second I added two events listeners

  1. MouseOver - This will add the class popon to the popup element of the dot

    holder.onmouseover = function() { popup.classList.add('popon'); }

  2. MouseLeave - This will remove the class popon to the popup element of the dot

    holder.onmouseleave = function() { popup.classList.remove('popon'); }

var map = AmCharts.makeChart("chartdiv", {
  "type": "map",
  "theme": "light",
  "projection": "mercator",

  "imagesSettings": {
    "rollOverColor": "#089282",
    "rollOverScale": 5,
    "selectedScale": 5,
    "selectedColor": "#089282",
    "color": "#13564e"
  },

  "areasSettings": {
    "unlistedAreasColor": "#15A892"
  },

  "dataProvider": {
    "map": "worldLow",
    "images": [{
      "zoomLevel": 5,
      "scale": 0.5,
      "title": "Hawaii, USA",
      "latitude": 19.8968,
      "longitude": -155.5828,
      "pop": "pop0"
    }, {
      "zoomLevel": 5,
      "scale": 0.5,
      "title": "Galápagos Islands",
      "latitude": -0.8675,
      "longitude": -89.4364,
      "pop": "pop1"
    }, {
      "zoomLevel": 5,
      "scale": 0.5,
      "title": "Italy",
      "latitude": 41.9028,
      "longitude": 12.4964,
      "pop": "pop2"
    }, {
      "zoomLevel": 5,
      "scale": 0.5,
      "title": "Greece",
      "latitude": 37.9838,
      "longitude": 23.7275,
      "pop": "pop3"
    }, {
      "zoomLevel": 5,
      "scale": 0.5,
      "title": "The Bahamas",
      "latitude": 24.9314,
      "longitude": -76.1900,
      "pop": "pop4"
    }, {
      "zoomLevel": 5,
      "scale": 0.5,
      "title": "South Florida, USA",
      "latitude": 26.1224,
      "longitude": -80.1373,
      "pop": "pop5"
    }, {
      "zoomLevel": 5,
      "scale": 0.5,
      "title": "Canada",
      "latitude": 45.5017,
      "longitude": -73.5673,
      "pop": "pop6"
    }, {
      "zoomLevel": 5,
      "scale": 0.5,
      "title": "Washington State, USA",
      "latitude": 47.6062,
      "longitude": -122.3321,
      "pop": "pop7"
    }, {
      "zoomLevel": 5,
      "scale": 0.5,
      "title": "California, USA",
      "latitude": 34.0522,
      "longitude": -118.2437,
      "pop": "pop8"
    }, {
      "zoomLevel": 5,
      "scale": 0.5,
      "title": "Illinois, USA",
      "latitude": 41.8781,
      "longitude": -87.6298,
      "pop": "pop9"
    }, {
      "zoomLevel": 5,
      "scale": 0.5,
      "title": "Costa Rica",
      "latitude": 9.7489,
      "longitude": -83.7534,
      "pop": "pop10"
    }, {
      "zoomLevel": 5,
      "scale": 0.5,
      "title": "North Carolina, USA",
      "latitude": 35.7596,
      "longitude": -79.0193,
      "pop": "pop11"
    }]
  }
});


// add events to recalculate map position when the map is moved or zoomed
map.addListener("positionChanged", updateCustomMarkers);

// this function will take current images on the map and create HTML elements for them
function updateCustomMarkers(event) {
  // get map object
  var map = event.chart;

  // go through all of the images
  for (var x in map.dataProvider.images) {
    // get MapImage object
    var image = map.dataProvider.images[x];

    // check if it has corresponding HTML element
    if ('undefined' == typeof image.externalElement)
      image.externalElement = createCustomMarker(image);

    // reposition the element accoridng to coordinates
    var xy = map.coordinatesToStageXY(image.longitude, image.latitude);
    image.externalElement.style.top = xy.y + 'px';
    image.externalElement.style.left = xy.x + 'px';
  }
}

// this function creates and returns a new marker element
function createCustomMarker(image) {
  // create holder
  var holder = document.createElement('div');
  holder.className = 'map-marker';
  holder.title = image.title;
  holder.style.position = 'absolute';

  // maybe add a link to it?
  if (undefined != image.url) {
    holder.onclick = function() {
      window.location.href = image.url;
    };
    holder.className += ' map-clickable';
  }

  // create dot
  var dot = document.createElement('div');
  dot.className = 'dot';
  holder.appendChild(dot);

  // create pulse
  var pulse = document.createElement('div');
  pulse.className = 'pulse';
  holder.appendChild(pulse);

  // append the marker to the map container
  image.chart.chartDiv.appendChild(holder);


  //Mouseover listener
  var popup = document.getElementById(image.pop);
  holder.onmouseover = function() {
    popup.classList.add('popon');
  }
  holder.onmouseleave = function() {
    popup.classList.remove('popon');
  }

  return holder;
}
  #chartdiv {
  width: 100%;
  height: 500px;
}

.map-marker {
  /* adjusting for the marker dimensions
    so that it is centered on coordinates */
  margin-left: -8px;
  margin-top: -8px;
}

.map-marker.map-clickable {
  cursor: pointer;
}

.pulse {
  width: 10px;
  height: 10px;
  border: 3px solid #FFB6C1;
  -webkit-border-radius: 20px;
  -moz-border-radius: 20px;
  border-radius: 20px;
  background-color: #ff69b4;
  z-index: 10;
  position: absolute;
}

.map-marker .dot {
  border: 3px solid #ff69b4;
  background: transparent;
  -webkit-border-radius: 40px;
  -moz-border-radius: 40px;
  border-radius: 40px;
  height: 50px;
  width: 50px;
  -webkit-animation: pulse 3s ease-out;
  -moz-animation: pulse 3s ease-out;
  animation: pulse 3s ease-out;
  -webkit-animation-iteration-count: infinite;
  -moz-animation-iteration-count: infinite;
  animation-iteration-count: infinite;
  position: absolute;
  top: -20px;
  left: -20px;
  z-index: 1;
  opacity: 0;
}

.popup {
  display: none;
  position: absolute;
  top: 0;
  bottom: 0;
  right: 0;
  left: 0;
  margin: auto;
  background: red;
  width: 50px;
  height: 50px;
}

.popon {
  display: block;
}

@-moz-keyframes pulse {
  0% {
    -moz-transform: scale(0);
    opacity: 0.0;
  }
  25% {
    -moz-transform: scale(0);
    opacity: 0.1;
  }
  50% {
    -moz-transform: scale(0.1);
    opacity: 0.3;
  }
  75% {
    -moz-transform: scale(0.5);
    opacity: 0.5;
  }
  100% {
    -moz-transform: scale(1);
    opacity: 0.0;
  }
}

@-webkit-keyframes "pulse" {
  0% {
    -webkit-transform: scale(0);
    opacity: 0.0;
  }
  25% {
    -webkit-transform: scale(0);
    opacity: 0.1;
  }
  50% {
    -webkit-transform: scale(0.1);
    opacity: 0.3;
  }
  75% {
    -webkit-transform: scale(0.5);
    opacity: 0.5;
  }
  100% {
    -webkit-transform: scale(1);
    opacity: 0.0;
  }
<script src="https://www.amcharts.com/lib/3/ammap.js"></script>
<script src="https://www.amcharts.com/lib/3/maps/js/worldLow.js"></script>
<script src="https://www.amcharts.com/lib/3/plugins/export/export.min.js"></script>
<link rel="stylesheet" href="https://www.amcharts.com/lib/3/plugins/export/export.css" type="text/css" media="all" />
<script src="https://www.amcharts.com/lib/3/themes/light.js"></script>
<div id="chartdiv"></div>
<div id="pop0" class="popup">A</div>
<div id="pop1" class="popup">B</div>
<div id="pop2" class="popup">C</div>
<div id="pop3" class="popup">D</div>
<div id="pop4" class="popup">E</div>
<div id="pop5" class="popup">F</div>
<div id="pop6" class="popup">G</div>
<div id="pop7" class="popup">H</div>
<div id="pop8" class="popup">I</div>
<div id="pop9" class="popup">J</div>
<div id="pop10" class="popup">K</div>
<div id="pop11" class="popup">L</div>
Gerardo BLANCO
  • 5,590
  • 1
  • 16
  • 35
  • Thanks so much! That looks like it should work perfectly. The one remaining question I have is where exactly to put the image I want to use in the html. Where do I plug in the source of the image to get it to pop up properly? (Forgive me, I know very little about code, I am a blogger not a coder, but I want to learn!) – Zoey Best Feb 15 '18 at 14:04
  • @ZoeyBest i dont really know what image you are refering, but we can do 1 of 3 things. 1) Edit your question and add what else you are refferring to. Comment when you do so i can notice it. This is not the best as having multiple different questions in a single SO posting might be bad. 2) Add another question with the next fase of the code and comment the lin k here so i can go and help you. 3) We can close this question and schedule a skype call so i can help you with your code. If you decide one of these things let me know, I am more than happy to help you. – Gerardo BLANCO Feb 15 '18 at 15:21
  • @ZoeyBest pliss dont forget to upvote or close the question if my answer help you. Thanks – Gerardo BLANCO Feb 18 '18 at 07:58
  • Sorry for the confusion! Let me see if I can clarify my question. Let's say I would like this image to be the image that pops up for "pop0." http://assets.natgeotv.com/Photos/56/140469.jpg Where do I enter that link into the html? Thank you! – Zoey Best Feb 21 '18 at 02:48
  • @ZoeyBest what i made is a div for each dot. You can just treat each div as a pop up. You can add text and images to it. Maybe you should close this question as i allready anser the question. And make another one to get the popup you want to make. I will love to help in the other question if you post the link in a comment – Gerardo BLANCO Feb 21 '18 at 03:23
  • @ZoeyBest thank you, pliss dont forget to upvote if you can. – Gerardo BLANCO Feb 22 '18 at 17:17