This repository was archived by the owner on Nov 9, 2017. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathclass-property-googlemap.php
More file actions
196 lines (164 loc) · 5.31 KB
/
Copy pathclass-property-googlemap.php
File metadata and controls
196 lines (164 loc) · 5.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Property GoogleMap
*
* @version 1.0.0
*/
class PropertyGoogleMap extends Papi_Property {
/**
* Generate the HTML for the property.
*
* @since 1.0.0
*/
public function html() {
// Property options.
$options = $this->get_options();
// Render the input field
$this->input();
?>
<style type="text/css">
.map-canvas {
width: 100%;
height: 400px;
}
.papi-property-map-search {
font-size: 16px;
font-weight: 300;
line-height: 1.5;
width: 100% !important;
margin: 5px 0;
}
</style>
<div id="<?php echo $options->slug; ?>" class="map-canvas"></div>
<?php
}
/**
* Output custom JavaScript for the property.
*
* @since 1.0.0
*/
public function js() {
// Property options.
$options = $this->get_options();
// Property settings.
$settings = $this->get_settings( array(
'api_key' => '',
'latlng' => ''
) );
if ( empty( $settings->api_key ) ) {
return;
}
?>
<script type="text/javascript"
src="https://maps.googleapis.com/maps/api/js?key=<?php echo $settings->api_key; ?>&sensor=false"></script>
<script type="text/javascript">
<?php
// Database value.
$value = $this->get_value();
if (isset($value['lat']) && isset($value['lng'])) {
$lat = $value['lat'];
$lng = $value['lng'];
} else {
if (!empty($settings->latlng)) {
$value = explode(',', trim($settings->latlng));
$lat = $value[0];
$lng = $value[1];
} else {
$lat = '59.32893';
$lng = '18.06491';
}
}
?>
(function ($) {
var geocoder = new google.maps.Geocoder (), latLng = new google.maps.LatLng (<?php echo $lat; ?>, <?php echo $lng; ?>), mapOptions = {
center: latLng, zoom: 14
}, map, maker;
function geocodePosition (pos) {
geocoder.geocode ({
latLng: pos
}, function (responses) {
if (responses && responses.length) {
console.log (responses[0].formatted_address);
updateMarkerAddress (responses[0].formatted_address);
} else {
updateMarkerAddress ('<?php _e("Cannot determine address at this location.", "papi"); ?>');
}
});
}
function updateLatitudeLangitude (position) {
$ ('input[name="<?php echo $options->slug; ?>[lat]"]').val (position.lat ());
$ ('input[name="<?php echo $options->slug; ?>[lng]"]').val (position.lng ());
}
function updateMarkerAddress (address) {
$ ('input[name="<?php echo $options->slug; ?>[address]"]').val (address);
}
function initialize () {
map = new google.maps.Map (document.getElementById ("<?php echo $options->slug; ?>"), mapOptions);
marker = new google.maps.Marker ({
position: latLng, map: map, draggable: true, title: 'Select position'
});
google.maps.event.addListener (marker, 'drag', function () {
updateLatitudeLangitude (marker.getPosition ());
geocodePosition (marker.getPosition ());
});
google.maps.event.addListener (marker, 'dragend', function () {
updateLatitudeLangitude (marker.getPosition ());
geocodePosition (marker.getPosition ());
});
}
google.maps.event.addDomListener (window, 'load', initialize);
$ ('.papi-property-map-search').on ('papi/update_map', function () {
var $this = $ (this), val = $this.val ();
geocoder.geocode ({
'address': val
}, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var loc = results[0].geometry.location;
var latLng = new google.maps.LatLng (loc.lat (), loc.lng ());
marker.setPosition (latLng);
map.setCenter (latLng);
updateLatitudeLangitude (marker.getPosition ());
}
});
});
$ ('.papi-property-map-search').on ('keypress', function (e) {
if (e.which == 13) {
e.preventDefault ();
$ (this).trigger ('papi/update_map');
}
});
$ ('.papi-property-map-search').on ('blur', function (e) {
e.preventDefault ();
$ (this).trigger ('papi/update_map');
});
}) (jQuery);
</script>
<?php
}
/**
* Generate the html for searching adress.
*
* @since 1.0.0
*/
public function input() {
$options = $this->get_options();
$value = $this->get_value( array(
'address' => '',
'lat' => '',
'lng' => ''
) );
?>
<input type="search" name="<?php echo $options->slug; ?>[address]"
value="<?php echo $value['address']; ?>" class="papi-property-map-search"
placeholder="<?php _e( 'Search for address...' ); ?>"/>
<input type="hidden" name="<?php echo $options->slug; ?>[lat]"
value="<?php echo $value['lat']; ?>"/>
<input type="hidden" name="<?php echo $options->slug; ?>[lng]"
value="<?php echo $value['lng']; ?>"/>
<?php
}
}