google map - show multiple location markers
Hi all,
I'm trying to work with google api and javascript and show multiple location markers
I grabbed this code to start:
My goal is to get my address from my database and replace the hard coded addresses with my variable locations rather than theirs...
below might seem a bit odd but is example of my fail....
- I'm getting an error when I try to replace the harcoded text with my variable (with the same text)
---> this will cause error = geocode of undefined failed:OVER_QUERY_LIMIT
i think it's because it's a string not an array / object?
Q: how can I get this working and convert back as an array?
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>multi 1 Document</title>
<link rel="stylesheet" href="https://code.jquery.com/mobile/1.4.5/jquery.mobile.structure-1.4.5.min.css" />
<script src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
<script type="text/javascript"
src=
"https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false">
</script>
<style type="text/css">
html, body, #map_canvas {
height: 500px;
width: 500px;
margin: 0px;
padding: 0px
}
</style>
</head>
<body>
<script>
// try to replace this working var with my version of same - makes error
var locations = [
['Location 1 Name', 'New York, NY', 'Location 1 URL'],
['Location 2 Name', 'Newark, NJ', 'Location 2 URL'],
['Location 3 Name', 'Philadelphia, PA', 'Location 3 URL']
];
// my attempt (really comes from database addresses - this simple for now)
var locations =
"[['Location 1 Name','New York, NY','Location 1 URL'], ['Location 2 Name','Newark, NJ','Location 2 URL'], ['Location 3 Name','Philadelphia, PA','Location 3 URL']]";
localStorage.setItem("tryit", locations);
// this will cause error = geocode of undefined failed:OVER_QUERY_LIMIT
// i think it's because it's a string not an array / object?
//Q: how can I get this working and convert back as an array?
var locations = localStorage.getItem("tryit");
var geocoder;
var map;
var bounds = new google.maps.LatLngBounds();
function initialize() {
map = new google.maps.Map(
document.getElementById("map_canvas"), {
center: new google.maps.LatLng(37.4419, -122.1419),
//center: new google.maps.LatLng(34.1623449, -118.3995877),
zoom: 13,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
geocoder = new google.maps.Geocoder();
for (i = 0; i < locations.length; i++) {
geocodeAddress(locations, i);
}
}
google.maps.event.addDomListener(window, "load", initialize);
function geocodeAddress(locations, i) {
var title = locations[0];
var address = locations[1];
var url = locations[2];
geocoder.geocode({
'address': locations[1]
},
function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var marker = new google.maps.Marker({
icon: 'http://maps.google.com/mapfiles/ms/icons/blue.png',
map: map,
position: results[0].geometry.location,
title: title,
animation: google.maps.Animation.DROP,
address: address,
url: url
})
infoWindow(marker, map, title, address, url);
bounds.extend(marker.getPosition());
map.fitBounds(bounds);
} else {
alert("geocode of " + address + " failed:" + status);
}
});
}
function infoWindow(marker, map, title, address, url) {
google.maps.event.addListener(marker, 'click', function () {
var html = "<div><h3>" + title + "</h3><p>" + address + "<br></div><a href='" + url + "'>View location</a></p></div>";
iw = new google.maps.InfoWindow({
content: html,
maxWidth: 350
});
iw.open(map, marker);
});
}
function createMarker(results) {
var marker = new google.maps.Marker({
icon: 'http://maps.google.com/mapfiles/ms/icons/blue.png',
map: map,
position: results[0].geometry.location,
title: title,
animation: google.maps.Animation.DROP,
address: address,
url: url
})
bounds.extend(marker.getPosition());
map.fitBounds(bounds);
infoWindow(marker, map, title, address, url);
return marker;
}
</script>
<div id="map_canvas" style="width:750px; height:450px; border: 2px solid #3872ac;"></div>
</body>
</html>
