I have a google map and a custom element to use as a marker. When I drop the marker from outside the map on the map I need to get the coordinates of that location. How to do this?
".clue_bottom_left img" is the element used as the marker.
Code for drag and drop using jquery-ui:
$(".clue_bottom_left img").draggable({
containment: 'map',
revert: "invalid",
start: function(evt, ui) {
$('.clue_bottom_left img').fadeTo('fast', 0.6, function() {});
},
stop: function(evt, ui) {
$('.clue_bottom_left img').fadeTo('fast', 1.0, function() {});
// INSERT Point
}
});
$('#map').droppable({
drop: function(e, ui) {
$(ui.draggable).draggable();
}
});
Code for creating the map:
function initAutocomplete() {
var map = new google.maps.Map(document.getElementById('map'), {
center: {lat: 41.9876644, lng: 22.4192234},
zoom: 6,
mapTypeId: google.maps.MapTypeId.ROADMAP,
disableDefaultUI: true, // a way to quickly hide all controls
});
// Create the search box and link it to the UI element.
var input = document.getElementById('pac-input');
var searchBox = new google.maps.places.SearchBox(input);
map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);
// Bias the SearchBox results towards current map's viewport.
map.addListener('bounds_changed', function() {
searchBox.setBounds(map.getBounds());
});
var markers = [];
// Listen for the event fired when the user selects a prediction and retrieve
// more details for that place.
searchBox.addListener('places_changed', function() {
var places = searchBox.getPlaces();
if (places.length == 0) {
return;
}
// Clear out the old markers.
markers.forEach(function(marker) {
marker.setMap(null);
});
markers = [];
// For each place, get the icon, name and location.
var bounds = new google.maps.LatLngBounds();
places.forEach(function(place) {
var icon = {
url: place.icon,
size: new google.maps.Size(71, 71),
origin: new google.maps.Point(0, 0),
anchor: new google.maps.Point(17, 34),
scaledSize: new google.maps.Size(25, 25)
};
// Create a marker for each place.
markers.push(new google.maps.Marker({
map: map,
icon: icon,
title: place.name,
position: place.geometry.location
}));
if (place.geometry.viewport) {
// Only geocodes have viewport.
bounds.union(place.geometry.viewport);
} else {
bounds.extend(place.geometry.location);
}
});
map.fitBounds(bounds);
});
}
UPDATE:
This is not a duplicate as someone marked it with another post on how to do something when the marker is dropped on the map. I know how to do that. My marker is not actually a marker. Is a image in a div outside the map. And I wondered how to use that image as a marker using drag and drop with jQuery UI.
Aucun commentaire:
Enregistrer un commentaire