-
Notifications
You must be signed in to change notification settings - Fork 0
/
PlacePicker.js
156 lines (149 loc) · 7.57 KB
/
PlacePicker.js
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
"use strict";
var $placePicker = null,
$modal = null,
n=null,
btnClass="btn btn-secondary btn-sm",
placePickerMap = null,
marker = false,
service = null,
geocoder = null,
result = null;
$.fn.PlacePicker = function (t) {
n = {
key : null,
title : null,
center: {lat: -34.397, lng: 150.644},
zoom: 18,
success: function () {}
};
var params = $.extend(n, t);
$placePicker = this;
$(this).wrap( "<div></div>" );
$(this).closest("div").hover(function(){
var left = $(this).offset().left+$(this).width() - 40;
var top = $(this).offset().top+5;
var btn = $('<div class="placePickerUIButton" title="Pick location from map" style="position:absolute;top: '+top+'px;left: '+left+'px;z-index: 1000;"><div class="'+params.btnClass+'"><i class="fa fa-map"></i></div></div>');
$(this).append(btn);
btn.click(function(){
if($("body").find(".modal.placePicker").length==0){
$("body").append('<div class="modal fade in placePicker" role="dialog"><style>.pac-container{ z-index: 10000; }</style><div class="modal-dialog modal-lg" style="width: 90%;"><div class="modal-content"><div class="modal-header"> <button type="button" class="close" data-dismiss="modal">×</button><h4 class="modal-title">Place Picker</h4></div><div class="modal-body" style="padding: 0px;"><div class="row"><div class="col-md-12" style="padding: 10px;position: absolute;z-index: 1;background: #fff;width: 30%;margin-left: 20px;margin-top: 4px;box-shadow: 0 2px 4px 0 rgba(0,0,0,0.16),0 2px 10px 0 rgba(0,0,0,0.12)!important;"><div class="input-group input-group-sm" style="width: 100%;"> <input type="text" class="form-control pull-right autocomplete" placeholder="Search here or pick a location on map" style="border: 1px solid #dddddd;" autocomplete="off"></div><div class="address_content" style="display: none"><div class="address" style="margin-top: 10px;display: block;padding: 9.5px;font-size: 13px;color: #333;background-color: #f5f5f5;border: 1px solid #ccc;border-radius: 4px;max-height: 50vh;overflow-x: hidden;overflow-y: scroll;text-align: left;"></div><div class="row"><div class="col-md-6"><div class="btn btn-sm btn-default placePickerCancel" style="width: 100%;margin-top: 10px;margin-bottom: 10px;" data-dismiss="modal"><i class="fa fa-close"></i> Cancel</div></div><div class="col-md-6"><div class="btn btn-sm btn-success placePickerSubmit" style="width: 100%;margin-top: 10px;margin-bottom: 10px;"><i class="fa fa-check"></i> Select</div></div></div></div></div><div class="col-md-12"><div id="placePickerMap" style="height:calc( 80vh );width:100%"></div></div></div></div></div></div></div>');
}
result = null;
$modal = $(".placePicker");
$modal.modal("show");
$(".placePicker").find(".address").html("");
$(".placePicker").find(".address_content").hide();
$(".placePicker").find(".autocomplete").val("");
placePickerMap = null
marker = false;
if (!(typeof google === 'object' && typeof google.maps === 'object')) {
$.getScript('https://maps.googleapis.com/maps/api/js?key='+params.key+'&libraries=places', function() {
initPlacePickerMap(params)
});
} else{
initPlacePickerMap(params)
}
if(params.title!=null){
$modal.find(".modal-title").html(params.title);
}
$(".placePickerSubmit").click(function(){
var place = $(".placePicker").find(".autocomplete");
var address = result.formatted_address;
var data=[];
data["country"]="";
data["administrative_area_level_2"]="";
data["postal_code"]="";
data["sublocality_level_1"]="";
data["sublocality_level_2"]="";
data["route"]="";
data["locality"]="";
data["formatted_address"] = address;
data["location"]=result.geometry.location;
$(result.address_components).each(function(i,address){
$(address.types).each(function(j,type){
if(type=="country"){
data[type] = address.short_name;
}else{
data[type] = address.long_name;
}
});
});
params.success(data,address);
$modal.modal("hide")
});
$(".placePickerCancel").click(function(){
$modal.modal("hide");
})
$modal.on('hidden.bs.modal', function () {
$("body").find(".modal.placePicker").remove();
})
});
},
function(){
$("body").find(".placePickerUIButton").remove();
});
};
function initPlacePickerMap(params) {
service = new google.maps.Geocoder;
geocoder= new google.maps.Geocoder;
setTimeout(function() {
var loc = new google.maps.LatLng(params.center.lat, params.center.lng);
placePickerMap = new google.maps.Map(document.getElementById('placePickerMap'), {
center: loc,
zoom: params.zoom
});
service = new google.maps.places.PlacesService(placePickerMap);
var autocomplete = new google.maps.places.Autocomplete(document.getElementsByClassName("autocomplete")[0]);
google.maps.event.addListener(autocomplete, 'place_changed', function() {
var place = autocomplete.getPlace();
if (typeof place.address_components !== 'undefined') {
placePickerMap.panTo(place.geometry.location)
if(marker === false){
marker = new google.maps.Marker({
position: place.geometry.location,
map: placePickerMap,
draggable: true
});
google.maps.event.addListener(marker, 'dragend', function(event){
markerLocation();
});
} else{
marker.setPosition(place.geometry.location);
}
markerLocation();
}
});
google.maps.event.addListener(placePickerMap, 'click', function(event) {
var clickedLocation = event.latLng;
if(marker === false){
marker = new google.maps.Marker({
position: clickedLocation,
map: placePickerMap,
draggable: true
});
google.maps.event.addListener(marker, 'dragend', function(event){
markerLocation();
});
} else{
marker.setPosition(clickedLocation);
}
markerLocation();
});
}, 1000);
}
function markerLocation(){
var currentLocation = marker.getPosition();
geocoder.geocode({'location': currentLocation}, function(results, status) {
if (status === 'OK') {
if (results[0]) {
result = results[0];
var cont = "<h6 style='font-weight:600;font-size: 16px;padding-bottom: 10px;'>"+result.formatted_address+"</h6>";
$(result.address_components).each(function(key,value){
cont += "<b>"+value.types.join(', ')+"</b> : "+value.long_name+"<br>";
});
$(".address").html(cont);
$(".address_content").show();
}
}
});
}