├── .gitignore ├── README.md ├── composer.json ├── composer.lock └── src ├── LaravelMaps.php ├── LaravelMapsServiceProvider.php ├── Leaflet ├── Event │ ├── LeafletEvent.php │ └── LeafletResponseEvent.php ├── LeafletCircle.php ├── LeafletGeojson.php ├── LeafletMap.php ├── LeafletMarker.php ├── LeafletMethod.php ├── LeafletPolygon.php └── LeafletPopup.php ├── Mapbox ├── Event │ ├── MapboxEvent.php │ └── MapboxResponseEvent.php ├── MapboxMap.php ├── MapboxMarker.php ├── MapboxMethod.php └── MapboxPopup.php ├── RawJs.php └── resources ├── config └── laravel-maps.php └── views ├── render.blade.php ├── scripts.blade.php └── styles.blade.php /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Demo : https://laravel-maps-demo.herokuapp.com/ 2 | 3 | ### Installation 4 | 5 | ```bash 6 | composer require bagusindrayana/laravel-maps 7 | ``` 8 | 9 | Add LaravelMapServiceProvider::class to config/app.php 10 | ```php 11 | 'providers'=>[ 12 | //.... 13 | 14 | Bagusindrayana\LaravelMaps\LaravelMapsServiceProvider::class, 15 | 16 | //... 17 | ], 18 | 19 | ``` 20 | 21 | publish provider 22 | ```bash 23 | php artisan vendor:publish --provider=Bagusindrayana\LaravelMaps\LaravelMapsServiceProvider 24 | ``` 25 | 26 | ### Usage 27 | 28 | in controller 29 | 30 | ```php 31 | $map = LaravelMaps::leaflet('map') 32 | ->setView([51.505, -0.09], 13); 33 | 34 | return view('your-view',compact('map')); 35 | ``` 36 | 37 | in view 38 | ```html 39 | 40 | 41 | My Map 42 | {!! @$map->styles() !!} 43 | 44 | 45 | {!! @$map->render() !!} 46 | {!! @$map->scripts() !!} 47 | 48 | 49 | ``` 50 | 51 | 52 | 53 | ## Leaflet 54 | 55 | ### Features 56 | - marker 57 | - circle 58 | - polygon 59 | - geojson 60 | - basic event and method 61 | 62 | ### Basic Usage 63 | 64 | ```php 65 | //'map' is variable name will be use in javascript code 66 | $map = LaravelMaps::leaflet('map') 67 | ->setView([51.505, -0.09], 13) 68 | ->addMarker(function(LeafletMarker $marker){ 69 | return $marker 70 | ->latLng([51.5, -0.09]) 71 | ->bindPopup('Hello world!
I am a popup.'); 72 | }) 73 | ->addCircle(function(LeafletCircle $circle){ 74 | return $circle 75 | ->latLng([51.508, -0.11]) 76 | ->options([ 77 | 'radius'=>500, 78 | 'color'=>'red', 79 | 'fillColor'=>'#f03', 80 | 'fillOpacity'=>0.5 81 | ]) 82 | ->bindPopup("I am a circle."); 83 | }) 84 | ->addPolygon(function(LeafletPolygon $polygon){ 85 | return $polygon 86 | ->latLng([ 87 | [51.509, -0.08], 88 | [51.503, -0.06], 89 | [51.51, -0.047] 90 | ]) 91 | ->bindPopup("I am a polygon."); 92 | }) 93 | ->addPopup("I am a standalone popup.",[51.513, -0.09]); 94 | ``` 95 | 96 | ### Method & Event 97 | 98 | method are dynamic so you can use most method from original leaflet https://leafletjs.com/reference.html#map-method 99 | argument or parameter in method can be array,Closure,string,and RawJs class 100 | 101 | 102 | 103 | ## Mapbox 104 | 105 | ### Features 106 | - marker 107 | - geojson 108 | - basic event and method 109 | 110 | ### Basic Usage 111 | 112 | ```php 113 | //'map' is variable name will be use in javascript code 114 | $map = LaravelMaps::mapbox('map',[ 115 | "center"=>[106.827293,-6.174465], 116 | "zoom"=>13, 117 | ]); 118 | 119 | $map->on('load',function($m){ 120 | $m->addMarker(function(MapboxMarker $marker){ 121 | return $marker 122 | ->lngLat([51.5, -0.09]) 123 | ->setPopup('Hello world!
I am a popup.'); 124 | }); 125 | }); 126 | ``` 127 | 128 | ### Method & Event 129 | 130 | method are dynamic so you can use most method from original mapbox https://docs.mapbox.com/mapbox-gl-js/api/map/#map-instance-members 131 | argument or parameter in method can be array,Closure,string,and RawJs class 132 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "bagusindrayana/laravel-maps", 3 | "description": "set mapbox and leaflet map initiation from within laravel", 4 | "type": "library", 5 | "license": "MIT", 6 | "autoload": { 7 | "psr-4": { 8 | "Bagusindrayana\\LaravelMaps\\": "src/" 9 | } 10 | }, 11 | "authors": [ 12 | { 13 | "name": "Bagus Indrayana", 14 | "email": "bagusindrayanaindo@gmail.com" 15 | } 16 | ], 17 | "require": {}, 18 | "extra": { 19 | "laravel": { 20 | "providers": [ 21 | "Bagusindrayana\\LaravelMaps\\LaravelMapsServiceProvider" 22 | ] 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /composer.lock: -------------------------------------------------------------------------------- 1 | { 2 | "_readme": [ 3 | "This file locks the dependencies of your project to a known state", 4 | "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", 5 | "This file is @generated automatically" 6 | ], 7 | "content-hash": "2365d1e8871cd6f444c7a78bce414c48", 8 | "packages": [], 9 | "packages-dev": [], 10 | "aliases": [], 11 | "minimum-stability": "stable", 12 | "stability-flags": [], 13 | "prefer-stable": false, 14 | "prefer-lowest": false, 15 | "platform": [], 16 | "platform-dev": [], 17 | "plugin-api-version": "2.0.0" 18 | } 19 | -------------------------------------------------------------------------------- /src/LaravelMaps.php: -------------------------------------------------------------------------------- 1 | mergeConfigFrom(__DIR__.'/resources/config/laravel-maps.php', 'laravel-maps'); 17 | 18 | } 19 | 20 | /** 21 | * Bootstrap services. 22 | * 23 | * @return void 24 | */ 25 | public function boot() 26 | { 27 | $this->publishes([ 28 | __DIR__.'/resources/config/laravel-maps.php' => config_path('laravel-maps.php'), 29 | ]); 30 | 31 | $this->loadViewsFrom(__DIR__.'/resources/views', 'laravel-maps'); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/Leaflet/Event/LeafletEvent.php: -------------------------------------------------------------------------------- 1 | type = $type; 15 | $this->target = $target; 16 | 17 | $this->sourceTarget = $sourceTarget; 18 | $this->propagatedFrom = $propagatedFrom; 19 | } 20 | 21 | 22 | public function result($mapName = null) 23 | { 24 | $this->name = $mapName; 25 | $args = []; 26 | if(is_array($this->type)){ 27 | foreach ($this->type as $type) { 28 | $args[] = "`".$type."`"; 29 | } 30 | } else { 31 | $args[] = "`".$this->type."`"; 32 | } 33 | $this->codes .= " 34 | {$mapName}.on(".implode(",",$args).",function(){\r\n"; 35 | $this->generateComponent($mapName); 36 | $this->codes .= "});\r\n"; 37 | return $this->codes; 38 | } 39 | } -------------------------------------------------------------------------------- /src/Leaflet/Event/LeafletResponseEvent.php: -------------------------------------------------------------------------------- 1 | codes .= " 9 | {$mapName}.on('{$this->type}',function(e){\r\n"; 10 | $this->generateComponent($mapName); 11 | $this->codes .= "});\r\n"; 12 | return $this->codes; 13 | } 14 | } -------------------------------------------------------------------------------- /src/Leaflet/LeafletCircle.php: -------------------------------------------------------------------------------- 1 | latLng = $latLng; 15 | if($options){ 16 | if(isset($options['name'])){ 17 | $this->name = $options['name']; 18 | } 19 | $this->options = $options; 20 | } 21 | } 22 | 23 | public function name($name) 24 | { 25 | $this->name = $name; 26 | return $this; 27 | } 28 | 29 | public function latLng($latLng) 30 | { 31 | $this->latLng = $latLng; 32 | return $this; 33 | } 34 | 35 | public function options($options) 36 | { 37 | $this->options = $options; 38 | return $this; 39 | } 40 | 41 | public function addTo(LeafletMap $leafletMap) 42 | { 43 | $leafletMap->circle($this); 44 | return $this; 45 | 46 | } 47 | 48 | public function bindPopup($popup) 49 | { 50 | $p = null; 51 | if($popup instanceof LeafletPopup){ 52 | $p = $popup; 53 | } else { 54 | $p = new LeafletPopup($popup); 55 | } 56 | $p->bind = true; 57 | $this->components[] = $p; 58 | return $this; 59 | } 60 | 61 | public function generateComponent() 62 | { 63 | $circleName = $this->name; 64 | foreach ($this->components as $component) { 65 | if(is_string($component)){ 66 | $this->codes .= $component; 67 | } else { 68 | $this->codes .= $component->result($circleName); 69 | } 70 | 71 | } 72 | return $this->codes; 73 | } 74 | 75 | public function result($mapName = null) 76 | { 77 | if(is_string($this->latLng)){ 78 | $this->codes .= "var {$this->name} = L.circle({$this->latLng}".($this->options?",".json_encode($this->options):"").");\r\n"; 79 | } else { 80 | $this->codes .= "var {$this->name} = L.circle(".json_encode($this->latLng).($this->options?",".json_encode($this->options):"").");\r\n"; 81 | } 82 | if($mapName){ 83 | $this->codes .= "{$this->name}.addTo({$mapName});\r\n"; 84 | } 85 | $this->generateComponent(); 86 | 87 | return $this->codes; 88 | } 89 | 90 | 91 | } -------------------------------------------------------------------------------- /src/Leaflet/LeafletGeojson.php: -------------------------------------------------------------------------------- 1 | feature = $feature; 18 | if($options){ 19 | if(isset($options['name'])){ 20 | $this->name = $options['name']; 21 | } 22 | $this->options = $options; 23 | } 24 | } 25 | 26 | public function name($name) 27 | { 28 | $this->name = $name; 29 | return $this; 30 | } 31 | 32 | public function feature($feature) 33 | { 34 | $this->feature = $feature; 35 | return $this; 36 | } 37 | 38 | public function options($options) 39 | { 40 | $this->options = $options; 41 | return $this; 42 | } 43 | 44 | public function addTo(LeafletMap $leafletMap) 45 | { 46 | $leafletMap->geojson($this); 47 | return $this; 48 | 49 | } 50 | 51 | public function bindPopup($popup) 52 | { 53 | $p = null; 54 | if($popup instanceof LeafletPopup){ 55 | $p = $popup; 56 | } else { 57 | $p = new LeafletPopup($popup); 58 | } 59 | $p->bind = true; 60 | $this->components[] = $p; 61 | return $this; 62 | } 63 | 64 | public function generateComponent() 65 | { 66 | $geojsonName = $this->name; 67 | foreach ($this->components as $component) { 68 | if(is_string($component)){ 69 | $this->codes .= $component; 70 | } else { 71 | $this->codes .= $component->result($geojsonName); 72 | } 73 | 74 | } 75 | return $this->codes; 76 | } 77 | 78 | public function result($mapName = null) 79 | { 80 | foreach ($this->options as $key => $option) { 81 | if($option instanceof RawJs){ 82 | $this->options[$key] = $option->result(); 83 | } 84 | } 85 | 86 | $jsonOptions = trim(preg_replace('/\s\s+|\s$/'," ",json_encode($this->options))); 87 | $string = str_replace(array('\n', "\r"), '', $jsonOptions); 88 | $string2 = str_replace(array('\n', "\r"), '', trim(preg_replace('/\s+/'," ",'"'.$this->options['onEachFeature'].'"'))); 89 | 90 | foreach ($this->options as $key => $option) { 91 | if(in_array($key,$this->optionMethods)){ 92 | 93 | $jsonOptions = str_replace($string2,$this->options[$key],$string); 94 | } 95 | } 96 | 97 | 98 | if(is_string($this->feature)){ 99 | $this->codes .= "var {$this->name} = L.geoJSON({$this->feature}".($this->options?",$jsonOptions":"").");\r\n"; 100 | } else { 101 | $this->codes .= "var {$this->name} = L.geoJSON(".json_encode($this->feature).($this->options?",$jsonOptions":"").");\r\n"; 102 | } 103 | if($mapName){ 104 | $this->codes .= "{$this->name}.addTo({$mapName});\r\n"; 105 | } 106 | $this->generateComponent(); 107 | 108 | return $this->codes; 109 | } 110 | 111 | 112 | } -------------------------------------------------------------------------------- /src/Leaflet/LeafletMap.php: -------------------------------------------------------------------------------- 1 | init($name, $options); 18 | 19 | } 20 | 21 | public function init($name = null, $options = null) 22 | { 23 | $this->css = config("laravel-maps.leaflet.css") ?? $this->css; 24 | $this->js = config("laravel-maps.leaflet.js") ?? $this->js; 25 | $this->name = $name ?? $this->name; 26 | $this->options = $options ?? $this->options; 27 | return $this; 28 | } 29 | 30 | public function tileLayer($tileLayer) 31 | { 32 | $this->tileLayer = $tileLayer; 33 | return $this; 34 | } 35 | 36 | public function latLng($latLng) 37 | { 38 | $this->latLng = $latLng; 39 | return $this; 40 | } 41 | 42 | public function css($link) 43 | { 44 | $this->css = is_array($link)?$link:[$link]; 45 | return $this; 46 | } 47 | 48 | public function js($link) 49 | { 50 | $this->js = is_array($link)?$link:[$link]; 51 | return $this; 52 | } 53 | 54 | public function name($name) 55 | { 56 | $this->name = $name; 57 | return $this; 58 | } 59 | 60 | public function options($options) 61 | { 62 | $this->options = $options; 63 | return $this; 64 | } 65 | 66 | public function map($name,$options = null) 67 | { 68 | $this->name($name); 69 | if($options != null) 70 | { 71 | $this->options($options); 72 | } 73 | return $this; 74 | } 75 | 76 | 77 | public function styles() 78 | { 79 | return view("laravel-maps::styles",[ 80 | "styles" => is_array($this->css)?$this->css:[$this->css], 81 | ]); 82 | 83 | } 84 | 85 | public function scripts() 86 | { 87 | $this->result(); 88 | return view("laravel-maps::scripts",[ 89 | "scripts" => is_array($this->js)?$this->js:[$this->js], 90 | "codes"=>$this->codes 91 | ]); 92 | 93 | } 94 | 95 | public function result($mapName = null) 96 | { 97 | $this->codes = " 98 | "; 111 | 112 | return $this->codes; 113 | } 114 | 115 | public function render() 116 | { 117 | $this->elemen = "
"; 118 | return view("laravel-maps::render",[ 119 | "elemen"=>$this->elemen, 120 | 121 | ]); 122 | 123 | } 124 | 125 | 126 | } -------------------------------------------------------------------------------- /src/Leaflet/LeafletMarker.php: -------------------------------------------------------------------------------- 1 | latLng = $latLng; 15 | if($options){ 16 | if(isset($options['name'])){ 17 | $this->name = $options['name']; 18 | } 19 | $this->options = $options; 20 | } 21 | } 22 | 23 | public function name($name) 24 | { 25 | $this->name = $name; 26 | return $this; 27 | } 28 | 29 | public function latLng($latLng) 30 | { 31 | $this->latLng = $latLng; 32 | return $this; 33 | } 34 | 35 | public function options($options) 36 | { 37 | if(isset($this->options) && count($this->options) > 0){ 38 | $this->options = array_merge($this->options,$options); 39 | } else { 40 | $this->options = $options; 41 | } 42 | return $this; 43 | } 44 | 45 | public function addTo(LeafletMap $leafletMap) 46 | { 47 | $leafletMap->marker($this); 48 | return $this; 49 | 50 | } 51 | 52 | public function bindPopup($popup) 53 | { 54 | $p = null; 55 | if($popup instanceof LeafletPopup){ 56 | $p = $popup; 57 | } else { 58 | $p = new LeafletPopup($popup); 59 | } 60 | $p->bind = true; 61 | $this->components[] = $p; 62 | return $this; 63 | } 64 | 65 | public function icon($options) 66 | { 67 | $this->codes .= "var ".$this->name."Icon = L.icon(".json_encode($options).");\r\n"; 68 | $this->options(['icon' => $this->name."Icon"]); 69 | return $this; 70 | } 71 | 72 | public function generateComponent() 73 | { 74 | $mapName = $this->name; 75 | foreach ($this->components as $component) { 76 | if(is_string($component)){ 77 | $this->codes .= $component; 78 | } else { 79 | $this->codes .= $component->result($mapName); 80 | } 81 | 82 | } 83 | return $this->codes; 84 | } 85 | 86 | public function result($mapName) 87 | { 88 | if(is_string($this->latLng)){ 89 | $this->codes .= "var {$this->name} = L.marker({$this->latLng}".($this->options?",".json_encode($this->options):"").");\r\n"; 90 | } else { 91 | $this->codes .= "var {$this->name} = L.marker(".json_encode($this->latLng).($this->options?",".json_encode($this->options):"").");\r\n"; 92 | } 93 | if(isset($this->options['icon'])){ 94 | $this->codes .= "{$this->name}.setIcon(".$this->options['icon'].");\r\n"; 95 | } 96 | if($mapName){ 97 | $this->codes .= "{$this->name}.addTo({$mapName});\r\n"; 98 | } 99 | $this->generateComponent(); 100 | 101 | return $this->codes; 102 | } 103 | 104 | 105 | } -------------------------------------------------------------------------------- /src/Leaflet/LeafletMethod.php: -------------------------------------------------------------------------------- 1 | [ 17 | 'unload', 18 | 'viewreset', 19 | 'load', 20 | 'zoomstart', 21 | 'movestart', 22 | 'zoom', 23 | 'move', 24 | 'zoomend', 25 | 'moveend', 26 | ], 27 | "\Bagusindrayana\LaravelMaps\Leaflet\Event\LeafletResponseEvent"=>[ 28 | 'click', 29 | 'dblclick', 30 | 'mousedown', 31 | 'mouseup', 32 | 'mouseover', 33 | 'mouseout', 34 | 'mousemove', 35 | 'contextmenu', 36 | 'locationfound', 37 | 'locationerror' 38 | ], 39 | ]; 40 | 41 | public function __call($method, $args) 42 | { 43 | foreach ($args as $index => $arg) { 44 | if(is_array($arg)){ 45 | $args[$index] = json_encode($arg); 46 | } else if($arg instanceof Closure){ 47 | $args[$index] = $arg($this); 48 | } else if(is_string($arg)){ 49 | $args[$index] = "`".$arg."`"; 50 | } else if($arg instanceof RawJs){ 51 | $args[$index] = $arg->result(); 52 | } 53 | } 54 | $this->components[] = $this->name.".$method(".implode(',',$args).");\r\n"; 55 | return $this; 56 | } 57 | 58 | 59 | public function name($name) 60 | { 61 | $this->name = $name; 62 | return $this; 63 | } 64 | 65 | public function marker($latLng) 66 | { 67 | if($latLng instanceof LeafletMarker){ 68 | $this->addMarker($latLng); 69 | } else { 70 | if(is_array($latLng)){ 71 | if(count($latLng) == 2 && isset($latLng[0]) && isset($latLng[1]) && !is_array($latLng[0]) && !is_array($latLng[1])){ 72 | $this->addMarker($latLng); 73 | } else { 74 | foreach ($latLng as $key => $arr) { 75 | if(!is_string($key)){ 76 | $this->addMarker($arr); 77 | } else if($arr instanceof LeafletMarker){ 78 | $this->addMarker($arr); 79 | } else { 80 | $marker = new LeafletMarker($arr); 81 | $marker->name($key); 82 | $this->addMarker($marker); 83 | } 84 | 85 | } 86 | } 87 | } 88 | 89 | } 90 | 91 | return $this; 92 | } 93 | 94 | public function addMarker($marker,$options = null) 95 | { 96 | if(!is_array($marker)){ 97 | try { 98 | $reflection = new ReflectionFunction($marker); 99 | } catch (\Throwable $th) { 100 | //throw $th; 101 | } 102 | } 103 | if(isset($reflection) && $reflection->isClosure()){ 104 | $m = new LeafletMarker([]); 105 | $this->components[] = $marker($m); 106 | } else if($marker instanceof LeafletMarker){ 107 | $this->components[] = $marker; 108 | } else { 109 | $marker = new LeafletMarker($marker,$options); 110 | $this->components[] = $marker; 111 | } 112 | return $this; 113 | } 114 | 115 | public function circle($latLng,$options = null) 116 | { 117 | if($latLng instanceof LeafletCircle){ 118 | $this->addCircle($latLng); 119 | } else { 120 | if(is_array($latLng)){ 121 | if(count($latLng) == 2 && isset($latLng[0]) && isset($latLng[1]) && !is_array($latLng[0]) && !is_array($latLng[1])){ 122 | $this->addCircle($latLng); 123 | } else { 124 | foreach ($latLng as $key => $arr) { 125 | if(!is_string($key)){ 126 | $this->addCircle($arr); 127 | } else if($arr instanceof LeafletCircle){ 128 | $this->addCircle($arr); 129 | } else { 130 | $circle = new LeafletCircle($arr); 131 | $circle->name($key); 132 | $this->addCircle($circle); 133 | } 134 | 135 | } 136 | } 137 | } 138 | 139 | } 140 | 141 | return $this; 142 | } 143 | 144 | public function addCircle($circle,$options = null) 145 | { 146 | 147 | $reflection = new ReflectionFunction($circle); 148 | if($reflection->isClosure()){ 149 | $m = new LeafletCircle([]); 150 | $this->components[] = $circle($m); 151 | } else if($circle instanceof LeafletCircle){ 152 | $this->components[] = $circle; 153 | } else { 154 | $circle = new LeafletCircle($circle,$options); 155 | $this->components[] = $circle; 156 | } 157 | return $this; 158 | } 159 | 160 | 161 | public function polygon($latLng) 162 | { 163 | if($latLng instanceof LeafletPolygon){ 164 | $this->addPolygon($latLng); 165 | } else { 166 | if(is_array($latLng)){ 167 | if(count($latLng) == 2 && isset($latLng[0]) && isset($latLng[1]) && !is_array($latLng[0]) && !is_array($latLng[1])){ 168 | $this->addPolygon($latLng); 169 | } else { 170 | foreach ($latLng as $key => $arr) { 171 | if(!is_string($key)){ 172 | $this->addPolygon($arr); 173 | } else if($arr instanceof LeafletPolygon){ 174 | $this->addPolygon($arr); 175 | } else { 176 | $polygon = new LeafletPolygon($arr); 177 | $polygon->name($key); 178 | $this->addPolygon($polygon); 179 | } 180 | 181 | } 182 | } 183 | } 184 | 185 | } 186 | 187 | return $this; 188 | } 189 | 190 | public function addPolygon($polygon,$options = null) 191 | { 192 | 193 | if(!is_array($polygon)){ 194 | try { 195 | $reflection = new ReflectionFunction($polygon); 196 | } catch (\Throwable $th) { 197 | //throw $th; 198 | } 199 | } 200 | if(isset($reflection) && $reflection->isClosure()){ 201 | $m = new LeafletPolygon([]); 202 | $this->components[] = $polygon($m); 203 | } else if($polygon instanceof LeafletPolygon){ 204 | $this->components[] = $polygon; 205 | } else { 206 | $polygon = new LeafletPolygon($polygon,$options); 207 | $this->components[] = $polygon; 208 | } 209 | return $this; 210 | } 211 | 212 | public function geojson($latLng) 213 | { 214 | if($latLng instanceof LeafletGeojson){ 215 | $this->addGeojson($latLng); 216 | } else { 217 | if(is_array($latLng)){ 218 | if(count($latLng) == 2 && isset($latLng[0]) && isset($latLng[1]) && !is_array($latLng[0]) && !is_array($latLng[1])){ 219 | $this->addGeojson($latLng); 220 | } else { 221 | foreach ($latLng as $key => $arr) { 222 | if(!is_string($key)){ 223 | $this->addGeojson($arr); 224 | } else if($arr instanceof LeafletGeojson){ 225 | $this->addGeojson($arr); 226 | } else { 227 | $geojson = new LeafletGeojson($arr); 228 | $geojson->name($key); 229 | $this->addGeojson($geojson); 230 | } 231 | 232 | } 233 | } 234 | } 235 | 236 | } 237 | 238 | return $this; 239 | } 240 | 241 | public function addGeojson($geojson,$options = null) 242 | { 243 | 244 | if(!is_array($geojson)){ 245 | try { 246 | $reflection = new ReflectionFunction($geojson); 247 | } catch (\Throwable $th) { 248 | //throw $th; 249 | } 250 | } 251 | if(isset($reflection) && $reflection->isClosure()){ 252 | $m = new LeafletGeojson([]); 253 | $this->components[] = $geojson($m); 254 | } else if($geojson instanceof LeafletGeojson){ 255 | $this->components[] = $geojson; 256 | } else { 257 | $geojson = new LeafletGeojson($geojson,$options); 258 | $this->components[] = $geojson; 259 | } 260 | return $this; 261 | } 262 | 263 | public function addPopup($contents,$latLng) 264 | { 265 | if($contents instanceof LeafletPopup){ 266 | $this->components[] = $contents; 267 | } else { 268 | $popup = new LeafletPopup(); 269 | $popup->latLng($latLng) 270 | ->contents($contents) 271 | ->openOn($this); 272 | } 273 | return $this; 274 | } 275 | 276 | public function on($eventName,$fun = null) 277 | { 278 | if(is_object($eventName)){ 279 | $this->components[] = $eventName; 280 | } else { 281 | foreach ($this->eventType as $key => $value) { 282 | foreach ($value as $en) { 283 | if($eventName == $en){ 284 | $eventName = new $key($eventName); 285 | $this->components[] = $eventName; 286 | } 287 | } 288 | } 289 | 290 | } 291 | if(!is_object($eventName)){ 292 | $eventName = new LeafletEvent($eventName); 293 | $this->components[] = $eventName; 294 | } 295 | $eventName->name($this->name); 296 | $fun($eventName); 297 | return $this; 298 | 299 | } 300 | 301 | public function rawJs($js) 302 | { 303 | if($js instanceof RawJs){ 304 | $this->components[] = $js; 305 | } else { 306 | $js = new RawJs($js); 307 | $this->components[] = $js; 308 | } 309 | return $this; 310 | } 311 | 312 | public function setMethod($name,$arg = null,$options = null) 313 | { 314 | $argFormat = null; 315 | if(is_array($arg)){ 316 | $argFormat = json_encode($arg); 317 | } else { 318 | $argFormat = $arg; 319 | } 320 | if($argFormat){ 321 | $this->components[] = "{$this->name}.{$name}($argFormat".($options?",".json_encode($options):"").");\r\n"; 322 | } else { 323 | $this->components[] = "{$this->name}.{$name}(".($options?json_encode($options):"").");\r\n"; 324 | } 325 | return $this; 326 | } 327 | 328 | // public function setView($latLng,$zoom = null) 329 | // { 330 | // $this->latLng = $latLng; 331 | // if($zoom){ 332 | // $this->setZoom($zoom); 333 | // } 334 | // return $this; 335 | // } 336 | 337 | // public function setZoom($zoom) 338 | // { 339 | // $this->zoom = $zoom; 340 | // return $this; 341 | // } 342 | 343 | // public function zoomIn($args,$options = null) 344 | // { 345 | // $this->setMethod("zoomIn",$args,$options); 346 | // return $this; 347 | // } 348 | 349 | // public function zoomOut($args,$options = null) 350 | // { 351 | // $this->setMethod("zoomOut",$args,$options); 352 | // return $this; 353 | // } 354 | 355 | // public function setZoomAround($args,$options = null) 356 | // { 357 | // $this->setMethod("setZoomAround",$args,$options); 358 | // return $this; 359 | // } 360 | 361 | // public function fitBounds($args,$options = null) 362 | // { 363 | // $this->setMethod("fitBounds",$args,$options); 364 | // return $this; 365 | // } 366 | 367 | // public function fitWorld($options = null) 368 | // { 369 | // $this->setMethod("fitWorld",null,$options); 370 | // return $this; 371 | // } 372 | 373 | // public function panTo($args,$options = null) 374 | // { 375 | // $this->setMethod("panTo",$args,$options); 376 | // return $this; 377 | // } 378 | 379 | // public function panBy($args,$options = null) 380 | // { 381 | // $this->setMethod("panBy",$args,$options); 382 | // return $this; 383 | // } 384 | 385 | // public function flyTo($args,$options = null) 386 | // { 387 | // $this->setMethod("flyTo",$args,$options); 388 | // return $this; 389 | // } 390 | 391 | // public function flyToBounds($args,$options = null) 392 | // { 393 | // $this->setMethod("flyToBounds",$args,$options); 394 | // return $this; 395 | // } 396 | 397 | // public function locate($options = null) 398 | // { 399 | // $this->setMethod('locate',null,$options); 400 | // return $this; 401 | // } 402 | 403 | 404 | public function generateComponent() 405 | { 406 | $mapName = $this->name; 407 | foreach ($this->components as $component) { 408 | if(is_string($component)){ 409 | $this->codes .= $component; 410 | } else { 411 | $this->codes .= $component->result($mapName); 412 | } 413 | 414 | } 415 | return $this->codes; 416 | } 417 | 418 | public function getComponents() 419 | { 420 | return $this->components; 421 | } 422 | 423 | 424 | public function result($mapName = null) 425 | { 426 | $this->generateComponent(); 427 | return $this->codes; 428 | } 429 | } -------------------------------------------------------------------------------- /src/Leaflet/LeafletPolygon.php: -------------------------------------------------------------------------------- 1 | latLng = $latLng; 15 | if($options){ 16 | if(isset($options['name'])){ 17 | $this->name = $options['name']; 18 | } 19 | $this->options = $options; 20 | } 21 | } 22 | 23 | public function name($name) 24 | { 25 | $this->name = $name; 26 | return $this; 27 | } 28 | 29 | public function latLng($latLng) 30 | { 31 | $this->latLng = $latLng; 32 | return $this; 33 | } 34 | 35 | public function options($options) 36 | { 37 | $this->options = $options; 38 | return $this; 39 | } 40 | 41 | public function addTo(LeafletMap $leafletMap) 42 | { 43 | $leafletMap->polygon($this); 44 | return $this; 45 | 46 | } 47 | 48 | public function bindPopup($popup) 49 | { 50 | $p = null; 51 | if($popup instanceof LeafletPopup){ 52 | $p = $popup; 53 | } else { 54 | $p = new LeafletPopup($popup); 55 | } 56 | $p->bind = true; 57 | $this->components[] = $p; 58 | return $this; 59 | } 60 | 61 | public function generateComponent() 62 | { 63 | $polygonName = $this->name; 64 | foreach ($this->components as $component) { 65 | if(is_string($component)){ 66 | $this->codes .= $component; 67 | } else { 68 | $this->codes .= $component->result($polygonName); 69 | } 70 | 71 | } 72 | return $this->codes; 73 | } 74 | 75 | public function result($mapName = null) 76 | { 77 | if(is_string($this->latLng)){ 78 | $this->codes .= "var {$this->name} = L.polygon({$this->latLng}".($this->options?",".json_encode($this->options):"").");\r\n"; 79 | } else { 80 | $this->codes .= "var {$this->name} = L.polygon(".json_encode($this->latLng).($this->options?",".json_encode($this->options):"").");\r\n"; 81 | } 82 | if($mapName){ 83 | $this->codes .= "{$this->name}.addTo({$mapName});\r\n"; 84 | } 85 | $this->generateComponent(); 86 | 87 | return $this->codes; 88 | } 89 | 90 | 91 | } -------------------------------------------------------------------------------- /src/Leaflet/LeafletPopup.php: -------------------------------------------------------------------------------- 1 | contents($contents); 20 | } 21 | } 22 | 23 | public function name($name) 24 | { 25 | $this->name = $name; 26 | return $this; 27 | } 28 | 29 | public function setLatLng($latLng) 30 | { 31 | $this->latLng = $latLng; 32 | return $this; 33 | } 34 | 35 | public function latLng($latLng) 36 | { 37 | $this->latLng = $latLng; 38 | return $this; 39 | } 40 | 41 | public function setContent($contents) 42 | { 43 | return $this->contents($contents); 44 | } 45 | 46 | public function openPopup() 47 | { 48 | $this->openPopup = true; 49 | return $this; 50 | } 51 | 52 | public function openOn($map) 53 | { 54 | if(is_object($map)){ 55 | $map->components[] = $this; 56 | $this->openOn = $map->name; 57 | } else if(is_string($map)) { 58 | $this->openOn = $map; 59 | } 60 | return $this; 61 | } 62 | 63 | 64 | public function contents($contents) 65 | { 66 | $this->contents = ($contents instanceof RawJs)?$contents->result():"`$contents`"; 67 | return $this; 68 | } 69 | 70 | public function result($objectName = null) 71 | { 72 | $this->codes .= "var {$this->name} = L.popup().setContent(".$this->contents.");\r\n"; 73 | if($this->bind){ 74 | $this->codes .= "{$objectName}.bindPopup(".$this->name.");\r\n"; 75 | } 76 | if($this->latLng){ 77 | $this->codes .= "{$this->name}.setLatLng(".json_encode($this->latLng).");\r\n"; 78 | } 79 | if($this->openPopup){ 80 | $this->codes .= "{$this->name}.openPopup();\r\n"; 81 | } 82 | if($this->openOn){ 83 | $this->codes .= "{$this->name}.openOn({$this->openOn});\r\n"; 84 | } 85 | return $this->codes; 86 | } 87 | 88 | 89 | } -------------------------------------------------------------------------------- /src/Mapbox/Event/MapboxEvent.php: -------------------------------------------------------------------------------- 1 | type = $type; 15 | $this->target = $target; 16 | 17 | $this->sourceTarget = $sourceTarget; 18 | $this->propagatedFrom = $propagatedFrom; 19 | } 20 | 21 | 22 | public function result($mapName = null) 23 | { 24 | $this->name = $mapName; 25 | $args = []; 26 | if(is_array($this->type)){ 27 | foreach ($this->type as $type) { 28 | $args[] = "`".$type."`"; 29 | } 30 | } else { 31 | $args[] = "`".$this->type."`"; 32 | } 33 | $this->codes .= " 34 | {$mapName}.on(".implode(",",$args).",function(){\r\n"; 35 | $this->generateComponent($mapName); 36 | $this->codes .= "});\r\n"; 37 | return $this->codes; 38 | } 39 | } -------------------------------------------------------------------------------- /src/Mapbox/Event/MapboxResponseEvent.php: -------------------------------------------------------------------------------- 1 | type)){ 10 | foreach ($this->type as $type) { 11 | $args[] = "`".$type."`"; 12 | } 13 | } else { 14 | $args[] = "`".$this->type."`"; 15 | } 16 | $this->codes .= " 17 | {$mapName}.on(".implode(",",$args).",function(e){\r\n"; 18 | $this->generateComponent($mapName); 19 | $this->codes .= "});\r\n"; 20 | return $this->codes; 21 | } 22 | } -------------------------------------------------------------------------------- /src/Mapbox/MapboxMap.php: -------------------------------------------------------------------------------- 1 | init($name, $options); 20 | } 21 | 22 | public function init($name = null, $options = null) 23 | { 24 | 25 | $this->css = config("laravel-maps.mapbox.css") ?? $this->css; 26 | $this->js = config("laravel-maps.mapbox.js") ?? $this->js; 27 | if(is_array($name)){ 28 | $this->options = $options ?? $this->options; 29 | } else { 30 | $this->name = $name ?? $this->name; 31 | $this->options = $options ?? $this->options; 32 | } 33 | return $this; 34 | } 35 | 36 | 37 | public function styles() 38 | { 39 | return view("laravel-maps::styles",[ 40 | "styles" => is_array($this->css)?$this->css:[$this->css], 41 | ]); 42 | 43 | } 44 | 45 | public function scripts() 46 | { 47 | $this->result(); 48 | return view("laravel-maps::scripts",[ 49 | "scripts" => is_array($this->js)?$this->js:[$this->js], 50 | "codes"=>$this->codes 51 | ]); 52 | 53 | } 54 | 55 | 56 | public function result($mapName = null) 57 | { 58 | if(!isset($this->options["style"])){ 59 | $this->options["style"] = $this->style; 60 | } 61 | if(!isset($this->options["container"])){ 62 | $this->options["container"] = $this->name; 63 | } 64 | $this->codes = " 65 | "; 71 | 72 | return $this->codes; 73 | } 74 | 75 | public function render() 76 | { 77 | $this->elemen = "
"; 78 | return view("laravel-maps::render",[ 79 | "elemen"=>$this->elemen, 80 | 81 | ]); 82 | 83 | } 84 | } -------------------------------------------------------------------------------- /src/Mapbox/MapboxMarker.php: -------------------------------------------------------------------------------- 1 | lngLat = $lngLat; 15 | if($options){ 16 | $this->options = $options; 17 | } 18 | } 19 | 20 | public function name($name) 21 | { 22 | $this->name = $name; 23 | return $this; 24 | } 25 | 26 | public function setLngLat($lngLat) 27 | { 28 | return $this->lngLat($lngLat); 29 | } 30 | 31 | public function lngLat($lngLat) 32 | { 33 | $this->lngLat = $lngLat; 34 | return $this; 35 | } 36 | 37 | public function options($options) 38 | { 39 | $this->options = $options; 40 | return $this; 41 | } 42 | 43 | public function addTo(MapboxMap $mapboxMap) 44 | { 45 | $mapboxMap->marker($this); 46 | return $this; 47 | 48 | } 49 | 50 | public function setPopup($popup) 51 | { 52 | if($popup instanceof MapboxPopup){ 53 | $popup->attachMarker = true; 54 | $this->components[] = $popup; 55 | } else { 56 | $p = new MapboxPopup($popup); 57 | $p->attachMarker = true; 58 | $this->components[] = $p; 59 | } 60 | return $this; 61 | } 62 | 63 | 64 | 65 | 66 | public function generateComponent() 67 | { 68 | $markerName = $this->name; 69 | foreach ($this->components as $component) { 70 | if(is_string($component)){ 71 | $this->codes .= $component; 72 | } else { 73 | $this->codes .= $component->result($markerName); 74 | } 75 | 76 | } 77 | return $this->codes; 78 | } 79 | 80 | public function result($mapName = null) 81 | { 82 | if(is_string($this->lngLat)){ 83 | $this->codes .= "var {$this->name} = new mapboxgl.Marker(".($this->options?json_encode($this->options):"").").setLngLat(".$this->lngLat.");\r\n"; 84 | } else { 85 | $this->codes .= "var {$this->name} = new mapboxgl.Marker(".($this->options?json_encode($this->options):"").").setLngLat(".json_encode($this->lngLat).");\r\n"; 86 | } 87 | if($mapName){ 88 | $this->codes .= "{$this->name}.addTo({$mapName});\r\n"; 89 | } 90 | $this->generateComponent(); 91 | 92 | return $this->codes; 93 | } 94 | 95 | 96 | } -------------------------------------------------------------------------------- /src/Mapbox/MapboxMethod.php: -------------------------------------------------------------------------------- 1 | [ 20 | 'load', 21 | 'viewreset', 22 | 'load', 23 | 'zoomstart', 24 | 'movestart', 25 | 'zoom', 26 | 'move', 27 | 'zoomend', 28 | 'moveend', 29 | ], 30 | "\Bagusindrayana\LaravelMaps\Mapbox\Event\MapboxResponseEvent"=>[ 31 | 'click', 32 | 'dblclick', 33 | 'mousedown', 34 | 'mouseup', 35 | 'mouseover', 36 | 'mouseout', 37 | 'mousemove', 38 | 'contextmenu', 39 | ], 40 | ]; 41 | 42 | public function __construct($name = null, $options = null) 43 | { 44 | 45 | $this->init($name, $options); 46 | } 47 | 48 | public function init($name = null, $options = null) 49 | { 50 | 51 | if(is_array($name)){ 52 | $this->options = $options ?? $this->options; 53 | } else { 54 | $this->name = $name ?? $this->name; 55 | $this->options = $options ?? $this->options; 56 | } 57 | return $this; 58 | } 59 | 60 | public function name($name) 61 | { 62 | $this->name = $name; 63 | return $this; 64 | } 65 | 66 | public function marker($latLng) 67 | { 68 | if($latLng instanceof MapboxMarker){ 69 | $this->addMarker($latLng); 70 | } else { 71 | if(is_array($latLng)){ 72 | if(count($latLng) == 2 && isset($latLng[0]) && isset($latLng[1]) && !is_array($latLng[0]) && !is_array($latLng[1])){ 73 | $this->addMarker($latLng); 74 | } else { 75 | foreach ($latLng as $key => $arr) { 76 | if(!is_string($key)){ 77 | $this->addMarker($arr); 78 | } else if($arr instanceof MapboxMarker){ 79 | $this->addMarker($arr); 80 | } else { 81 | $marker = new MapboxMarker($arr); 82 | $marker->name($key); 83 | $this->addMarker($marker); 84 | } 85 | 86 | } 87 | } 88 | } 89 | 90 | } 91 | 92 | return $this; 93 | } 94 | 95 | public function addMarker($marker,$options = null) 96 | { 97 | if(!is_array($marker)){ 98 | try { 99 | $reflection = new ReflectionFunction($marker); 100 | } catch (\Throwable $th) { 101 | //throw $th; 102 | } 103 | } 104 | if(isset($reflection) && $reflection->isClosure()){ 105 | $m = new MapboxMarker([]); 106 | $this->components[] = $marker($m); 107 | } else if($marker instanceof MapboxMarker){ 108 | $this->components[] = $marker; 109 | } else { 110 | $marker = new MapboxMarker($marker,$options); 111 | $this->components[] = $marker; 112 | } 113 | return $this; 114 | } 115 | 116 | public function addPopup($contents,$lngLat) 117 | { 118 | if($contents instanceof MapboxPopup){ 119 | $this->components[] = $contents; 120 | } else { 121 | $popup = new MapboxPopup(); 122 | $popup->lngLat($lngLat) 123 | ->htmls($contents) 124 | ->addTo($this); 125 | } 126 | return $this; 127 | } 128 | 129 | public function on($eventName,$fun = null) 130 | { 131 | if(is_object($eventName)){ 132 | $this->components[] = $eventName; 133 | } else { 134 | foreach ($this->eventType as $key => $value) { 135 | foreach ($value as $en) { 136 | if(is_array($eventName) && in_array($en,$eventName)){ 137 | $eventName = new $key($eventName); 138 | $this->components[] = $eventName; 139 | } else { 140 | if($eventName == $en){ 141 | $eventName = new $key($eventName); 142 | $this->components[] = $eventName; 143 | } 144 | } 145 | 146 | } 147 | } 148 | 149 | } 150 | if(!is_object($eventName)){ 151 | $eventName = new MapboxEvent($eventName); 152 | $this->components[] = $eventName; 153 | } 154 | $eventName->name($this->name); 155 | $fun($eventName); 156 | return $this; 157 | 158 | } 159 | 160 | 161 | public function __call($method, $args) 162 | { 163 | foreach ($args as $index => $arg) { 164 | if(is_array($arg)){ 165 | $args[$index] = json_encode($arg); 166 | } else if($arg instanceof Closure){ 167 | $args[$index] = $arg($this); 168 | } else if(is_string($arg)){ 169 | $args[$index] = "`".$arg."`"; 170 | } else if($arg instanceof RawJs){ 171 | $args[$index] = $arg->result(); 172 | } 173 | } 174 | $this->components[] = $this->name.".$method(".implode(',',$args).");\r\n"; 175 | return $this; 176 | } 177 | 178 | public function rawJs($js) 179 | { 180 | if($js instanceof RawJs){ 181 | $this->components[] = $js; 182 | } else { 183 | $js = new RawJs($js); 184 | $this->components[] = $js; 185 | } 186 | return $this; 187 | } 188 | 189 | 190 | public function generateComponent() 191 | { 192 | $mapName = $this->name; 193 | foreach ($this->components as $component) { 194 | if(is_string($component)){ 195 | $this->codes .= $component; 196 | } else { 197 | $this->codes .= $component->result($mapName); 198 | } 199 | 200 | } 201 | return $this->codes; 202 | } 203 | 204 | 205 | } -------------------------------------------------------------------------------- /src/Mapbox/MapboxPopup.php: -------------------------------------------------------------------------------- 1 | options = $options; 20 | } else { 21 | $this->htmls($options); 22 | if(is_array($args)){ 23 | $this->options = $args; 24 | } 25 | } 26 | } 27 | 28 | public function name($name) 29 | { 30 | $this->name = $name; 31 | return $this; 32 | } 33 | 34 | public function setHTML($htmls) 35 | { 36 | return $this->htmls($htmls); 37 | } 38 | 39 | public function setLatLng($lngLat) 40 | { 41 | $this->lngLat = $lngLat; 42 | return $this; 43 | } 44 | 45 | public function lngLat($lngLat) 46 | { 47 | $this->lngLat = $lngLat; 48 | return $this; 49 | } 50 | 51 | public function openPopup() 52 | { 53 | $this->openPopup = true; 54 | return $this; 55 | } 56 | 57 | public function addTo($map) 58 | { 59 | if(is_object($map)){ 60 | $map->components[] = $this; 61 | $this->addTo = $map->name; 62 | } else if(is_string($map)) { 63 | $this->addTo = $map; 64 | } 65 | return $this; 66 | } 67 | 68 | 69 | public function htmls($htmls) 70 | { 71 | $this->htmls = ($htmls instanceof RawJs)?$htmls->result():"`$htmls`"; 72 | return $this; 73 | } 74 | 75 | public function result($markerName = null) 76 | { 77 | $this->codes .= "var {$this->name} = new mapboxgl.Popup(".($this->options?json_encode($this->options):"").").setHTML(".$this->htmls.");\r\n"; 78 | 79 | if($this->lngLat){ 80 | if(is_string($this->lngLat)){ 81 | $this->codes .= "{$this->name}.setLngLat(".$this->lngLat.");\r\n"; 82 | } else if($this->lngLat instanceof RawJs){ 83 | $this->codes .= "{$this->name}.setLngLat(".$this->lngLat->result().");\r\n"; 84 | } else { 85 | $this->codes .= "{$this->name}.setLngLat(".json_encode($this->lngLat).");\r\n"; 86 | } 87 | } 88 | 89 | if($this->attachMarker && $markerName){ 90 | $this->codes .= "{$markerName}.setPopup({$this->name});\r\n"; 91 | } 92 | 93 | if($this->addTo){ 94 | $this->codes .= "{$this->name}.addTo({$this->addTo});\r\n"; 95 | } 96 | return $this->codes; 97 | } 98 | 99 | 100 | } -------------------------------------------------------------------------------- /src/RawJs.php: -------------------------------------------------------------------------------- 1 | codes = $codes; 10 | } 11 | 12 | public function result() 13 | { 14 | return $this->codes; 15 | } 16 | 17 | public static function raw($codes) 18 | { 19 | return $codes; 20 | } 21 | } -------------------------------------------------------------------------------- /src/resources/config/laravel-maps.php: -------------------------------------------------------------------------------- 1 | [ 4 | "css"=>["https://unpkg.com/leaflet@1.7.1/dist/leaflet.css"], 5 | "js"=>["https://unpkg.com/leaflet@1.7.1/dist/leaflet.js"] 6 | ], 7 | "mapbox"=>[ 8 | "css"=>["https://api.mapbox.com/mapbox-gl-js/v2.6.1/mapbox-gl.css"], 9 | "js"=>["https://api.mapbox.com/mapbox-gl-js/v2.6.1/mapbox-gl.js"], 10 | "mapbox_access_token"=>env("MAPBOX_ACCESS_TOKEN",""), 11 | "mapbox_username"=>env("MAPBOX_USERNAME","") 12 | ] 13 | ]; -------------------------------------------------------------------------------- /src/resources/views/render.blade.php: -------------------------------------------------------------------------------- 1 | {!! $elemen !!} -------------------------------------------------------------------------------- /src/resources/views/scripts.blade.php: -------------------------------------------------------------------------------- 1 | @foreach ($scripts as $link) 2 | 3 | @endforeach 4 | 5 | {!! $codes !!} -------------------------------------------------------------------------------- /src/resources/views/styles.blade.php: -------------------------------------------------------------------------------- 1 | @foreach ($styles as $link) 2 | 3 | @endforeach --------------------------------------------------------------------------------