Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Javascript: Polygon not showing on the map

Why polygon not showing on the map? I was trying to highlight a specific location using polygon but it is not showing on the map even though there is no error. here is the code:

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" 
    />
    <link rel="stylesheet" 
href="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.7.1/leaflet.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.7.1/leaflet.js"> 
</script>
<style>
  #map {position: absolute; top: 0; right: 0; bottom: 0; left: 0;}
</style>
</head>
<body>
<div id="map">
  <a href="https://www.maptiler.com" 
style="position:absolute;left:10px;bottom:10px;z-index:999;"><img 
src="https://api.maptiler.com/resources/logo.svg" alt="MapTiler logo"></a>
</div>
<p><a href="https://www.maptiler.com/copyright/" target="_blank">© MapTiler</a> <a 
href="https://www.openstreetmap.org/copyright" target="_blank">© OpenStreetMap 
contributors</a></p>
<script>
  var map = L.map('map').setView([8.94917, 125.54361], 12);
  L.tileLayer('https://api.maptiler.com/maps/streets/{z}/{x}/{y}.png? 
key=aLd1dKi19JrBFCzDze2p',{
    tileSize: 512,
    zoomOffset: -1,
    minZoom: 1,
    attribution: "\u003ca href=\"https://www.maptiler.com/copyright/\" 
target=\"_blank\"\u003e\u0026copy; MapTiler\u003c/a\u003e \u003ca 
href=\"https://www.openstreetmap.org/copyright\" target=\"_blank\"\u003e\u0026copy; 
OpenStreetMap contributors\u003c/a\u003e",
    crossOrigin: true
  }).addTo(map);
  var polygon = L.polygon([8.94917, 125.54361],[9.00333023, 125.48916626], 
[8.85280991, 125.66551208]).addTo(map);
</script>
</body>
</html>

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

There are several issues that prevent the map from displaying, and the polygon shape for loading:

  1. some small errors in source, caused by line returns: see browser console to check and correct these. (note these might not exist in your source, but may be caused by line wrapping when copy/pasting the source).

  2. Main issue is that the polygon coordinates should be wrapped in an array, as shown in bold in this example :

    var polygon = L.polygon( [ [8.94917, 125.54361],[9.00333023, 125.48916626], [8.85280991, 125.66551208] ] ).addTo(map);

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading