Skip to content

Instantly share code, notes, and snippets.

@PerryRylance
Last active June 2, 2020 07:40
Show Gist options
  • Save PerryRylance/af18aa3046c2bd338a6405885b7330af to your computer and use it in GitHub Desktop.
Save PerryRylance/af18aa3046c2bd338a6405885b7330af to your computer and use it in GitHub Desktop.
jQuery(function($) {
var Parent;
WPGMZA.CustomOLMap = function(element, options)
{
var self = this;
WPGMZA.ProMap.apply(this, arguments);
$("[data-map-id] > .update-nag").remove();
this.setOptions(options);
var extent = [0, 0, 8192, 8192];
var projection = new ol.proj.Projection({
code: 'gta5-map',
units: 'pixels',
extent: extent
});
this.olMap = new ol.Map({
layers: [
new ol.layer.Image({
source: new ol.source.ImageStatic({
attributions: '© Rockstar',
url: 'https://cdn.gtaboom.com/wp-content/uploads/2020/05/gta5map.jpg',
projection: projection,
imageExtent: extent
})
})
],
target: element,
view: new ol.View({
projection: projection,
center: ol.extent.getCenter(extent),
zoom: 2,
maxZoom: 8
})
});
// TODO: Re-implement using correct setting names
// Interactions
this.olMap.getInteractions().forEach(function(interaction) {
// NB: The true and false values are flipped because these settings represent the "disabled" state when true
if(interaction instanceof ol.interaction.DragPan)
interaction.setActive( (self.settings.wpgmza_settings_map_draggable == "yes" ? false : true) );
else if(interaction instanceof ol.interaction.DoubleClickZoom)
interaction.setActive( (self.settings.wpgmza_settings_map_clickzoom ? false : true) );
else if(interaction instanceof ol.interaction.MouseWheelZoom)
interaction.setActive( (self.settings.wpgmza_settings_map_scroll == "yes" ? false : true) );
}, this);
// Controls
this.olMap.getControls().forEach(function(control) {
// NB: The true and false values are flipped because these settings represent the "disabled" state when true
if(control instanceof ol.control.Zoom && WPGMZA.settings.wpgmza_settings_map_zoom == "yes")
self.olMap.removeControl(control);
}, this);
if(WPGMZA.settings.wpgmza_settings_map_full_screen_control != "yes")
this.olMap.addControl(new ol.control.FullScreen());
// Listen for end of pan so we can wrap longitude if needs be
this.olMap.on("moveend", function(event) {
self.wrapLongitude();
self.isBeingDragged = false;
self.dispatchEvent("dragend");
self.onIdle();
});
// Listen for zoom
this.olMap.getView().on("change:resolution", function(event) {
self.dispatchEvent("zoom_changed");
self.dispatchEvent("zoomchanged");
setTimeout(function() {
self.onIdle();
}, 10);
});
// Listen for bounds changing
this.olMap.getView().on("change", function() {
// Wrap longitude
self.onBoundsChanged();
});
self.onBoundsChanged();
// Vector rendering mode
if(WPGMZA.OLMarker.renderMode == WPGMZA.OLMarker.RENDER_MODE_VECTOR_LAYER)
{
// Marker layer
this.markerLayer = new ol.layer.Vector({
source: new ol.source.Vector({
features: []
})
});
this.olMap.addLayer(this.markerLayer);
this.olMap.on("click", function(event) {
var features = self.olMap.getFeaturesAtPixel(event.pixel);
if(!features || !features.length)
return;
var marker = features[0].wpgmzaMarker;
if(!marker)
return;
marker.trigger("click");
marker.trigger("select");
});
}
// Store locator center
var marker;
if(this.storeLocator && (marker = this.storeLocator.centerPointMarker))
{
this.olMap.addOverlay(marker.overlay);
marker.setVisible(false);
}
// Right click listener
$(this.element).on("click contextmenu", function(event) {
var isRight;
event = event || window.event;
var latLng = self.pixelsToLatLng(event.offsetX, event.offsetY);
if("which" in event)
isRight = event.which == 3;
else if("button" in event)
isRight = event.button == 2;
if(event.which == 1 || event.button == 1)
{
if(self.isBeingDragged)
return;
// Left click
if($(event.target).closest(".ol-marker").length)
return; // A marker was clicked, not the map. Do nothing
self.trigger({
type: "click",
latLng: latLng
});
return;
}
if(!isRight)
return;
return self.onRightClick(event);
});
this.trigger("init");
this.dispatchEvent("created");
WPGMZA.events.dispatchEvent({type: "mapcreated", map: this});
// Legacy event
$(this.element).trigger("wpgooglemaps_loaded");
this.setMinZoom(3);
}
Parent = WPGMZA.OLProMap;
WPGMZA.CustomOLMap.prototype = Object.create(Parent.prototype);
WPGMZA.CustomOLMap.prototype.constructor = WPGMZA.CustomOLMap;
WPGMZA.Map.createInstance = function(element, options)
{
return new WPGMZA.CustomOLMap(element, options);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment