I have been following a tutorial to set up a google map for my website here and I have completed this, however I now want a custom info box that pops up when you click the icon. (at the moment it pops up with googles default)
I was trying to use this code to do it, but every time I try none of the icons appear.
<script type="text/javascript">
//<![CDATA[
var customIcons = {
restaurant: {
icon: 'http://labs.google.com/ridefinder/images/mm_20_blue.png',
shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
},
bar: {
icon: 'http://labs.google.com/ridefinder/images/mm_20_red.png',
shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
}
};
var labelText = "City Hall";
var myOptions = {
content: labelText
,boxStyle: {
border: "1px solid black"
,textAlign: "center"
,fontSize: "8pt"
,width: "50px"
}
,disableAutoPan: true
,pixelOffset: new google.maps.Size(-25, 0)
,position: new google.maps.LatLng(49.47216, -123.76307)
,closeBoxURL: ""
,isHidden: false
,pane: "mapPane"
,enableEventPropagation: true
};
function load() {
var map = new google.maps.Map(document.getElementById("map"), {
center: new google.maps.LatLng(47.6145, -122.3418),
zoom: 13,
mapTypeId: 'roadmap'
});
This is the part that im changing at the moment
var infoWindow = new google.maps.InfoWindow;
to this
var infoWindow = new infoBox(myOptions);
.
// Change this depending on the name of your PHP file
downloadUrl("phpsqlajax_genxml.php", function(data) {
var xml = data.responseXML;
var markers = xml.documentElement.getElementsByTagName("marker");
for (var i = 0; i < markers.length; i++) {
var name = markers[i].getAttribute("name");
var address = markers[i].getAttribute("address");
var type = markers[i].getAttribute("type");
var point = new google.maps.LatLng(
parseFloat(markers[i].getAttribute("lat")),
parseFloat(markers[i].getAttribute("lng")));
var html = "<b>" + name + "</b> <br/>" + address;
var icon = customIcons[type] || {};
var marker = new google.maps.Marker({
map: map,
position: point,
icon: icon.icon,
shadow: icon.shadow
});
bindInfoWindow(marker, map, infoWindow, html);
}
});
}
function bindInfoWindow(marker, map, infoWindow, html) {
google.maps.event.addListener(marker, 'click', function() {
infoWindow.setContent(html);
infoWindow.open(map, marker);
});
}
function downloadUrl(url, callback) {
var request = window.ActiveXObject ?
new ActiveXObject('Microsoft.XMLHTTP') :
new XMLHttpRequest;
request.onreadystatechange = function() {
if (request.readyState == 4) {
request.onreadystatechange = doNothing;
callback(request, request.status);
}
};
request.open('GET', url, true);
request.send(null);
}
function doNothing() {}
//]]>
</script>
Any help to say where i'm going wrong would be appreciated
Assuming that you have included the infobox-library and the php-file returns the expected XML there is nothing wrong except of this:
var infoWindow = new infoBox(myOptions);
it has to be:
var infoWindow = new InfoBox(myOptions);
//-------------------^
Related
I have tried pretty much all the codes I can get on this site, and none of the has worked. Here is the original code I got from google map API for generating markers. I want the map to set the bound and center automatically based on the locations of the markers.
Please help me with the code and kindly inform me where to put the code exactly.
Thanks.
<script>
var customLabel = {
restaurant: {
label: 'R'
},
bar: {
label: 'B'
}
};
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
center: new google.maps.LatLng(0, 0),
zoom: 12
});
var infoWindow = new google.maps.InfoWindow;
// Change this depending on the name of your PHP or XML file
downloadUrl('http://localhost/test/mapxml.php', function(data) {
var xml = data.responseXML;
var markers = xml.documentElement.getElementsByTagName('marker');
var bounds = new google.maps.LatLngBounds();
Array.prototype.forEach.call(markers, function(markerElem) {
var name = markerElem.getAttribute('name');
var address = markerElem.getAttribute('address');
var type = markerElem.getAttribute('type');
var point = new google.maps.LatLng(
parseFloat(markerElem.getAttribute('lat')),
parseFloat(markerElem.getAttribute('lng')));
var infowincontent = document.createElement('div');
var strong = document.createElement('strong');
strong.textContent = name
infowincontent.appendChild(strong);
infowincontent.appendChild(document.createElement('br'));
var text = document.createElement('text');
text.textContent = address
infowincontent.appendChild(text);
var icon = customLabel[type] || {};
var marker = new google.maps.Marker({
map: map,
position: point,
label: icon.label,
});
marker.addListener('click', function() {
infoWindow.setContent(infowincontent);
infoWindow.open(map, marker);
});
var bounds = new google.maps.LatLngBounds();
bounds.extend(myLatLng);
map.fitBounds(bounds);
map.setCenter(bounds.getCenter());
});//marker
});//download xml
}//initMap
function downloadUrl(url, callback) {
var request = window.ActiveXObject ?
new ActiveXObject('Microsoft.XMLHTTP') :
new XMLHttpRequest;
request.onreadystatechange = function() {
if (request.readyState == 4) {
request.onreadystatechange = doNothing;
callback(request, request.status);
}
};
request.open('GET', url, true);
request.send(null);
}
function doNothing() {}
</script>
I am trying to set a radius around my marker the radius is set in my SQL database and that radius should show around the marker this is my current code
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCh0MbGxFVti1rJkypMgs8548dN4wr6oKY"
type="text/javascript"></script>
<script type="text/javascript">
//<![CDATA[
var customIcons = {
restaurant: {
icon: 'http://labs.google.com/ridefinder/images/mm_20_blue.png'
},
bar: {
icon: 'http://labs.google.com/ridefinder/images/mm_20_red.png'
}
};
function load() {
var map = new google.maps.Map(document.getElementById("map"), {
center: new google.maps.LatLng(47.6145, -122.3418),
zoom: 13,
mapTypeId: google.maps.MapTypeId.HYBRID
});
var infoWindow = new google.maps.InfoWindow;
// Change this depending on the name of your PHP file
downloadUrl("<?php echo $url; ?>", function(data) {
var xml = data.responseXML;
var markers = xml.documentElement.getElementsByTagName("marker");
var bounds = new google.maps.LatLngBounds();
for (var i = 0; i < markers.length; i++) {
var MSISDN = markers[i].getAttribute("lbs_msisdn");
var Time = markers[i].getAttribute("lbs_time");
var Radius = markers[i].getAttribute("distance");
var point = new google.maps.LatLng(
parseFloat(markers[i].getAttribute("lat")),
parseFloat(markers[i].getAttribute("lng")));
bounds.extend(point);
var html = "<b>" + MSISDN + "</b> <br/>" + Time;
// var icon = customIcons[type] || {};
var marker = new google.maps.Marker({
map: map,
position: point,
// icon: icon.icon
});
var cirlcle = new google.maps.Circle({
strokeColor: "#00F",
strokeOpacity: 0.8,
strokeWeight: 1,
fillColor: "#00F",
fillOpacity: 0.10,
map: map,
center: point,
radius: 1*markers[i].getAttribute("distance")
});
bindInfoWindow(marker, map, infoWindow, html);
}
map.fitBounds(bounds);
});
}
function bindInfoWindow(marker, map, infoWindow, html) {
google.maps.event.addListener(marker, 'click', function() {
infoWindow.setContent(html);
infoWindow.open(map, marker);
});
}
function downloadUrl(url, callback) {
var request = window.ActiveXObject ?
new ActiveXObject('Microsoft.XMLHTTP') :
new XMLHttpRequest;
request.onreadystatechange = function() {
if (request.readyState == 4) {
request.onreadystatechange = doNothing;
callback(request, request.status);
}
};
request.open('GET', url, true);
request.send(null);
}
function doNothing() {}
function parseXml(str) {
if (window.ActiveXObject) {
var doc = new ActiveXObject('MicrosoftXMLDOM');
doc.loadXML(str);
return doc;
} else if (window.DOMParser) {
return (new DOMParser()).parseFromString(str, 'text/xml');
}
}
google.maps.event.addDomListener(window, "load", load);
</script>
I have updated the code with the multiply in it and still not receiving the circles around the marker
Your code generates a javascript error: InvalidValueError: setRadius: not a number, because the result of markers[i].getAttribute("distance") is a string, not a number. Use parseFloat, parseInt or multiply it by a number (1*markers[i].getAttribute("distance") to solve that.
Proof of concept fiddle
code snippet:
function load() {
var map = new google.maps.Map(document.getElementById("map"), {
center: new google.maps.LatLng(47.6145, -122.3418),
zoom: 13,
mapTypeId: 'roadmap'
});
var infoWindow = new google.maps.InfoWindow;
// downloadUrl("SO_20160101.xml", function(data) {
var xml = parseXml(xmlData); // data.responseXML;
var markers = xml.documentElement.getElementsByTagName("marker");
var bounds = new google.maps.LatLngBounds();
var radius = 0;
for (var i = 0; i < markers.length; i++) {
var MSISDN = markers[i].getAttribute("lbs_msisdn");
var Time = markers[i].getAttribute("lbs_time");
var Radius = markers[i].getAttribute("distance");
var point = new google.maps.LatLng(
parseFloat(markers[i].getAttribute("lat")),
parseFloat(markers[i].getAttribute("lng")));
bounds.extend(point);
var html = "<b>" + MSISDN + "</b> <br/>" + Time;
var marker = new google.maps.Marker({
map: map,
position: point,
});
var circle = new google.maps.Circle({
strokeColor: "#00F",
strokeOpacity: 0.8,
strokeWeight: 1,
fillColor: "#00F",
fillOpacity: 0.10,
map: map,
center: point,
radius: 1 * markers[i].getAttribute("distance")
});
if (circle.getRadius() > radius) {
radius = circle.getRadius();
map.fitBounds(circle.getBounds());
}
bindInfoWindow(marker, map, infoWindow, html);
}
// map.fitBounds(bounds);
// });
}
function bindInfoWindow(marker, map, infoWindow, html) {
google.maps.event.addListener(marker, 'click', function() {
infoWindow.setContent(html);
infoWindow.open(map, marker);
});
}
function parseXml(str) {
if (window.ActiveXObject) {
var doc = new ActiveXObject('MicrosoftXMLDOM');
doc.loadXML(str);
return doc;
} else if (window.DOMParser) {
return (new DOMParser()).parseFromString(str, 'text/xml');
}
}
google.maps.event.addDomListener(window, "load", load);
var xmlData = '<markers><marker lbs_msisdn="27827910119" lbs_time="15:09:32" lat="-26.0" lng="28.0" distance="300"/><marker lbs_msisdn="27827910119" lbs_time="19:07:32" lat="-26.2726" lng="28.2179" distance="1206"/><marker lbs_msisdn="27827910119" lbs_time="19:08:56" lat="-26.2726" lng="28.2179" distance="1206"/><marker lbs_msisdn="27827910119" lbs_time="19:21:29" lat="-26.2726" lng="28.2179" distance="1206"/><marker lbs_msisdn="27827910119" lbs_time="21:58:13" lat="-26.2615" lng="28.2037" distance="148"/><marker lbs_msisdn="27827910119" lbs_time="22:01:43" lat="-26.2615" lng="28.2037" v="148"/><marker lbs_msisdn="27827910119" lbs_time="22:02:07" lat="-26.2615" lng="28.2037" distance="148"/><marker lbs_msisdn="27827910119" lbs_time="22:02:42" lat="-26.2615" lng="28.2037" distance="148"/><marker lbs_msisdn="27827910119" lbs_time="22:13:15" lat="-26.2615" lng="28.2037" distance="148"/><marker lbs_msisdn="27827910119" lbs_time="20:19:30" lat="-26.2615" lng="28.2037" distance="148"/></markers>';
html,
body,
#map {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map"></div>
I have a custom Google map with markers of artist locations. I want to make 8 different categories of markers. I read about having to make arrays of markers and assigning a category, but I honestly don't know where to start..
I think this question comes close to what I want: Toggle on/off Google map markers by category. Tried to get that working, but to no avail, I just have too little knowledge.
I have a map and two checkboxes ready, the checkboxes aren't used yet.
I'm new to google maps So.. What's the best way to create different arrays of markers and toggle their visibility with check boxes?
Please help me
Thanks
var marker;
var infowindow;
var map;
var customIcons = {
restaurant: {
icon: 'http://labs.google.com/ridefinder/images/mm_20_blue.png'
},
bar: {
icon: 'http://labs.google.com/ridefinder/images/mm_20_red.png'
}
};
var markerGroups = { "restaurant": [], "bar": []};
//alert(markerGroups);
function initialize() {
var latlng = new google.maps.LatLng(26.9200, 75.8200);
var options = {
zoom: 13,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var infoWindow = new google.maps.InfoWindow;
var map = new google.maps.Map(document.getElementById("map-canvas"), options);
var html = "<table>" +
"<tr><td>Name:</td> <td><input type='text' id='name'/> </td> </tr>" +
"<tr><td>Address:</td> <td><input type='text' id='address'/></td> </tr>" +
"<tr><td>Type:</td> <td><select id='type'>" +
"<option value='bar' SELECTED>bar</option>" +
"<option value='restaurant'>restaurant</option>" +
"</select> </td></tr>" +
"<tr><td></td><td><input type='button' value='Save & Close' onclick='saveData()'/></td></tr>";
infowindow = new google.maps.InfoWindow({
content: html
});
google.maps.event.addListener(map, "click", function(event) {
marker = new google.maps.Marker({
position: event.latLng,
map: map,
draggable: true
});
google.maps.event.addListener(marker, "click", function() {
infowindow.open(map, marker);
});
});
/*******************Start code for fetch record****************/
downloadUrl("include/phpsqlajax_genxml3.php", function(data) {
var xml = data.responseXML;
//alert(xml);
var markers = xml.documentElement.getElementsByTagName("marker");
for (var i = 0; i < markers.length; i++) {
var name = markers[i].getAttribute("name");
var address = markers[i].getAttribute("address");
var type = markers[i].getAttribute("type");
var point = new google.maps.LatLng(
parseFloat(markers[i].getAttribute("lat")),
parseFloat(markers[i].getAttribute("lng")));
var html = "<b>" + name + "</b> <br/>" + address;
var icon = customIcons[type] || {};
//alert(icon);
var marker = new google.maps.Marker({
map: map,
position: point,
icon: icon.icon,
shadow: icon.shadow
});
markerGroups[type].push(marker);
var marker = createMarker(point, name, address, type);
marker.setMap(map);
bindInfoWindow(marker, map, infoWindow, html);
}
});
/*******************End code for fetch record****************/
}
/*******************Start code for show detail****************/
function bindInfoWindow(marker, map, infoWindow, html) {
google.maps.event.addListener(marker, 'click', function() {
infoWindow.setContent(html);
infoWindow.open(map, marker);
});
}
/*******************End code for show record****************/
function saveData() {
var name = escape(document.getElementById("name").value);
var address = escape(document.getElementById("address").value);
var type = document.getElementById("type").value;
var latlng = marker.getPosition();
var url = "include/phpsqlinfo_addrow.php?name=" + name + "&address=" + address +
"&type=" + type + "&lat=" + latlng.lat() + "&lng=" + latlng.lng();
//alert(url);
downloadUrl(url, function(data, responseCode) {
if (responseCode == 200 && data.length <= 1) {
infowindow.close();
document.getElementById("message").innerHTML = "Location added.";
}
});
}
function createMarker(point, name, address, type) {
var marker = new google.maps.Marker(point, customIcons[type]);
return marker;
}
function toggleGroup(type) {
//alert(type);
for (var i = 0; i < markerGroups[type].length; i++) {
//alert(markerGroups);
var marker = markerGroups[type][i];
//alert(marker);
if (marker.getMap() === null) {
marker.setMap(map);
//alert(map);
} else {
marker.setMap(null);
}
}
}
function downloadUrl(url, callback) {
var request = window.ActiveXObject ?
new ActiveXObject('Microsoft.XMLHTTP') :
new XMLHttpRequest;
request.onreadystatechange = function() {
if (request.readyState == 4) {
request.onreadystatechange = doNothing;
callback(request, request.status);//right here
//callback(request.responseText, request.status);
}
};
request.open('GET', url, true);
request.send(null);
}
function doNothing() {}
Here's my html
<body style="margin:0px; padding:0px;" onLoad="initialize();">
<div id="map-canvas" style="width: 500px; height: 300px"></div>
<div id="message"></div>
<div id="sidebar" style="float:left; width: 120px; height: 250px; border: 1px solid black">
<input id="restaurantCheckbox" type="checkbox" checked="" onClick="toggleGroup('restaurant')">
Restaurants
<br>
<input id="barCheckbox" type="checkbox" checked="" onClick="toggleGroup('bar')">
Bars
</div>
Try using setVisible(false) instead of setMap(null).
As pointed out here setMap() will not refresh the drawing of the map and hence you will not see anything change. setMap() seems also to be the wrong choice if you want to reshow the marker later.
Edit: To summarize the comments: When using setVisible() you will have to check for the value of marker.getVisible() instead of marker.getMap().
I am using data from my SQL to draw a map, I want to place a circle of a radius also saved in my SQL around the marker
Currently I am using this to get the map and the marker.
<?php
$lats = $_REQUEST['lats'];
$longs = $_REQUEST['longs'];
$radius = $_REQUEST['radius'];
$msisdn = $_REQUEST['msisdn'];
?>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"> </script>
<script type="text/javascript">
var customIcons = {
office: {
icon: 'http://labs.google.com/ridefinder/images/mm_20_blue.png',
shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
},
house: {
icon: 'http://e-track.co.za/logo.png',
shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
}
};
function load() {
var map = new google.maps.Map(
document.getElementById("map"),
{zoom: 14, mapTypeId: google.maps.MapTypeId.HYBRID}
);
var infoWindow = new google.maps.InfoWindow;
var type = "house";
var dist = <?php echo json_encode($radius); ?>;
var lat = <?php echo json_encode($lats); ?>;
var lng = <?php echo json_encode($longs); ?>;
var msisdn = <?php echo json_encode($msisdn); ?>;
var point = new google.maps.LatLng(
parseFloat(lat),
parseFloat(lng));
var html = "<b>Cell: " + msisdn + "</b> <br/>Radius: " + dist + "m";
var icon = customIcons[type] || {};
var marker = new google.maps.Marker({
map: map,
position: point,
icon: icon.icon,
shadow: icon.shadow
});
bindInfoWindow(marker, map, infoWindow, html);
map.setCenter(new google.maps.LatLng(lat, lng));
}
function bindInfoWindow(marker, map, infoWindow, html) {
google.maps.event.addListener(marker, 'click', function() {
infoWindow.setContent(html);
infoWindow.open(map, marker);
});
}
function downloadUrl(url, callback) {
var request = window.ActiveXObject ?
new ActiveXObject('Microsoft.XMLHTTP') :
new XMLHttpRequest;
request.onreadystatechange = function() {
if (request.readyState == 4) {
request.onreadystatechange = doNothing;
callback(request, request.status);
}
};
request.open('GET', url, true);
request.send(null);
}
function doNothing() {}
</script>
I call the Longs $ Lats with the following function
<a href="../lbs_map/lbs_map.php?lats=<? echo $rows['lat']; ?>&longs=<? echo
$rows['lng']; ?>&radius=<? echo $rows['distance']; ?>&msisdn=<? echo
$rows['msisdn']; ?>&lbs_log_id=<? echo $rows['lbs_log_id']; ?>"target="_blank"
class="update"><? echo $rows['msisdn']; ?></a>
Is there away I can add the circle around the point from the radius i send out? or is it best to try xml functions which i Know nothing of
https://developers.google.com/maps/documentation/javascript/reference#CircleOptions
Essentially you do the same thing as a marker so
var cirlcle = new google.maps.Circle({
map: map,
center: point,
radius: yourRadius
});
something like this should work
var circle = new google.maps.Circle({
map: map,
radius: 50000, //your radius in meters
fillColor: '#AA0000ff'
center: point
});
I need help:
I have to create a map showing the marker when the user clicks on the input checbox.
example:
I click "hotels" and I see only the hotels.
I click no more hotels, disappearing markers on the map
The code:
<script type="text/javascript">
(function() {
window.onload = function(){
var latlng = new google.maps.LatLng(37.8530665, 15.287916300000006);
var options = {
zoom: 14,
center: latlng,
backgroundColor: '#fff',
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById('map_canvas'), options);
var markers = new Array();
$(".chek").click(function() {
if($(this).is(':checked')) {
var id_checkbox = $(this).val();
$.post("ajax.php?page=mapHome",{ id_checkbox:id_checkbox }, function(data) {
for (i=0; i < data.marker.length; i++) {
var marker = new google.maps.Marker({
position: new google.maps.LatLng(data.marker[i].latitude, data.marker[i].longitude),
animation: google.maps.Animation.DROP,
map: map,
title: data.marker[i].nome,
icon: data.marker[i].marker
});
markers[i] = marker;
markers[i].id_cat = data.marker[i].id_cat;
}// for
},"json");//json
} else {
//hide markers on the map
for (i = 0; i < markers.length; i++) {
if(id_checkbox == markers[i].id_cat) {
markers[i].setMap(null);
}
}
}
});
}
})();
</script>
I do a query through json and show results.
I see setMap(null); but no setMap(map);