I’m trying to do two things from the code below:
- If the geocode is not successfull don’t show the map (so far I’ve only hidden the error message.)
- If it is successful only load the address and not the original latlng before reloading the address.
You’ll have to excuse all the single quote marks, the javascript loads under a php echo.
Any ideas welcome, ideally I’d like to handle it in the javascript but don’t mind a bit of php if needed, I’m looking to use this in a few areas of the site.
echo '
<script type="text/javascript">
var geocoder;
var map;
function initialize() {
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(51.500141,-0.125195);
var address = \''.$company.', '.$address_l1.', '.$address_l2.', '.$address_l3.', '.$town.', '.$county.', '.$post_code.', '.$country.'\';
var myOptions = {
zoom: 16,
scrollwheel: false,
navigationControl: false,
mapTypeControl: false,
scaleControl: false,
streetViewControl: false,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
geocoder.geocode( { \'address\': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
} else {
//alert("Geocode was not successful for the following reason: " + status);
}
});
}
</script>
<div id="map_canvas"></div>
';
Move the map creation into the success function of geocode and use document.getElementById(‘map_canvas’).style.display=’block’ or none to show/hide the map on fail.