0

I am trying to use the JS version of mapbox in my web application. BUt when changing the example lat-long to delhi's latlong its showing wrong location.

<!DOCTYPE html>
<html>
<head>
    <meta charset='utf-8' />
    <title>Display a map</title>
    <meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
    <script src='https://api.tiles.mapbox.com/mapbox-gl-js/v0.54.0/mapbox-gl.js'></script>
    <link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.54.0/mapbox-gl.css' rel='stylesheet' />
    <style>
        body { margin:0; padding:0; }
        #map { position:absolute; top:0; bottom:0; width:100%; }
    </style>
</head>
<body>

<div id='map'></div>
<script>
mapboxgl.accessToken = 'pk.eyJ1Ijoid2ViZXhwZXJ0c3VqZWV0IiwiYSI6ImNqdmh4MXM0dDAwMWs0NXFyNHkxc2o1eGwifQ.jgbBoS14uvv8i_lMrCQF5A';
var map = new mapboxgl.Map({
    container: 'map', // container id
    style: 'mapbox://styles/mapbox/streets-v11', // stylesheet location
    center: [28.6466773,76.813073], // starting position [lng, lat]
    zoom: 9 // starting zoom
});
</script>

</body>
</html>

enter image description here

Sujeet Kumar
  • 1,280
  • 1
  • 17
  • 25
  • 3
    Is this a case of swapped co-ordinates, ie does Mapbox expect *long,lat* rather than the near-universal convention of *lat, long* ? Just guessing, the map tile you show is high-latitude (`~76N`) and east longitude (`~28E`). Or, on closer reading of your comments, have you made the switch ? – High Performance Mark May 12 '19 at 10:00
  • please explain more, I am not aware about the swapped co-ordinates. I am using the latlong getting from google map or html5 location service. – Sujeet Kumar May 13 '19 at 03:27
  • 3
    If one interprets `28.6466773,76.813073` as latitude+longitude the coordinates specify a point off the north coast of Europe, as your picture shows. If one interprets them as longitude+latitude they specify a point in India. The almost-universal convention is to specify geo-coordinates as latitude+longitude. If I look at what you have posted you appear to think that they are longitude+latitude, but the map tells a different story. – High Performance Mark May 13 '19 at 06:07
  • Oh, got it. Its working now. As you said Its an issue of swapped co-ordinates. – Sujeet Kumar May 13 '19 at 07:45
  • It's not that universal. https://macwright.org/lonlat/ – Steve Bennett May 14 '19 at 04:04

1 Answers1

0

As High Performance Mark mentioned, MapBox uses swapped co-ordinates that means they are not using:

[lat, lng]

they are using

[lng, lat]

Try to change it and then try again.

Mayank Pandeyz
  • 25,704
  • 4
  • 40
  • 59