I want to show map in each WordPress post. Location will be vary, address will get fetched from database. This code is generating the result, but not working properly.
- Map Maker is missing.
- Zoom option is not working or I can't see it visually.
This function need initialize() mentioned in body tag so I have customized my body tag as:
<body onload="initialize()">
Here is the script I am using:
var geocoder;
var map;
var address ="<?php echo $address;?>";
function initialize() {
geocoder = new google.maps.Geocoder();
var myOptions = {
zoom: 16,
center: new google.maps.LatLng(-33, 151),
mapTypeControl: true,
mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU},
navigationControl: true,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
if (geocoder) {
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (status != google.maps.GeocoderStatus.ZERO_RESULTS) {
map.setCenter(results[0].geometry.location);
var infowindow = new google.maps.InfoWindow(
{ content: '<b>'+address+'</b>',
size: new google.maps.Size(150,50)
});
var marker = new google.maps.Marker({
position: results[0].geometry.location,
map: map,
title:address
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.open(map,marker);
});
} else {
alert("No results found");
}
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
}
I am using Div which show result in WordPress post loop:
<div id="map_canvas" style="width:470px; height:358px"></div>
This code is working perfect while used in HTML file, because of my poor knowledge I am unable to figure out why it is not working when div is in loop