Good day, I have concern about adding mark icon to my google map, so every time the user will click the location. the mark will place to the location itself.
This is my code for the click function
$('button#addresses').click(function(){
var address_href = $(this).val();
var commaPos = address_href.indexOf(',');
var coordinatesLat = parseFloat(address_href.substring(0, commaPos));
var coordinatesLong = parseFloat(address_href.substring(commaPos + 1, address_href.length));
var centerPoint = new google.maps.LatLng(coordinatesLat, coordinatesLong);
map.setCenter(centerPoint);
})
so if the user click the address the google map automatically change the location.
This is the code for default function of google map
var map;
function initMap() {
var myHome = { "lat" : "53.628301" , "long" : "-113.408736" };
map = new google.maps.Map(document.getElementById('map'), {
zoom: 16,
center: new google.maps.LatLng(myHome.lat, myHome.long),
mapTypeId: 'roadmap'
});
var geocoder = new google.maps.Geocoder();
document.getElementById('submit').addEventListener('click', function() {
geocodeAddress(geocoder, map);
});
var infowindow = new google.maps.InfoWindow();
var service = new google.maps.places.PlacesService(map)
function geocodeAddress(geocoder, resultsMap) {
var address = document.getElementById('address').value;
geocoder.geocode({'address': address}, function(results, status) {
if (status === 'OK') {
resultsMap.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: resultsMap,
position: results[0].geometry.location,
icon: '{{ asset('assets/googlemap-marker-hiflyer.png') }}'
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent('<div><strong>' + place.name + '</strong><br>' +
'Place ID: ' + place.place_id + '<br>' +
place.formatted_address + '</div>');
infowidnow.open(map, this);
});
} else {
alert('Fill the blank');
}
});
}
var iconBase = 'https://maps.google.com/mapfiles/kml/shapes/';
var features = [
{
position: new google.maps.LatLng(53.628301, -113.408736),
type: 'info'
},
];
// Create markers.
features.forEach(function(feature) {
var marker = new google.maps.Marker({
position: feature.position,
icon: '{{ asset('assets/googlemap-marker-hiflyer.png') }}',
map: map
});
});
}
So the question is how to set the marker if the user click the location.
Thanks guys. I hope it will solve my problem.