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

Map::useGoogleMap()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 37

Duplication

Lines 37
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 37
loc 37
rs 9.328
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
24
        switch (config('admin.map_provider')) {
25
            case 'tencent':
26
                $js = '//map.qq.com/api/js?v=2.exp';
27
                break;
28
            case 'google':
29
                $js = '//maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&key='.env('GOOGLE_API_KEY');
30
                break;
31
            case 'yandex':
32
                $js = '//api-maps.yandex.ru/2.1/?lang=ru_RU';
33
                break;
34
            default:
35
                $js = '//maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&key='.env('GOOGLE_API_KEY');
36
        }
37
38
        return compact('js');
39
    }
40
41
    public function __construct($column, $arguments)
42
    {
43
        $this->column['lat'] = $column;
44
        $this->column['lng'] = $arguments[0];
45
46
        array_shift($arguments);
47
48
        $this->label = $this->formatLabel($arguments);
49
        $this->id = $this->formatId($this->column);
50
51
        /*
52
         * Google map is blocked in mainland China
53
         * people in China can use Tencent map instead(;
54
         */
55
        switch (config('admin.map_provider')) {
56
            case 'tencent':
57
                $this->useTencentMap();
58
                break;
59
            case 'google':
60
                $this->useGoogleMap();
61
                break;
62
            case 'yandex':
63
                $this->useYandexMap();
64
                break;
65
            default:
66
                $this->useGoogleMap();
67
        }
68
69
70
    }
71
72 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...
73
    {
74
        $this->script = <<<EOT
75
        function initGoogleMap(name) {
76
            var lat = $('#{$this->id['lat']}');
77
            var lng = $('#{$this->id['lng']}');
78
79
            var LatLng = new google.maps.LatLng(lat.val(), lng.val());
80
81
            var options = {
82
                zoom: 13,
83
                center: LatLng,
84
                panControl: false,
85
                zoomControl: true,
86
                scaleControl: true,
87
                mapTypeId: google.maps.MapTypeId.ROADMAP
88
            }
89
90
            var container = document.getElementById("map_"+name);
91
            var map = new google.maps.Map(container, options);
92
93
            var marker = new google.maps.Marker({
94
                position: LatLng,
95
                map: map,
96
                title: 'Drag Me!',
97
                draggable: true
98
            });
99
100
            google.maps.event.addListener(marker, 'dragend', function (event) {
101
                lat.val(event.latLng.lat());
102
                lng.val(event.latLng.lng());
103
            });
104
        }
105
106
        initGoogleMap('{$this->id['lat']}{$this->id['lng']}');
107
EOT;
108
    }
109
110 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...
111
    {
112
        $this->script = <<<EOT
113
        function initTencentMap(name) {
114
            var lat = $('#{$this->id['lat']}');
115
            var lng = $('#{$this->id['lng']}');
116
117
            var center = new qq.maps.LatLng(lat.val(), lng.val());
118
119
            var container = document.getElementById("map_"+name);
120
            var map = new qq.maps.Map(container, {
121
                center: center,
122
                zoom: 13
123
            });
124
125
            var marker = new qq.maps.Marker({
126
                position: center,
127
                draggable: true,
128
                map: map
129
            });
130
131
            if( ! lat.val() || ! lng.val()) {
132
                var citylocation = new qq.maps.CityService({
133
                    complete : function(result){
134
                        map.setCenter(result.detail.latLng);
135
                        marker.setPosition(result.detail.latLng);
136
                    }
137
                });
138
139
                citylocation.searchLocalCity();
140
            }
141
142
            qq.maps.event.addListener(map, 'click', function(event) {
143
                marker.setPosition(event.latLng);
144
            });
145
146
            qq.maps.event.addListener(marker, 'position_changed', function(event) {
147
                var position = marker.getPosition();
148
                lat.val(position.getLat());
149
                lng.val(position.getLng());
150
            });
151
        }
152
153
        initTencentMap('{$this->id['lat']}{$this->id['lng']}');
154
EOT;
155
    }
156
157 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...
158
    {
159
        $this->script = <<<EOT
160
        function initYandexMap(name) {
161
            ymaps.ready(function(){
162
    
163
                var lat = $('#{$this->id['lat']}');
164
                var lng = $('#{$this->id['lng']}');
165
    
166
                var myMap = new ymaps.Map("map_"+name, {
167
                    center: [lat.val(), lng.val()],
168
                    zoom: 18
169
                }); 
170
171
                var myPlacemark = new ymaps.Placemark([lat.val(), lng.val()], {
172
                }, {
173
                    preset: 'islands#redDotIcon',
174
                    draggable: true
175
                });
176
177
                myPlacemark.events.add(['dragend'], function (e) {
178
                    lat.val(myPlacemark.geometry.getCoordinates()[0]);
179
                    lng.val(myPlacemark.geometry.getCoordinates()[1]);
180
                });                
181
182
                myMap.geoObjects.add(myPlacemark);
183
            });
184
185
        }
186
        
187
        initYandexMap('{$this->id['lat']}{$this->id['lng']}');
188
EOT;
189
    }    
190
}