Completed
Pull Request — master (#163)
by
unknown
08:39
created

Map::getAssets()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 0
dl 0
loc 10
rs 9.4285
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
     * Get assets required by this field.
11
     *
12
     * @return array
13
     */
14
    public static function getAssets()
15
    {
16
        if (config('app.locale') == 'zh_CN') {
17
            $js = 'http://map.qq.com/api/js?v=2.exp';
18
        } else {
19
            $js = 'https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&key='.env('GOOGLE_API_KEY');
20
        }
21
22
        return compact('js');
23
    }
24
25
    public function __construct($column, $arguments)
26
    {
27
        $this->column['lat'] = $column;
28
        $this->column['lng'] = $arguments[0];
29
30
        array_shift($arguments);
31
32
        $this->label = $this->formatLabel($arguments);
33
        $this->id = $this->formatId($this->column);
34
35
        /*
36
         * Google map is blocked in mainland China
37
         * people in China can use Tencent map instead(;
38
         */
39
        if (config('app.locale') == 'zh_CN') {
40
            $this->useTencentMap();
41
        } else {
42
            $this->useGoogleMap();
43
        }
44
    }
45
46 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...
47
    {
48
        $this->script = <<<EOT
49
        function initGoogleMap(name) {
50
            var lat = $('#{$this->id['lat']}');
51
            var lng = $('#{$this->id['lng']}');
52
53
            var LatLng = new google.maps.LatLng(lat.val(), lng.val());
54
55
            var options = {
56
                zoom: 13,
57
                center: LatLng,
58
                panControl: false,
59
                zoomControl: true,
60
                scaleControl: true,
61
                mapTypeId: google.maps.MapTypeId.ROADMAP
62
            }
63
64
            var container = document.getElementById("map_"+name);
65
            var map = new google.maps.Map(container, options);
66
67
            var marker = new google.maps.Marker({
68
                position: LatLng,
69
                map: map,
70
                title: 'Drag Me!',
71
                draggable: true
72
            });
73
74
            google.maps.event.addListener(marker, 'dragend', function (event) {
75
                lat.val(event.latLng.lat());
76
                lng.val(event.latLng.lng());
77
            });
78
        }
79
80
        initGoogleMap('{$this->id['lat']}{$this->id['lng']}');
81
EOT;
82
    }
83
84 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...
85
    {
86
        $this->script = <<<EOT
87
        function initTencentMap(name) {
88
            var lat = $('#{$this->id['lat']}');
89
            var lng = $('#{$this->id['lng']}');
90
91
            var center = new qq.maps.LatLng(lat.val(), lng.val());
92
93
            var container = document.getElementById("map_"+name);
94
            var map = new qq.maps.Map(container, {
95
                center: center,
96
                zoom: 13
97
            });
98
99
            var marker = new qq.maps.Marker({
100
                position: center,
101
                draggable: true,
102
                map: map
103
            });
104
105
            if( ! lat.val() || ! lng.val()) {
106
                var citylocation = new qq.maps.CityService({
107
                    complete : function(result){
108
                        map.setCenter(result.detail.latLng);
109
                        marker.setPosition(result.detail.latLng);
110
                    }
111
                });
112
113
                citylocation.searchLocalCity();
114
            }
115
116
            qq.maps.event.addListener(map, 'click', function(event) {
117
                marker.setPosition(event.latLng);
118
            });
119
120
            qq.maps.event.addListener(marker, 'position_changed', function(event) {
121
                var position = marker.getPosition();
122
                lat.val(position.getLat());
123
                lng.val(position.getLng());
124
            });
125
        }
126
127
        initTencentMap('{$this->id['lat']}{$this->id['lng']}');
128
EOT;
129
    }
130
}
131