Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialJason Hegarty
2,487 PointsAdding Point Markers to a Map tutorial
I'm really struggling with getting the map to display the correct location and I also cannot get the marker to display at all.
The js code is the same (I think) as is taught in the video but the rest is different as I'm using it in a real world situation.
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript"
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAtrlBrH5md22zETMw91iVZ38XxgpAlT4o&sensor=false">
</script>
<script type="text/javascript">
$(".contactpage").prepend('<div id="map_canvas"></div>');
$(".static_map").remove();
var map;
var bounds;
var geocoder;
var center;
function initialize() {
var mapOptions = {
center: new google.maps.LatLng(-34.397, 150.644),
zoom: 8,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map_canvas"),
mapOptions);
geocoder = new google.maps.Geocoder();
bounds = new google.maps.LatLngBounds();
}
function addMarkerToMap(location){
var marker = new google.maps.Marker({map: map, position: location});
bounds.extend(location);
map.fitBounds(bounds);
}
initialize();
$("address").each(function(){
var $address = $(this);
geocoder.geocode({address: $address.text()}, function(results,status){
if(status == google.maps.GeocoderStatus.OK) addMarkerToMap(results[0].geometry.location);
});
});
google.maps.event.addDomListener(map, "idle", function(){
center = map.getCenter();
});
$(window).resize(function(){
map.setCenter(center);
});
</script>
Sorry for the lengthy paste of code but I'm pulling my hair out and feel like I need to be declaring more variables but the video doesn't say so ... HELP!
2 Answers
Andrew Chalkley
Treehouse Guest TeacherCan you share your full page in a code pen?
Jason Hegarty
2,487 PointsHi Andrew sorry for the late reply I managed to figure out what was wrong....
I was trying to follow along using my own markup which didn't contain an address element. Added and working like a charm.
Andrew Chalkley
Treehouse Guest TeacherGreat job!
Debugging is a pain but worth it! :)