I'm struggling with google maps.
Yesterday I had a problem to show an infowindow in the place that I wanted and an user of stackoverflow told me to try this:
http://google-maps-utility-library-v3.googlecode.com/svn/tags/infobox/1.1.5/src/infobox.js
That plugin solved me the problem, but I get another one.
I wanted to be able to resize the page and always get the marker position at the center of the map. How can I do this ?
Heres my code:
function initialize() {
var loc, map, marker, infobox;
loc = new google.maps.LatLng(40.20384, -8.39935);
map = new google.maps.Map(document.getElementById("map"), {
zoom: 18,
center: loc,
mapTypeId: google.maps.MapTypeId.HYBRID,
scrollwheel: false,
draggable: false,
});
marker = new google.maps.Marker({
map: map,
position: loc,
visible: true,
icon: "public/images/logo.png",
});
infobox = new InfoBox({
content: document.getElementById("over_map"),
disableAutoPan: false,
maxWidth: 150,
pixelOffset: new google.maps.Size(100, -100),
zIndex: null,
boxStyle: {
background: "gray",
opacity: 0.75,
width: "280px"
},
closeBoxMargin: "10px 2px 2px 2px",
isHidden: false,
closeBoxURL: "http://www.google.com/intl/en_us/mapfiles/close.gif",
infoBoxClearance: new google.maps.Size(1, 1)
});
infobox.open(map, marker);
google.maps.event.addListener(marker, 'click', function() {
infobox.open(map, this);
map.panTo(loc);
});
}
google.maps.event.addDomListener(window, 'load', initialize);
Hope someone could help me :)
You can use window.onresize to that :
window.onresize=function() {
map.setCenter(marker.getPosition());
}
see this working demo -> http://jsfiddle.net/Fae26/ - try resize the frames or resize the entire window.
Related
Trying to figure out how to "attach" an InfoBox" to the left edge of a circle drawn in Google Maps (v3).
Here's what I have so far:
http://jsfiddle.net/a1aq9ey8/6/
If you zoom in/out of the map, you'll notice that the InfoBox (8 MILE) position no longer left aligns with the circle... I'd like the infoBox to stay left aligned to the left/centered edge of the circle when the user zooms in or out... Is this possible?
$(document).ready(function(){
//set your google maps parameters
var map;
var bblocation = new google.maps.LatLng(37.787321,-122.258365);
var map_zoom = 11;
//set google map options
var map_options = {
center: bblocation,
zoom: map_zoom,
panControl: false,
zoomControl: true,
mapTypeControl: false,
streetViewControl: false,
mapTypeId: google.maps.MapTypeId.ROADMAP,
scrollwheel: false,
disableDefaultUI: true,
}
//inizialize the map
map = new google.maps.Map(document.getElementById('google-container'), map_options);
var radiusOptions = {
strokeColor:"#222c38",
strokeOpacity:1,
strokeWeight:1,
fillColor:"#ffffff",
fillOpacity:0,
map: map,
center: bblocation,
radius: 12872
};
markerCircles = new google.maps.Circle(radiusOptions);
var labelText = "8 MILE";
var myOptions = {
content: labelText,
boxStyle: {
background: '#222c38',
color: '#ffffff',
textAlign: "center",
fontSize: "8pt",
width: "50px"
},
disableAutoPan: true,
pixelOffset: new google.maps.Size(-45, 0),
position: new google.maps.LatLng(37.787321,-122.374365),
closeBoxURL: "",
isHidden: false,
pane: "mapPane",
enableEventPropagation: true
};
var mmLabel = new InfoBox(myOptions);
mmLabel.open(map);
google.maps.event.addDomListener(window, "resize", function() {
var center = map.getCenter();
google.maps.event.trigger(map, "resize");
map.setCenter(center);
});
});
You need to calculate the position of the label to put it on the circle. Using the geometry library computeOffset method.
distance = radius of the circle
heading = -90 degrees (west)
fromLatLng = center of the circle
var radiusOptions = {
strokeColor:"#222c38",
strokeOpacity:1,
strokeWeight:1,
fillColor:"#ffffff",
fillOpacity:0,
map: map,
center: bblocation,
radius: 12872
};
markerCircles = new google.maps.Circle(radiusOptions);
// calculate the position of the label
var labelPosn = google.maps.geometry.spherical.computeOffset(radiusOptions.center,radiusOptions.radius,-90);
var labelText = "8 MILE";
var myOptions = {
content: labelText,
boxStyle: {
background: '#222c38',
color: '#ffffff',
textAlign: "center",
fontSize: "8pt",
width: "50px"
},
disableAutoPan: true,
pixelOffset: new google.maps.Size(0, 0), // left upper corner of the label
position: labelPosn, // on the left edge of the circle
closeBoxURL: "",
isHidden: false,
pane: "mapPane",
enableEventPropagation: true
};
updated fiddle
code snippet:
$(document).ready(function() {
//set your google maps parameters
var map;
var bblocation = new google.maps.LatLng(37.787321, -122.258365);
var map_zoom = 11;
//set google map options
var map_options = {
center: bblocation,
zoom: map_zoom,
panControl: false,
zoomControl: true,
mapTypeControl: false,
streetViewControl: false,
mapTypeId: google.maps.MapTypeId.ROADMAP,
scrollwheel: false,
disableDefaultUI: true,
}
//inizialize the map
map = new google.maps.Map(document.getElementById('google-container'), map_options);
var radiusOptions = {
strokeColor: "#222c38",
strokeOpacity: 1,
strokeWeight: 1,
fillColor: "#ffffff",
fillOpacity: 0,
map: map,
center: bblocation,
radius: 12872
};
markerCircles = new google.maps.Circle(radiusOptions);
// calculate the position of the label
var labelPosn = google.maps.geometry.spherical.computeOffset(radiusOptions.center, radiusOptions.radius, -90);
var labelText = "8 MILE";
var myOptions = {
content: labelText,
boxStyle: {
background: '#222c38',
color: '#ffffff',
textAlign: "center",
fontSize: "8pt",
width: "50px"
},
disableAutoPan: true,
pixelOffset: new google.maps.Size(0, 0), // left upper corner of the label
position: labelPosn, // on the left edge of the circle
closeBoxURL: "",
isHidden: false,
pane: "mapPane",
enableEventPropagation: true
};
var mmLabel = new InfoBox(myOptions);
mmLabel.open(map);
google.maps.event.addDomListener(window, "resize", function() {
var center = map.getCenter();
google.maps.event.trigger(map, "resize");
map.setCenter(center);
});
});
html,
body,
#google-container {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?libraries=geometry"></script>
<script src="https://cdn.rawgit.com/googlemaps/v3-utility-library/master/infobox/src/infobox.js"></script>
<div id="google-container"></div>
We are currently using two google maps on one page, the first one is an aerial view, and this I can turn off draggable and it works fine. The other is an internal view of the building, when I use the same code on this, it doesnt disable draggable on mobile or on desktop, I can disable scrollwheel movement, but not draggable. The code I am using is below.
function initialize2() {
var fenway = new google.maps.LatLng(0, 0);
var mapOptions = {
center: fenway,
scrollwheel: false,
zoom: 8,
draggable: false
};
var panoramaOptions = {
position: fenway,
scrollwheel: false,
draggable: false,
<?php echo(isMobile()) ? 'draggable: false,' : ''; ?>
pov: {
heading: 180,
pitch: 0
}
};
var panorama = new google.maps.StreetViewPanorama(document.getElementById('map-canvas2'),panoramaOptions);
map.setStreetView(panorama);
}
google.maps.event.addDomListener(window, 'load', initialize);
You just need to add: draggable: false like this
// Function to initialize the map within the map div
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
center: { lat: 27.963989, lng: -82.799957},
zoom: 14,
draggable: false
});
}
For some reason my infoBox'es turn up behind the markers. I know this is supposed to be impossible, but it is never the less the case. Here is the code for my markers:
var yellowCircle={
path: google.maps.SymbolPath.CIRCLE,
scale: 5,
fillColor: '#faeadd',
strokeColor: 'black',
fillOpacity: 1.0,
strokeWeight: 0.5,
zIndex:-10
};
var yellowBlackCircle={
path: google.maps.SymbolPath.CIRCLE,
scale: 5,
fillColor: '#faeadd',
strokeColor: 'black',
fillOpacity: 1.0,
strokeWeight: 1.5,
zIndex:-10
};
var marker = new google.maps.Marker({
position: new google.maps.LatLng(Lat,Lng),
map: map,
title: name,
icon:yellowCircle,
url:url,
zIndex:-1,
});
google.maps.event.addListener(marker, 'mouseover', function() {
marker.setIcon(yellowBlackCircle);
});
google.maps.event.addListener(marker, 'mouseout', function() {
marker.setIcon(yellowCircle);
});
google.maps.event.addListener(marker, 'click', function() {
window.location.href = url;
});
And the code for my infoboxes:
var myOptions = {
content: name
,boxStyle: {
border: "1px solid black"
,textAlign: "center"
,fontSize: "12pt"
,fontType: "arial"
,backgroundColor: "#faeadd"
,fontWeight: "bold"
,zIndex:1
}
,disableAutoPan: true
,pixelOffset: new google.maps.Size(-getTextWidth(name, "bold 12pt arial")/2,-30)
,position: new google.maps.LatLng(49.47216, -123.76307)
,closeBoxURL: ""
,isHidden: false
,pane: "mapPane"
,enableEventPropagation: true
,zIndex:1
};
var infobox = new InfoBox(myOptions);
google.maps.event.addListener(marker, 'mouseover', function() {
infobox.open(map,marker);
});
google.maps.event.addListener(marker, 'mouseout', function() {
infobox.close();
});
You can see that I have even tried setting the zIndex'es as well, but it has no effect. I believe this is the same question as was asked in google maps infobox above all other content z-index does not work.
I apologize for not attaching a jsfiddle - I couldn't seem to make it work, even after I uploaded the infobox_packed.js to a repository so I could add is as external resource. Instead, here is the whole thing:
http://www.kaarebmikkelsen.dk/wp-content/uploads/2014/05/test2.html
The info-box code is mostly copied from https://google-maps-utility-library-v3.googlecode.com/svn/trunk/infobox/docs/examples.html.
Your code came from the example that makes "map labels" and you are attaching the infoBox to the mapPane:
pane: "mapPane"
That will put it behind the markers. If you want it to be an infowindow replacement, copy the code from that example:
pane: "floatPane"
from the documentation you reference
Here is my code
panoramaOptions = {
enableCloseButton : true,
visible : false
};
myPano = new google.maps.StreetViewPanorama(document.getElementById("pano"), panoramaOptions);
var mapOptions = {
center: currentCenter,
zoom: defaultZoom,
mapTypeId: google.maps.MapTypeId.ROADMAP,
overviewMapControl: true,
streetViewControl: false,
streetView : myPano
};
map = new google.maps.Map(document.getElementById("map"), mapOptions);
map.setStreetView(myPano);
google.maps.event.addListener(map,"click", function(e) {
map.setCenter(e.latLng);
myPano.setPosition(e.latLng);
if (marker != null) {
marker.setMap(null);
}
marker = new google.maps.Marker({
icon: new google.maps.MarkerImage("http:///maps.gstatic.com/mapfiles/cb/man_arrow-0.png"),
map: map,
draggable: true,
position: e.latLng
});
marker.setMap(map);
myPano.setVisible(true);
});
Now the problem is that sometimes the streetview doesn't come. How to solve this?
API v3
I have a strange issue with a marker:
The marker that I place on the map is set to be draggable, WebInspector reports it as being draggable, but I still can't drag it. The cursor doesn't change when I go over the marker.
The underlying map is draggable, and that works.
On another page, with a different setup (less layers), but the same javascript, it works as expected (so there I can drag it).
I also made the marker clickable and bound an event to it, and that works fine. The cursor changes as well on mouseover.
I have run out of options.... Why can I not drag it? Can this have to do with some zIndex or layer positioning? As far as I can see, the map has a higher zIndex than all the other layers....
// display map
geocoder.geocode( { 'address':address }, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
// map options
var mapOptions = {
zoom: 14,
center: results[0].geometry.location,
panControl: false,
zoomControl: true,
zoomControlOptions: {
style: google.maps.ZoomControlStyle.SMALL
},
mapTypeControl: false,
scaleControl: false,
streetViewControl: false,
mapTypeId: google.maps.MapTypeId.ROADMAP,
scrollwheel: false
};
// render map
map = new google.maps.Map(J('#'+mapElementId)[0], mapOptions);
// set marker
marker = new google.maps.Marker({
clickable: true,
draggable: true,
icon: markerImage,
shadow: markerShadow,
shape: markerShape,
map: map,
position: results[0].geometry.location
});
google.maps.event.addListener(marker, 'click', function () { alert('marker clicked'); });
google.maps.event.addListener(marker, 'dragend', function () {
alert('marker dragged')
map.setCenter(marker.getPosition());
J('input[name=coordinates]').val(marker.getPosition());
J('input[name=address]').val('');
});
J('input[name=address]').val(results[0].formatted_address);
}
});
you have draggable: false,.
change it to true
see it working here: http://jsfiddle.net/RASG/vA4eQ/
Try this:
map.setOptions({draggable: true});