温馨提示:本文翻译自stackoverflow.com,查看原文请点击:leaflet - Incorrect Latitude Conversion from SA-MP(game) coordinates for map (while Longitude is correct)
leaflet latitude-longitude marker coordinate-systems

leaflet - 从 map的SA-MP(游戏)坐标进行的纬度转换不正确(经度正确)

发布于 2020-03-29 13:01:30

介绍

我正在将游戏坐标转换为 map坐标,我具有自定义的图块设置和 map。

我在传单 map上显示了玩家的车辆(与 map框相同的问题),x轴的坐标转换正确(在视觉上),而y轴(纬度)不正确。

我尝试过的

I am trying to convert game coordinates which range from 3000 to -3000 on X and 3000 to -3000 on Y The Map coordinates I believe range from 180 to -180 on X - Longitude and 85 to -85 on Y - Latitude

Currently I am using this formula to calculate its coordinates on map, But I cant figure out what I'm doing wrong here.

I put u dynamic so i can easily test it..

Notice that return value has x and y inverted because that is just how leaflet is...

function getLatLngFromPos(x, y, u = 85) {
    height = 3000; // of the game map
    width = 3000; // of the game map

    return [(y / height) * u, (x / width) * 180];
}

Results(~ means not calculated but visually guessed):

getLatLngFromPos(1482.47 , -1731.9)

-> [-49.0705, 88.9482] Should be: ~[-71.519235, 88.813477]

getLatLngFromPos(1378.4,-1876.25)

-> [-53.16041666666666,82.70400000000001] Should be: ~[-73.758964, 81.958008]

If You need the tiles here is the code to that http://145.239.116.170/app-assets/images/maptiles/sanandreas.{z}.{x}.{y}.png

Note: MaxZoom is '4'

Results

Incorrect Result:

The Car icon on the map should go to the arrow, and there is extra information like the center of the map. (Also it is repeating on X axis which i want to remove)

ScreenShot: (https://i.ibb.co/HNHgRLC/Shared-Screenshot.jpg)

Hypothesis

I am thinking the Y axis on these maps has some kind of acceleration towards the edges... This is due to the Default Projection of Leaflet Also there is an implementation of this for Google Maps API at (https://github.com/ikkentim/SanMap/blob/master/js_src/js/SanMap.js) But Google Maps is being a douche by charging money...

I am assuming now that I will have to create some kind of projection like in SanMap implementation, How can I create my own Projection for Mapbox or Leaflet?

查看更多

查看更多

提问者
Raja Bilal
被浏览
37
hiwave 2020-01-31 18:51

I'm working on the same problem, this is not the solution yet, but it might get you on the right track. What I tried was to use CRS instead of leaflets Earth-like projection. Just change w,h and the tileSize in tileLayer and it should be good to go. Also 0,0 will be in the top left corner.

好的,我已经用我的方法找到了解决方案。由于tilesize为256,因此我们的mapsize为256x256。游戏中的坐标从-3000,-3000到3000,3000,这使其成为6000x6000坐标系。您可以通过将 map的大小除以我们拥有的tileize来获得比例,然后将x除以比例。我们通过将x加128并从y减去-128来校正坐标。之后,我们在返回坐标时切换坐标。

function fixCoords(x,y) {
    x=x/(6000/256);
    y=y/(6000/256);
    x = x+128;
    y = y-128;
    return [y,x];
}
var w = 32768;
var h = 32768;
var mapMinZoom = 1;
var mapMaxZoom = 4;      
var _map = L.map('mapid', {
    maxZoom: mapMaxZoom,
    minZoom: mapMinZoom,
    crs: L.CRS.Simple,
    zoomControl: false,
    attributionControl: false,
    detectRetina: true,
});

var _mapCenter = _map.unproject([w/2, h/2], mapMaxZoom);
_map.setView(_mapCenter, 2);

var _tileLayer = L.tileLayer(
        'http://145.239.116.170/app-assets/images/maptiles/sanandreas.{z}.{x}.{y}.png', {
        minZoom: mapMinZoom, maxZoom: mapMaxZoom,
        bounds: _mapBounds,
        continuousWorld: true,
        noWrap:true,
        tileSize:256,
        crs: L.CRS.Simple,
        detectRetina:false,
}).addTo(_map);

var marker = L.marker([0, 0]).addTo(_map);