Reputation: 41
I'm using google maps api and need to show a marker with a search box, this marker will use the lat and long I have on my database but will change if dragged or someone uses the search box. I managed to load the map and center it on the coordinates but it doesn’t shows the marker, after I use the search box all works. I tried adding a marker in the beginning of the code but without success, it creates a new marker when I used the functions:
var mapOptions = {
zoom: zoom,
center: LatLng,
panControl: false,
zoomControl: false,
scaleControl: true,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById('map'),mapOptions);
var latitude = ;
var longitude = ;
var zoom = "+MapZoom+";
var LatLng = new google.maps.LatLng(51.501476, -0.140634);
var marker = new google.maps.Marker({
position: LatLng,
map: map,
title: '"+Latitude+"',
draggable: true
});
This is my current code, I’m sure I’m just missing something any help would be much appreciated
function initialize() {
var markers = [];
var map = new google.maps.Map(document.getElementById('map'), {
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var defaultBounds = new google.maps.LatLngBounds(
new google.maps.LatLng(51.501476, -0.140634));
map.fitBounds(defaultBounds);
var input = (document.getElementById('pacinput')
);
map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);
var searchBox = new google.maps.places.SearchBox((input));
google.maps.event.addListener(searchBox, 'places_changed', function() {
var places = searchBox.getPlaces();
if (places.length == 0) {
return;
}
for (var i = 0, marker; marker = markers[i]; i++) {
marker.setMap(null);
}
markers = [];
var bounds = new google.maps.LatLngBounds();
for (var i = 0, place; place = places[i]; i++) {
var marker = new google.maps.Marker({
draggable: true,
map: map,
title: place.name,
position: place.geometry.location
});
// drag response
google.maps.event.addListener(marker, 'dragend', function(e) {
displayPosition(this.getPosition());
});
// click response
google.maps.event.addListener(marker, 'click', function(e) {
displayPosition(this.getPosition());
});
markers.push(marker);
bounds.extend(place.geometry.location);
}
map.fitBounds(bounds);
});
google.maps.event.addListener(map, 'bounds_changed', function() {
var bounds = map.getBounds();
searchBox.setBounds(bounds);
});
// displays a position on two <input> elements
function displayPosition(pos) {
document.getElementById('Latitude').value = pos.lat();
document.getElementById('Longitude').value = pos.lng();
}
}
google.maps.event.addDomListener(window, 'load', initialize);
initialize();
</script>"
Upvotes: 3
Views: 2275
Reputation: 805
as per my understanding you want marker on load of page to the same location where your map is centered try using this
var map;
var marker;
$(document).ready(function(){
initialize();
drawmarker();
darggable();
});
function initialize() {
var mapProp = {
center:new google.maps.LatLng(51.508742,-0.120850),
zoom:5,
mapTypeId:google.maps.MapTypeId.ROADMAP
};
map=new google.maps.Map(document.getElementById("googleMap"),mapProp);
}
function drawmarker(){
marker = new google.maps.Marker({
position : new google.maps.LatLng(51.508742,-0.120850),
title:"this s it",
map : map,
draggable:true,
})
}
make your marker as global now use method on dragend
function darggable(){
google.maps.event.addListener(marker, 'dragend', function(e) {
console.log(e.latLng.lat(),e.latLng.lng())
});
}
now you get the latlng now use it to display in the text box
Upvotes: 0