'Uncaught InvalidValueError' (: setCenter: not a LatLng or LatLngLiteral object)
It is also worth noting that Google Maps v3 doesn't appear to sanitize the data values for setting markers. This seems to have changed in the last few weeks, because previously I was passing in icon height as a string for google's MarkerImage function (it is coming from JSON). But that suddenly broke because it wasn't a "number".
Obviously the solution is to pass in a number, or wrap your data in parseInt()
or parseFloat()
so that the API gets a number or float. Google "should" be sanitizing it's data but it isn't.
var custom_icon = new google.maps.MarkerImage(
icon_path,
new google.maps.Size(
parseInt(icon_options.width),
parseInt(icon_options.height)
),
new google.maps.Point(
parseFloat(icon_options.origin_x),
parseFloat(icon_options.origin_y)
),
new google.maps.Point(
parseFloat(icon_options.anchor_x),
parseFloat(icon_options.anchor_y)
)
);
You are initializing mapOptions
in wrong way.
Initialize it in following way..
var mapOptions = {
center: new google.maps.LatLng(20.68177501, -103.3514794),
zoom: 15,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
Instead of...
var mapOptions = {
center: (20.68177501, -103.3514794), //this is not correct
zoom: 15,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
Following is corrected code...
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<style type="text/css">
html { height: 100% }
body { height: 100%; margin: 0; padding: 0 }
#map-canvas { height: 100% }
</style>
<script type="text/javascript"
src="https://maps.googleapis.com/maps/api/js?AIzaSyBVfO8LckdOHAot1a8rZW0bmJIoWO2A3os=API_KEY&sensor=true">
</script>
<script type="text/javascript">
function initialize() {
var mapOptions = {
center: new google.maps.LatLng(20.68177501, -103.3514794),
zoom: 15,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map-canvas"),
mapOptions);
// To add the marker to the map, use the 'map' property
<?php $i = 0; ?>
<?php foreach($locations as $key => $value): ?>
<?php foreach($value as $key => $value): ?>
<?php foreach($value as $key => $value): ?>
<?php if($key == "Latitude"): ?>
<?php $myLatLng = "$value, "; endif;?>
<?php if($key == "Longitude"): ?>
<?php $myLatLng .="$value"; ?>
var myLatlng<?php echo $i; ?> = new google.maps.LatLng(<?php echo $myLatLng; ?>);
var marker<?php echo $i; ?> = new google.maps.Marker({
position: (myLatlng<?php echo $i; ?>),
title:"Hello World!"
});
marker<?php echo $i; ?>.setMap(map);
<?php $i++; ?>
<?php endif; ?>
<?php endforeach; ?>
<?php endforeach; ?>
<?php endforeach; ?>
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<div id="map-canvas"/>
</body>
</html>