WME Thai Regions Overlay

Creates polygons for WME "Thai Regions Regions"

// ==UserScript==
// @name                WME Thai Regions Overlay
// @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==


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


//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.9617572',lat:'19.805365099999996'},{lon:'97.28618710000002',lat:'18.633208300000003'},{lon:'97.4372148',lat:'18.132883599999996'},{lon:'97.7862344',lat:'17.3851027'},{lon:'101.1605203',lat:'17.449844200000005'},{lon:'101.3864965',lat:'19.0522525'},{lon:'101.3010153',lat:'19.654267700000002'},{lon:'100.6515692',lat:'19.659402400000005'},{lon:'100.6144347',lat:'20.311073000000004'},{lon:'99.952611',lat:'20.595338699999996'},{lon:'98.1511627',lat:'19.9293108'},{lon:'97.9617572',lat:'19.805365099999996'}];
AddRaidPolygon(raid_mapLayer, V1ChiangMai,"#7C3592","1 Chiang Mai");

var V2Phitsanulok = [{lon:'98.1585294',lat:'14.7601395'},{lon:'101.4207939',lat:'14.737450799999998'},{lon:'101.1605203',lat:'17.449844200000005'},{lon:'97.7862344',lat:'17.3851027'},{lon:'98.5596333',lat:'16.4499527'},{lon:'98.4785569',lat:'15.464377100000004'},{lon:'98.1249216',lat:'15.2622496'},{lon:'98.1585294',lat:'14.7601395'}];
AddRaidPolygon(raid_mapLayer, V2Phitsanulok,"#DB4436","2 Phitsanulok");

var V3UdonThani = [{lon:'101.1605203',lat:'17.449844200000005'},{lon:'101.3317963',lat:'15.6732575'},{lon:'105.0681855',lat:'14.7226788'},{lon:'105.2063067',lat:'14.3440908'},{lon:'105.2065801',lat:'14.313747900000001'},{lon:'105.5245569',lat:'14.445575300000002'},{lon:'105.7021554',lat:'15.016740599999999'},{lon:'105.6568513',lat:'15.750560699999998'},{lon:'105.2750986',lat:'16.3228913'},{lon:'105.041573',lat:'16.621735'},{lon:'104.8537885',lat:'17.5076777'},{lon:'103.9347842',lat:'18.6172727'},{lon:'102.38870530000001',lat:'18.497428'},{lon:'101.7458044',lat:'18.085931900000002'},{lon:'101.184312',lat:'17.627965300000003'},{lon:'101.1605203',lat:'17.449844200000005'}];
AddRaidPolygon(raid_mapLayer, V3UdonThani,"#7C3592","3 Udon Thani");

var V4Kanchanaburi = [{lon:'98.1585294',lat:'14.7601395'},{lon:'99.0406499',lat:'13.0601131'},{lon:'99.4848735',lat:'11.8083785'},{lon:'99.0348816',lat:'11.057125200000002'},{lon:'100.2951678',lat:'11.0148409'},{lon:'100.244751',lat:'12.4338944'},{lon:'100.2135086',lat:'13.4321997'},{lon:'100.1972416',lat:'13.7156702'},{lon:'100.6181145',lat:'14.2644663'},{lon:'100.6096495',lat:'14.7473376'},{lon:'98.1585294',lat:'14.7601395'}];
AddRaidPolygon(raid_mapLayer, V4Kanchanaburi,"#7C3592","4 Kanchanaburi");

var V5Bangkok = [{lon:'100.6182861',lat:'14.263717600000001'},{lon:'100.1980591',lat:'13.7153719'},{lon:'100.2145385',lat:'13.432366599999998'},{lon:'100.7878875',lat:'13.4430521'},{lon:'100.7748413',lat:'13.9287356'},{lon:'100.6182861',lat:'14.263717600000001'}];
AddRaidPolygon(raid_mapLayer, V5Bangkok,"#DB4436","5 Bangkok");

var V6Pattaya = [{lon:'100.6096495',lat:'14.7473376'},{lon:'100.6196361',lat:'14.2639913'},{lon:'100.7752585',lat:'13.928937'},{lon:'100.7878875',lat:'13.4430521'},{lon:'100.6879377',lat:'13.4412573'},{lon:'100.5578613',lat:'12.785017899999998'},{lon:'100.8874512',lat:'12.4365765'},{lon:'101.4667152',lat:'12.422032900000001'},{lon:'101.5631104',lat:'13.806075399999997'},{lon:'101.4207939',lat:'14.737450799999998'},{lon:'100.6096495',lat:'14.7473376'}];
AddRaidPolygon(raid_mapLayer, V6Pattaya,"#7C3592","6 Pattaya");

var V7NakhonRatchasima = [{lon:'101.3317963',lat:'15.6732575'},{lon:'101.4207939',lat:'14.737450799999998'},{lon:'101.5659344',lat:'13.806621'},{lon:'101.4667152',lat:'12.422032900000001'},{lon:'102.5539278',lat:'11.5040205'},{lon:'102.8303984',lat:'11.533386899999998'},{lon:'102.9606993',lat:'11.6487313'},{lon:'102.80078400000001',lat:'12.431741899999999'},{lon:'102.5646647',lat:'12.9304945'},{lon:'102.7000921',lat:'13.5901806'},{lon:'103.1290886',lat:'14.174740599999998'},{lon:'105.1821666',lat:'14.201369300000001'},{lon:'105.2063441',lat:'14.313768699999999'},{lon:'105.2062772',lat:'14.3440752'},{lon:'105.0643988',lat:'14.7206527'},{lon:'101.3317963',lat:'15.6732575'}];
AddRaidPolygon(raid_mapLayer, V7NakhonRatchasima,"#DB4436","7 Nakhon Ratchasima");

var V8SuratThani = [{lon:'99.0348816',lat:'11.057125200000002'},{lon:'98.7621851',lat:'10.681651799999997'},{lon:'98.5087631',lat:'9.9713535'},{lon:'97.6361616',lat:'9.470163'},{lon:'97.4735079',lat:'6.4650827'},{lon:'98.62852780000001',lat:'6.461021700000001'},{lon:'99.8220175',lat:'6.484652999999999'},{lon:'100.0758501',lat:'6.4711106'},{lon:'100.0823173',lat:'6.534198700000001'},{lon:'99.95175950000001',lat:'7.1877962'},{lon:'99.2775566',lat:'8.9060362'},{lon:'99.7679595',lat:'9.3357435'},{lon:'100.8130801',lat:'9.399340799999997'},{lon:'100.2951678',lat:'11.0148409'},{lon:'99.0348816',lat:'11.057125200000002'}];
AddRaidPolygon(raid_mapLayer, V8SuratThani,"#DB4436","8 Surat Thani");

var V9HatYai = [{lon:'99.7680246',lat:'9.3355585'},{lon:'99.2775706',lat:'8.9060322'},{lon:'99.9556369',lat:'7.1895637'},{lon:'100.0833928',lat:'6.534426100000001'},{lon:'100.0717163',lat:'6.4135661'},{lon:'100.1184341',lat:'6.3987703'},{lon:'100.1624053',lat:'6.413994299999999'},{lon:'100.2169999',lat:'6.669299600000001'},{lon:'100.27865740000001',lat:'6.680055900000001'},{lon:'100.3238206',lat:'6.532573599999999'},{lon:'100.679309',lat:'6.4218793000000005'},{lon:'100.7847997',lat:'6.4038748'},{lon:'100.8380044',lat:'6.2139423'},{lon:'101.0092701',lat:'6.209639699999999'},{lon:'100.9662083',lat:'5.8011256'},{lon:'101.1427506',lat:'5.568914299999999'},{lon:'101.5602805',lat:'5.8599682'},{lon:'101.6797132',lat:'5.7285572'},{lon:'101.83538020000002',lat:'5.7081347'},{lon:'101.9453529',lat:'5.825560699999999'},{lon:'102.1131117',lat:'6.1369355'},{lon:'102.1643978',lat:'6.3689876000000005'},{lon:'101.5529611',lat:'6.889047799999999'},{lon:'100.766599',lat:'7.2319844'},{lon:'100.8165133',lat:'9.393243900000002'},{lon:'100.6567383',lat:'9.384032199999998'},{lon:'99.7680246',lat:'9.3355585'}];
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);});
       
       
}