Completed
Pull Request — master (#2395)
by
unknown
03:34
created

Map::useYandexMap()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 33

Duplication

Lines 33
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 33
loc 33
rs 9.392
c 0
b 0
f 0
1
<?php
2
3
namespace Encore\Admin\Form\Field;
4
5
use Encore\Admin\Form\Field;
6
7
class Map extends Field
8
{
9
    /**
10
     * Column name.
11
     *
12
     * @var array
13
     */
14
    protected $column = [];
15
16
    /**
17
     * Get assets required by this field.
18
     *
19
     * @return array
20
     */
21
    public static function getAssets()
22
    {
23
        switch (config('admin.map_provider')) {
24
            case 'tencent':
25
                $js = '//map.qq.com/api/js?v=2.exp';
26
                break;
27
            case 'google':
28
                $js = '//maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&key='.env('GOOGLE_API_KEY');
29
                break;
30
            case 'yandex':
31
                $js = '//api-maps.yandex.ru/2.1/?lang=ru_RU';
32
                break;
33
            default:
34
                $js = '//maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&key='.env('GOOGLE_API_KEY');
35
        }
36
37
        return compact('js');
38
    }
39
40
    public function __construct($column, $arguments)
41
    {
42
        $this->column['lat'] = $column;
43
        $this->column['lng'] = $arguments[0];
44
45
        array_shift($arguments);
46
47
        $this->label = $this->formatLabel($arguments);
48
        $this->id = $this->formatId($this->column);
49
50
        /*
51
         * Google map is blocked in mainland China
52
         * people in China can use Tencent map instead(;
53
         */
54
        switch (config('admin.map_provider')) {
55
            case 'tencent':
56
                $this->useTencentMap();
57
                break;
58
            case 'google':
59
                $this->useGoogleMap();
60
                break;
61
            case 'yandex':
62
                $this->useYandexMap();
63
                break;
64
            default:
65
                $this->useGoogleMap();
66
        }
67
    }
68
69 View Code Duplication
    public function useGoogleMap()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
70
    {
71
        $this->script = <<<EOT
72
        function initGoogleMap(name) {
73
            var lat = $('#{$this->id['lat']}');
74
            var lng = $('#{$this->id['lng']}');
75
76
            var LatLng = new google.maps.LatLng(lat.val(), lng.val());
77
78
            var options = {
79
                zoom: 13,
80
                center: LatLng,
81
                panControl: false,
82
                zoomControl: true,
83
                scaleControl: true,
84
                mapTypeId: google.maps.MapTypeId.ROADMAP
85
            }
86
87
            var container = document.getElementById("map_"+name);
88
            var map = new google.maps.Map(container, options);
89
90
            var marker = new google.maps.Marker({
91
                position: LatLng,
92
                map: map,
93
                title: 'Drag Me!',
94
                draggable: true
95
            });
96
97
            google.maps.event.addListener(marker, 'dragend', function (event) {
98
                lat.val(event.latLng.lat());
99
                lng.val(event.latLng.lng());
100
            });
101
        }
102
103
        initGoogleMap('{$this->id['lat']}{$this->id['lng']}');
104
EOT;
105
    }
106
107 View Code Duplication
    public function useTencentMap()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
108
    {
109
        $this->script = <<<EOT
110
        function initTencentMap(name) {
111
            var lat = $('#{$this->id['lat']}');
112
            var lng = $('#{$this->id['lng']}');
113
114
            var center = new qq.maps.LatLng(lat.val(), lng.val());
115
116
            var container = document.getElementById("map_"+name);
117
            var map = new qq.maps.Map(container, {
118
                center: center,
119
                zoom: 13
120
            });
121
122
            var marker = new qq.maps.Marker({
123
                position: center,
124
                draggable: true,
125
                map: map
126
            });
127
128
            if( ! lat.val() || ! lng.val()) {
129
                var citylocation = new qq.maps.CityService({
130
                    complete : function(result){
131
                        map.setCenter(result.detail.latLng);
132
                        marker.setPosition(result.detail.latLng);
133
                    }
134
                });
135
136
                citylocation.searchLocalCity();
137
            }
138
139
            qq.maps.event.addListener(map, 'click', function(event) {
140
                marker.setPosition(event.latLng);
141
            });
142
143
            qq.maps.event.addListener(marker, 'position_changed', function(event) {
144
                var position = marker.getPosition();
145
                lat.val(position.getLat());
146
                lng.val(position.getLng());
147
            });
148
        }
149
150
        initTencentMap('{$this->id['lat']}{$this->id['lng']}');
151
EOT;
152
    }
153
154 View Code Duplication
    public function useYandexMap()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
155
    {
156
        $this->script = <<<EOT
157
        function initYandexMap(name) {
158
            ymaps.ready(function(){
159
    
160
                var lat = $('#{$this->id['lat']}');
161
                var lng = $('#{$this->id['lng']}');
162
    
163
                var myMap = new ymaps.Map("map_"+name, {
164
                    center: [lat.val(), lng.val()],
165
                    zoom: 18
166
                }); 
167
168
                var myPlacemark = new ymaps.Placemark([lat.val(), lng.val()], {
169
                }, {
170
                    preset: 'islands#redDotIcon',
171
                    draggable: true
172
                });
173
174
                myPlacemark.events.add(['dragend'], function (e) {
175
                    lat.val(myPlacemark.geometry.getCoordinates()[0]);
176
                    lng.val(myPlacemark.geometry.getCoordinates()[1]);
177
                });                
178
179
                myMap.geoObjects.add(myPlacemark);
180
            });
181
182
        }
183
        
184
        initYandexMap('{$this->id['lat']}{$this->id['lng']}');
185
EOT;
186
    }
187
}
188