Advertisement
Guest User

Untitled

a guest
May 3rd, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Haxe 5.17 KB | None | 0 0
  1. package worldofconflict.components;
  2.  
  3. import google.maps.LatLng;
  4. import google.maps.Maps;
  5. import google.maps.Marker;
  6. import haxe.ui.core.Component;
  7.  
  8. class Map extends Component {
  9.     public static inline var MARKER_RED:String = "https://storage.googleapis.com/support-kms-prod/SNP_2752125_en_v0";
  10.     public static inline var MARKER_BLUE:String = "https://storage.googleapis.com/support-kms-prod/SNP_2752068_en_v0";
  11.     public static inline var MARKER_PINK:String = "https://storage.googleapis.com/support-kms-prod/SNP_2752264_en_v0";
  12.     public static inline var MARKER_YELLOW:String = "https://storage.googleapis.com/support-kms-prod/SNP_2752063_en_v0";
  13.     public static inline var MARKER_GREEN:String = "https://storage.googleapis.com/support-kms-prod/SNP_2752129_en_v0";
  14.    
  15.     private var _maps:google.maps.Maps;
  16.     private var _map:google.maps.Map;
  17.    
  18.     public function new() {
  19.         super();
  20.         addClass("map");
  21.     }
  22.    
  23.     private var _key:String;
  24.     public var key(get, set):String;
  25.     private function get_key():String {
  26.         return _key;
  27.     }
  28.     private function set_key(value:String):String {
  29.         _key = value;
  30.         _maps = new Maps(_key);
  31.        
  32.         var minZoomLevel = 2;
  33.        
  34.         _maps.init(function() {
  35.             var options:Dynamic = { };
  36.             options.zoom = 3;
  37.             options.mapTypeId = _mapType;
  38.             options.mapTypeControl = false;
  39.             options.streetViewControl = false;
  40.             if (_center != null) {
  41.                 options.center = _center;
  42.             } else {
  43.                 _center = new LatLng(35.88905007936091, -8.96484375);
  44.                 options.center = _center;
  45.             }
  46.            
  47.             _map = new google.maps.Map(this.element, options);
  48.             _map.addListener("click", function(e) {
  49.                 var mapEvent:MapEvent = new MapEvent(MapEvent.MAP_CLICKED);
  50.                 mapEvent.latlng = new LatLng(e.latLng.lat(), e.latLng.lng());
  51.                 dispatch(mapEvent);
  52.             });
  53.            
  54.             _map.addListener("center_changed", function() {
  55.                 var latNorth = _map.getBounds().getNorthEast().lat();
  56.                 var latSouth = _map.getBounds().getSouthWest().lat();
  57.                 var newLat = null;
  58.                
  59.                 if (latNorth < 85 && latSouth >-85) {
  60.                     return;
  61.                 } else {
  62.                     if (latNorth > 85 && latSouth <-85) {
  63.                         return;
  64.                     } else {
  65.                         if (latNorth > 85) {
  66.                             newLat =  _map.getCenter().lat() - (latNorth-85);
  67.                         }
  68.                         if (latSouth <-85) {
  69.                             newLat =  _map.getCenter().lat() - (latSouth+85);
  70.                         }
  71.                     }
  72.                 }
  73.                
  74.                 var newCenter = new LatLng( newLat , _map.getCenter().lng() );
  75.                 _map.setCenter(newCenter);
  76.                
  77.             });
  78.            
  79.             _map.addListener("zoom_changed", function() {
  80.                 if (_map.getZoom() < minZoomLevel) _map.setZoom(minZoomLevel);
  81.             });
  82.         });
  83.         return value;
  84.     }
  85.    
  86.     private var _mapType:String = "roadmap";
  87.     public var mapType(get, set):String;
  88.     private function get_mapType():String {
  89.         return _mapType;
  90.     }
  91.     private function set_mapType(value:String):String {
  92.         _mapType = value;
  93.         if (_map != null) {
  94.             _map.setMapTypeId(_mapType);
  95.         }
  96.         return value;
  97.     }
  98.    
  99.     private var _center:LatLng;
  100.     public var center(get, set):LatLng;
  101.     private function get_center():LatLng {
  102.         return _center;
  103.     }
  104.     private function set_center(value:LatLng):LatLng {
  105.         _center = value;
  106.         if (_map != null) {
  107.             _map.setCenter(value);
  108.         }
  109.         return value;
  110.     }
  111.    
  112.     public function addMarker(position:LatLng = null, icon:String = null, title:String = null):Marker {
  113.         var options:Dynamic = { };
  114.         options.map = _map;
  115.         if (position != null) {
  116.             options.position = position;
  117.         }
  118.         if (icon != null) {
  119.             options.icon = icon;
  120.         }
  121.         if (title != null) {
  122.             options.title = title;
  123.         }
  124.         var marker = new Marker(options);
  125.         return marker;
  126.     }
  127.    
  128.     public function removeMarker(marker:Marker) {
  129.         marker.setMap(null);
  130.     }
  131.    
  132.     public function panBy(x:Float, y:Float) {
  133.         _map.panBy(x, y);
  134.     }
  135.    
  136.     public function panTo(latlng:LatLng) {
  137.         _map.panTo(latlng);
  138.     }
  139.    
  140.     public var cursor(null, set):String;
  141.     private function set_cursor(value:String):String {
  142.         if (value == null) {
  143.             value = "";
  144.         }
  145.         _map.setOptions({draggableCursor: value});
  146.         return value;
  147.     }
  148.    
  149.     private static function parseLatLng(s:String):LatLng {
  150.         var p = s.split(",");
  151.         var lat = Std.parseFloat(StringTools.trim(p[0]));
  152.         var lng = Std.parseFloat(StringTools.trim(p[1]));
  153.         return new LatLng(lat, lng);
  154.     }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement