WME Thai Regions Overlay - New Regions

Creates polygons for WME "Thai Regions Regions"

// ==UserScript==
// @name                WME Thai Regions Overlay - New Regions
// @namespace           https://greasyfork.org/users/5252
// @description         Creates polygons for WME "Thai Regions Regions"
// @include             https://www.waze.com/editor/*
// @include             https://www.waze.com/*/editor/*
// @include             https://editor-beta.waze.com/*
// @version             2.3
// @grant               none
// @copyright           2014 davielde
// ==/UserScript==


//---------------------------------------------------------------------------------------

//updated by benandjerry to reflect new coordinates of the Thailand regions. (Jan 1, 2017)

//generated by rickzabel's overlay generator

//RZ RaidName will be replaced by the name of the layer in your KML file
//RZ RaidNameNoSpaces will be replaced by the name of the layer in your KML file
//RZ AreaPoints will be replaced by the names, colors, and area points from your KML file

setTimeout(InitMapRaidOverlay, 1000);

function AddRaidPolygon(raidLayer,groupPoints,groupColor,groupNumber){
    
    var mro_Map = Waze.map;
    var mro_OL = OpenLayers;
    var raidGroupLabel = 'Thai Region ' + groupNumber;
    var groupName = 'RaidGroup' + groupNumber;
    
    var style = {
        strokeColor: groupColor,
        strokeOpacity: .8,
        strokeWidth: 3,
        fillColor: groupColor,
        fillOpacity: 0.15,
        label: raidGroupLabel,
        labelOutlineColor: "black",
        labelOutlineWidth: 3,
        fontSize: 14,
        fontColor: groupColor,
        fontOpacity: .85,
        fontWeight: "bold"  
    };
    
    var attributes = {
        name: groupName,
        number: groupNumber
    };
    
    var pnt= [];
    for(i=0;i<groupPoints.length;i++){
        convPoint = new OpenLayers.Geometry.Point(groupPoints[i].lon,groupPoints[i].lat).transform(new OpenLayers.Projection("EPSG:4326"), mro_Map.getProjectionObject());
        //console.log('MapRaid: ' + JSON.stringify(groupPoints[i]) + ', ' + groupPoints[i].lon + ', ' + groupPoints[i].lat);
        pnt.push(convPoint);
    }
		       
    var ring = new mro_OL.Geometry.LinearRing(pnt);
    var polygon = new mro_OL.Geometry.Polygon([ring]);
    
    var feature = new mro_OL.Feature.Vector(polygon,attributes,style);
    raidLayer.addFeatures([feature]);

}

function CurrentRaidLocation(raid_mapLayer){
    var mro_Map = Waze.map;

    for(i=0;i<raid_mapLayer.features.length;i++){
        var raidMapCenter = mro_Map.getCenter();
        var raidCenterPoint = new OpenLayers.Geometry.Point(raidMapCenter.lon,raidMapCenter.lat);
        var raidCenterCheck = raid_mapLayer.features[i].geometry.components[0].containsPoint(raidCenterPoint);
		var holes = raid_mapLayer.features[i].attributes.holes
		
        
        if(raidCenterCheck === true){

			//var str = $('#topbar-container > div > div.location-info-region > div').text();
			var str = $('#topbar-container > div > div.topbar > div.location-info-region > div.location-info').text();
			
			var n2 = str.indexOf(" - ");
			
			if(n2 > 0){
				var n = str.length;
				var res = str.substring(n2+2, n);
				var rescount = res.indexOf(" - ");
				if(rescount>0){
					var n3 = res.length;
					var res2 = res.substring(rescount+2, n3);
				}
				var raidLocationLabel = 'Thai Region ' + raid_mapLayer.features[i].attributes.number + ' - ' + res2;

			} else {
				//var raidLocationLabel = 'Thai Region ' + raid_mapLayer.features[i].attributes.number + ' - ' + $('#topbar-container > div > div.location-info-region > div').text();
				var raidLocationLabel = 'Thai Region ' + raid_mapLayer.features[i].attributes.number + ' - ' + $('#topbar-container > div > div.topbar > div.location-info-region > div.location-info').text();
						
			}	
			//setTimeout(function(){$('#topbar-container > div > div.location-info-region > div').text(raidLocationLabel);},200);
			setTimeout(function(){$('#topbar-container > div > div.topbar > div.location-info-region > div.location-info').text(raidLocationLabel);},200);
			 if (holes === "false") { break; }
		}
    }
}

function InitMapRaidOverlay(){

    var mro_Map = Waze.map;
    var mro_OL = OpenLayers;

    //if (!mro_Map) return;
	
    //if (!mro_OL) return;

    var mro_mapLayers = mro_Map.getLayersBy("uniqueName","__ThaiRegions");
        
    var raid_mapLayer = new mro_OL.Layer.Vector("Thai Regions", {
        displayInLayerSwitcher: true,
        uniqueName: "__ThaiRegions"
    });
        
    I18n.translations.en.layers.name["__ThaiRegions"] = "Thai Regions";
    mro_Map.addLayer(raid_mapLayer);
    raid_mapLayer.setVisibility(true);
    

var V1ChiangMai = [{lon:'97.96228',lat:'20.10442'},{lon:'97.59973',lat:'18.64867'},{lon:'97.2865304',lat:'18.6332083'},{lon:'97.90735',lat:'17.45754'},{lon:'101.10162',lat:'17.48898'},{lon:'101.49164',lat:'19.79937'},{lon:'100.9176',lat:'19.57255'},{lon:'100.3573',lat:'20.62969'},{lon:'97.96228',lat:'20.10442'}];
AddRaidPolygon(raid_mapLayer, V1ChiangMai,"#7C3592","1 Chiang Mai");

var V2Phitsanulok = [{lon:'97.8991',lat:'17.57207'},{lon:'98.77802',lat:'16.09773'},{lon:'98.61597',lat:'16.19482'},{lon:'98.21497',lat:'15.26996'},{lon:'98.05017',lat:'15.28875'},{lon:'98.5528',lat:'14.21'},{lon:'101.24719',lat:'14.21'},{lon:'101.24719',lat:'15.26996'},{lon:'101.24719',lat:'15.30464'},{lon:'101.24719',lat:'17.57207'},{lon:'97.8991',lat:'17.57207'}];
AddRaidPolygon(raid_mapLayer, V2Phitsanulok,"#DB4436","2 Phitsanulok");

var V3UdonThani = [{lon:'101.24719',lat:'14.21'},{lon:'105.64296',lat:'14.19935'},{lon:'105.6567',lat:'16.09605'},{lon:'105.08691',lat:'16.0883'},{lon:'104.64746',lat:'18.28567'},{lon:'103.20825',lat:'18.47334'},{lon:'102.72485',lat:'17.88535'},{lon:'102.10687',lat:'18.24529'},{lon:'101.2417',lat:'17.79737'},{lon:'101.19775',lat:'16.10941'},{lon:'101.24719',lat:'14.21'}];
AddRaidPolygon(raid_mapLayer, V3UdonThani,"#7C3592","3 Udon Thani");

var V4Kanchanaburi = [{lon:'100.09363',lat:'11.53513'},{lon:'100.07166',lat:'14.21'},{lon:'98.72377',lat:'14.21'},{lon:'99.43719',lat:'11.5543'},{lon:'100.09363',lat:'11.53513'}];
AddRaidPolygon(raid_mapLayer, V4Kanchanaburi,"#7C3592","4 Kanchanaburi");

var V5Bangkok = [{lon:'100.0616',lat:'13.43104'},{lon:'100.80217',lat:'13.43104'},{lon:'100.80217',lat:'14.21'},{lon:'100.0616',lat:'14.21'},{lon:'100.0616',lat:'13.43104'}];
AddRaidPolygon(raid_mapLayer, V5Bangkok,"#DB4436","5 Bangkok");

var V6Pattaya = [{lon:'101.47516',lat:'12.50597'},{lon:'101.47516',lat:'14.21'},{lon:'100.73152',lat:'14.21'},{lon:'100.73152',lat:'12.50597'},{lon:'101.47516',lat:'12.50597'}];
AddRaidPolygon(raid_mapLayer, V6Pattaya,"#7C3592","6 Pattaya");

var V7NakhonRatchasima = [{lon:'103.06218',lat:'14.21399'},{lon:'101.47516',lat:'14.21'},{lon:'101.48059',lat:'13.47432'},{lon:'101.46136',lat:'12.1836'},{lon:'102.6287842',lat:'11.6845143'},{lon:'103.0379',lat:'11.60039'},{lon:'102.53689',lat:'13.48122'},{lon:'103.06218',lat:'14.21399'}];
AddRaidPolygon(raid_mapLayer, V7NakhonRatchasima,"#DB4436","7 Nakhon Ratchasima");

var V8SuratThani = [{lon:'96.6741',lat:'6.91956'},{lon:'100.36825',lat:'8.29163'},{lon:'100.14303',lat:'11.55555'},{lon:'99.28747',lat:'11.56631'},{lon:'96.6741',lat:'6.91956'}];
AddRaidPolygon(raid_mapLayer, V8SuratThani,"#DB4436","8 Surat Thani");

var V9HatYai = [{lon:'101.02632',lat:'6.88585'},{lon:'100.62531',lat:'8.3477'},{lon:'100.16615',lat:'8.2559'},{lon:'100.076',lat:'8.26888'},{lon:'98.72145',lat:'7.70375'},{lon:'99.14992',lat:'6.49774'},{lon:'100.18263',lat:'6.402229'},{lon:'100.14191',lat:'6.6813'},{lon:'100.1417541',lat:'6.7055262'},{lon:'101.12085',lat:'6.101'},{lon:'100.97116',lat:'5.79915'},{lon:'101.13458',lat:'5.59827'},{lon:'101.47516',lat:'5.87291'},{lon:'101.80201',lat:'5.65043'},{lon:'102.15356',lat:'6.20773'},{lon:'101.43396',lat:'7.01251'},{lon:'101.02632',lat:'6.88585'}];
AddRaidPolygon(raid_mapLayer, V9HatYai,"#7C3592","9 Hat Yai");

    setTimeout(function(){CurrentRaidLocation(raid_mapLayer);},3000);
    mro_Map.events.register("moveend", Waze.map, function(){ setTimeout(function(){CurrentRaidLocation(raid_mapLayer);},1500);});
    mro_Map.events.register("zoomend", Waze.map, function(){ setTimeout(function(){CurrentRaidLocation(raid_mapLayer);},1500);});
       
       
}