Skip to content

Commit 9449dec

Browse files
committed
fix: accuracy issues in the map markers
It was bugging me. This version still has it's issues but. it's a lot more accurate.
1 parent efc8913 commit 9449dec

File tree

3 files changed

+70
-52
lines changed

3 files changed

+70
-52
lines changed

js/src/map.js

+29-33
Original file line numberDiff line numberDiff line change
@@ -21,36 +21,51 @@ var _MAP_markerStore = [];
2121
var _MAP_map;
2222

2323
var _MAP_tiles = {
24-
"Normal": L.tileLayer("images/tiles/normal/minimap_sea_{y}_{x}.png", { maxNativeZoom: 0 }), //Originally had the "tileSize" attribute set. Removing it allows for the "zoomed out" look
25-
"Postal": L.tileLayer("images/tiles/postal/minimap_sea_{y}_{x}.png", { maxNativeZoom: 0 })
24+
"Normal": L.tileLayer("images/tiles/normal/minimap_sea_{y}_{x}.png", { minZoom: -2, maxZoom: 0, maxNativeZoom: 0, minNativeZoom: 0, tileSize: 1024 }), //Originally had the "tileSize" attribute set. Removing it allows for the "zoomed out" look
25+
"Postal": L.tileLayer("images/tiles/postal/minimap_sea_{y}_{x}.png", { minZoom: -4, maxZoom: 0, maxNativeZoom: 0, minNativeZoom: 0, tileSize: 3072 })
2626
};
2727

28+
var _MAP_currentLayer = _MAP_tiles["Normal"];
29+
2830
function mapInit(elementID) {
2931
_MAP_map = L.map(elementID, {
30-
maxZoom: 3,
31-
minZoom: 0,
3232
crs: L.CRS.Simple,
33-
layers: [_MAP_tiles["Normal"]]
34-
}).setView([0, 0], 0);
33+
layers: [_MAP_currentLayer]
34+
}).setView([0,0], 0);
3535

36-
var mapBounds = [[0,256*2], [-256*3, 0]];
37-
38-
//_MAP_map.fitBounds(mapBounds);
39-
_MAP_map.setMaxBounds(mapBounds);
40-
//L.rectangle(mapBounds).addTo(_MAP_map);
36+
// Use the "normal" bounds since, it's default.
37+
var h = _MAP_currentLayer.options.tileSize * 3,
38+
w = _MAP_currentLayer.options.tileSize * 2;
4139

40+
var southWest = _MAP_map.unproject([0, h], _MAP_map.getMaxZoom());
41+
var northEast = _MAP_map.unproject([w, 0], _MAP_map.getMaxZoom());
42+
var mapBounds = new L.LatLngBounds(southWest, northEast);
43+
44+
_MAP_map.setMaxBounds(mapBounds);
4245
L.control.layers(_MAP_tiles).addTo(_MAP_map);
4346

4447
_MAP_map.on("baselayerchange", function (e) {
45-
console.log(e.layer);
46-
//var recent = _MAP_map.unproject([e.layer.options.tileSize, e.layer.options.tileSize], _MAP_map.getMaxZoom());
47-
//_MAP_map.setView(recent, 0);
48+
var h = e.layer.options.tileSize * 3,
49+
w = e.layer.options.tileSize * 2;
50+
51+
var southWest = _MAP_map.unproject([0, h], _MAP_map.getMaxZoom());
52+
var northEast = _MAP_map.unproject([w, 0], _MAP_map.getMaxZoom());
53+
54+
var mapBounds = new L.LatLngBounds(southWest, northEast);
55+
56+
_MAP_map.setMaxBounds(mapBounds);
57+
_MAP_currentLayer = e.layer;
58+
59+
clearAllMarkers();
60+
toggleBlips();
4861
});
4962

5063
_MAP_map.on('click', function (e) {
5164
console.log(e);
5265
});
5366
//L.tileLayer("images/tiles/minimap_{y}_{x}.png", {tileSize: 512}).addTo(_MAP_map);
67+
68+
///_MAP_map.setCenter([w * 0.5, h * 0.5]);
5469
}
5570

5671
function createMarker(animated, draggable, objectRef, title) {
@@ -87,25 +102,6 @@ function createMarker(animated, draggable, objectRef, title) {
87102
draggable: draggable ? true : false
88103
}).addTo(_MAP_map).bindPopup(infoContent);
89104

90-
/*
91-
var marker = new google.maps.Marker({
92-
id: _MAP_markerStore.length,
93-
type: locationType.name,
94-
position: coord,
95-
icon: image,
96-
map: _MAP_map,
97-
popup: infoBox,
98-
object: objectRef,
99-
draggable: draggable ? true : false,
100-
animation: animated ? google.maps.Animation.DROP : 0
101-
});
102-
103-
if (name == "@DEBUG@@Locator") {
104-
$("#marker-list").append('<div id="marker_' + marker.id + '" data-id="' + marker.id + '" class="marker-item"><div class="marker-desc"><span class="marker_name">@Locator</span></div><div class="marker-options"><a href="#" class="marker_view" title="View"><img src="images/icons/view.png" alt="View" height="16" width="16" /></a> </div></div><div class="clear"></div>');
105-
} else {
106-
$("#marker-list").append('<div id="marker_' + marker.id + '" data-id="' + marker.id + '" class="marker-item"><div class="marker-desc"><span class="marker_name">' + name + '</span></div><div class="marker-options"><a href="#" class="marker_view" title="View"><img src="images/icons/view.png" alt="View" height="16" width="16" /></a> / <a href="#" class="marker_delete" title="Delete"><img src="images/icons/delete.png" alt="Delete" height="16" width="16" /></a></div></div><div class="clear"></div>');
107-
}
108-
*/
109105
_MAP_markerStore.push(marker);
110106
return _MAP_markerStore.length;
111107
}

js/src/socket.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ function onMessage(e) {
9797
//console.log("player left:" + data.payload);
9898
playerLeft(data.payload);
9999
}
100-
100+
101101
}
102102

103103
function onError(e) {
@@ -335,7 +335,7 @@ function doPlayerUpdate(players) {
335335
var t = MarkerTypes[plr.icon];
336336

337337
//console.log("Got icon of :" + plr.icon);
338-
338+
339339
_MAP_markerStore[localCache[plr.identifer].marker].setIcon(L.icon(t));
340340
}
341341

@@ -357,7 +357,7 @@ function doPlayerUpdate(players) {
357357
var html = getPlayerInfoHtml(plr);
358358

359359
var infoContent = '<div class="info-window"><div class="info-header-box"><div class="info-icon"></div><div class="info-header">' + plr.name + '</div></div><div class="clear"></div><div id=info-body>' + html + "</div></div>";
360-
360+
361361
_MAP_markerStore[m].bindPopup(infoContent);
362362

363363
}

js/src/utils.js

+38-16
Original file line numberDiff line numberDiff line change
@@ -19,39 +19,61 @@
1919
function isNumeric(n) {
2020
return !isNaN(parseFloat(n)) && isFinite(n)
2121
}
22+
function normalize(value, min, max) {
23+
return Math.abs((value - min) / (max - min));
24+
}
25+
26+
var game_1_x = 327.86;
27+
var game_1_y = 1660.16;
28+
29+
var game_2_x = -3778.16;
30+
var game_2_y = -4549.6;
2231

23-
//These settings seem to work for tilesize 256 (the default). The resources I could find don't help here.. So, the values below are botched. And _seem_ to work alright.
2432

25-
// Gta 5's min max bounds
2633
var game_min_x = -4000.00;
2734
var game_max_x = 6000.00;
28-
var game_min_y = 8000;
29-
var game_max_y = -4000;
30-
31-
// Map's min max bounds based on Leaflet's shitty system
35+
var game_min_y = 8000.00;
36+
var game_max_y = -4000.00;
3237
var map_min_x = 0;
33-
var map_max_x = 585; // Don't fucking ask.
34-
var map_min_y = 17; // IDFK why this needs to be 17 but, it seems to be accurate
35-
var map_max_y = 256*3 - 45; // WHYYYY???
38+
var map_min_y = 0;
3639

37-
function normalize(value, min, max){
38-
return Math.abs((value - min) / (max - min));
40+
/*
41+
var rLng = map_1_lng + (x - game_1_x) * (map_1_lng - map_2_lng) / (game_1_x - game_2_x);
42+
var rLat = map_1_lat + (y - game_1_y) * (map_1_lat - map_2_lat) / (game_1_y - game_2_y);
43+
*/
44+
function convertToMap(x, y) {
45+
var h = _MAP_currentLayer.options.tileSize * 3,
46+
w = _MAP_currentLayer.options.tileSize * 2;
47+
48+
var latLng1 = _MAP_map.unproject([w * 0.5, h * 0.5], _MAP_map.getMaxZoom());
49+
var latLng2 = _MAP_map.unproject([0, h], _MAP_map.getMaxZoom());
50+
51+
var rLng = latLng1.lng + (x - game_1_x) * (latLng1.lng - latLng2.lng) / (game_1_x - game_2_x);
52+
var rLat = latLng1.lat + (y - game_1_y) * (latLng1.lat - latLng2.lat) / (game_1_y - game_2_y);
53+
return result = {
54+
lat: rLat,
55+
lng: rLng
56+
};
3957
}
4058

41-
function convertToMap(x, y) {
59+
function convertToMapOld(x, y) {
60+
var map_max_x = _MAP_currentLayer.options.tileSize * 2,
61+
map_max_y = _MAP_currentLayer.options.tileSize * 3;
62+
4263
var xPercent = normalize(x, game_min_x, game_max_x);
4364
var destX = xPercent * (Math.abs(map_max_x - map_min_x)) + map_min_x;
4465

4566
var yPercent = normalize(y, game_min_y, game_max_y);
4667
var destY = yPercent * (Math.abs(map_max_y - map_min_y)) + map_min_y;
47-
48-
console.log(destX, destY);
49-
return {x: destX, y: destY};
68+
69+
console.log(convertToMap(x,y));
70+
console.log(_MAP_map.unproject([destX, destY], _MAP_map.getMaxZoom()));
71+
return _MAP_map.unproject([destX, destY], _MAP_map.getMaxZoom());
5072
}
5173

5274
function convertToMapLeaflet(x, y){
5375
var t = convertToMap(x, y);
54-
return {lat: -t.y, lng: t.x};// Leaflet switches lat and lng around. Y gets lower the further down you go so.. Need to invert it
76+
return t;
5577
}
5678

5779
function stringCoordToFloat(coord) {

0 commit comments

Comments
 (0)