-
Notifications
You must be signed in to change notification settings - Fork 2
/
autocompleteroutemap.js
128 lines (89 loc) · 3.41 KB
/
autocompleteroutemap.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
(function ( $ ) {
$.fn.acroute = function( options ) {
var myPosition;
var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
var input = document.getElementById('inputform');
var autocomplete = new google.maps.places.Autocomplete(input);
// DETECT OPTIONS
var settings = $.extend({
originType: "geolocation",
originDirections: { lat: null, lng: null },
destination: { lat: null, lng: null },
showDetailedRoute: false,
showMap: false,
mapContainer: null
}, options );
// GET GEOLOCATION ACCESS AND DATA
if(navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position){
// RECOVERY ADDRESS AND FILL INPUT TEXT
recoveryAddress(position.coords.latitude, position.coords.longitude);
// CREATE CUSTOM MAP
if(settings.showMap){
directionsDisplay = new google.maps.DirectionsRenderer(
{
hideRouteList:false,
suppressBicyclingLayer:true,
suppressInfoWindows:true,
polylineOptions:{draggable:true, editable:true}
}
);
var myOptions = {
zoom: 17,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById(settings.mapContainer), myOptions);
myPosition = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
var marker = new google.maps.Marker({
position:myPosition,
map:map,
icon: 'img/pointer.png'
});
marker.setMap(map);
map.setCenter(myPosition);
}
});
}
// CREATE CONTAINER FOR RECEIVE AUTO-COMPLETE DATA
$(this).parent().append('<div class="acroutecontainer"></div>');
// LISTEN CHANGES ON INPUT FIELD
setInterval(function(){
this.onkeyup = function(){
}
},1000);
};
}( jQuery ));
function recoveryAddress(lat, lng) {
var objPosition = new google.maps.LatLng(lat, lng);
var geocoder = new google.maps.Geocoder();
geocoder.geocode({'latLng': objPosition}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[1]) {
var endereco = results[1].formatted_address;
$('.acroutecontainer').prev().val(endereco);
}else{
console.log("erro");
}
return results[1].formatted_address;
}else{
console.log("erro: " + status);
}
});
}
function showError(error) {
switch(error.code) {
case error.PERMISSION_DENIED:
return false;
break;
case error.POSITION_UNAVAILABLE:
return false;
break;
case error.TIMEOUT:
return false;
break;
case error.UNKNOWN_ERROR:
return false;
break;
}
}