var coord = [
{"id": 1, "coordlat": 47.2200, "coordlng": 39.6170},
{"id": 2, "coordlat": 47.2882, "coordlng": 39.6643},
{"id": 3, "coordlat": 47.2190, "coordlng": 39.7162},
{"id": 4, "coordlat": 47.2502, "coordlng": 39.7930},
{"id": 5, "coordlat": 47.2503, "coordlng": 39.5639},
{"id": 6, "coordlat": 47.2612, "coordlng": 39.6854},
{"id": 7, "coordlat": 47.2830, "coordlng": 39.6253},
{"id": 8, "coordlat": 47.2799, "coordlng": 39.7486},
{"id": 9, "coordlat": 47.1972, "coordlng": 39.6569},
];
var delAdress = $('input"]');
var markers = [];
var coords = [];
var map;
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
zoom: 11,
center: {lat: 47.2381, lng: 39.7002}
});
var geocoder = new google.maps.Geocoder();
for( i = 0; i < coord.length; i++ ) {
var position = new google.maps.LatLng(coord[i].coordlat, coord[i].coordlng);
marker = new google.maps.Marker({
position: position,
map: map,
});
coords.push(marker);
};
document.getElementById('btn').addEventListener('click', function() {
if(delAdress.val()==''){
return false;
} else{
geocodeAddress(geocoder, map);
}
});
map.addListener('click', function(event) {
addMarker(event.latLng);
});
};
function addMarker(location){
setMapOnAll(null);
markers = [];
var marker = new google.maps.Marker({
position: location,
map: map,
});
markers.push(marker);
};
function geocodeAddress(geocoder, resultsMap) {
var address = document.getElementById('suggestions').value;
geocoder.geocode({'address': address}, function(results, status) {
if (status === 'OK') {
setMapOnAll(null);
markers = [];
resultsMap.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location,
});
markers.push(marker);
} else {
console.log('Geocode was not successful for the following reason: ' + status);
}
});
};
function setMapOnAll(map) {
for (var i = 0; i < markers.length; i++) {
markers[i].setMap(map);
}
}