Reputation: 31
edit: Fixed it by moving the
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBjEILUPsJO83wPyTKZlscBCMO-Aajy6Kw&signed_in=true&libraries=geometry, places&callback=initMap"></script>
to after the js script call. then fixed the bugs i added by attempting to fix my code before. thanks for the help!
I've been working on re-organizing my code so the project will be more robust (vs how all the javascript was embedded within the html file), however when moving it to a new javascript file and calling the script I receive the error "Uncaught TypeError: window.initMap is not a function".
If there's any tips you can offer me/fixes to this error that'd be super appreciated!
the HTML Code:
<!DOCTYPE html>
{% load staticfiles %}
<html>
<head>
<title>{% block title %} RacksOnRacks{% endblock %}</title>
<link rel="stylesheet" href="{% static "css/racks.css" %}" />
<script src="//code.jquery.com/jquery-1.11.2.min.js"></script> <!-- not sure what this is-->
{% block script_block %}{% endblock %}
</head>
<body>
<div id="menu">
<div id="logo" onclick="location.href='/';" style="cursor:pointer;">
<img id="logoimg" src="{% static "images/temprackicon.png" %}"/>
{# TODO make this match onclick initmap#}
</div>
<div id="title" onclick="location.href='/';" style="cursor:pointer;">
RACKS ON RACKS
</div>
</div>
{% block body_block %}{% endblock %}
</body>
</html>
Other HTML File
{% extends 'racks/baselayer.html' %}
{% load staticfiles %}
{% block title %} Racks On Racks: Find Nearby Places to Secure Your Bike! {% endblock %}
{% block script_block %}
<link rel="stylesheet" href="{% static "map/css/maps-admin.css" %}" />
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBjEILUPsJO83wPyTKZlscBCMO-Aajy6Kw&signed_in=true&libraries=geometry, places&callback=initMap"></script>
<script type="text/javascript" src="{% static "map/javascript/googlemaps.js" %}"></script>
{% endblock %}
{% block body_block %}
<select id="filters">
<option value="100">100m</option>
<option value="250">250m</option>
<option value="500">500m</option>
<option value="1000">1000m</option>
</select>
<div id="map_canvas"></div>
{% endblock %}
and the JS:
function initMap(){
var myLatLngGlobal;
var map;
var filterDistance;
var self = {
// starts all the processes
//function placeRackMarkers(locations, map) {
initialize: function () {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function (position) {
var myLatLng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
myLatLngGlobal = myLatLng;
}, function () {
handleLocationError(true, infoWindow, map.getCenter());
});
} else {
// Browser doesn't support Geolocation
handleLocationError(false, infoWindow, map.getCenter());
}
var zoom = 12;
//var filterDistance = 1000;
//var latlng = new google.maps.LatLng(lat, lng);
//myLatLngGlobal = latlng;
var options = {
zoom: zoom,
center: myLatLngGlobal,
//mapTypeId:
};
map = new google.maps.Map(document.getElementById("map_canvas"), options);
var marker = new google.maps.Marker({
position: myLatLngGlobal,
map: map,
title: 'Hello World!',
draggable: false,
animation: google.maps.Animation.DROP
});
self.attachHandlers();
self.displayRacks();
//add all the intialiazing functions (self.(....)
},
//Event handlers attached
attachHandlers: function () {
$("#filterDistance").click(function () {
filterDistance = "#filterDistance";
});
//console.log("filterDistance = " + self.filterDistance);
},
/*
var filterDistance1 = document.getElementById("filters").value; //put outside of self var if this doesnt work
console.log("filterDistance =" + filterDistance1);
filterDistance = filterDistance1;
*/
displayRacks: function () {
var locations;
$.get('/racks/map_locations/', {}, function (data) {
locations = data['racks'];
filterPlaceRacks(locations, myLatLngGlobal, map);
});
}
};
function placeRackMarkers(locations, map) {
var marker, i;
for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
map: map
});
}
}
function filterPlaceRacks(racks, map) {
var filteredRacks = [];
for (var i = 0; i < racks.length; i++) {
if (checkDistance(racks[i]) <= self.filterDistance) {
filteredRacks.push(racks[i]);
console.log(filteredRacks);
}
}
placeRackMarkers(filteredRacks, map);
}
function checkDistance(rack) {
var rackLatLng = new google.maps.LatLng(rack[1], rack[2]);
return (google.maps.geometry.spherical.computeDistanceBetween(rackLatLng, myLatLngGlobal));
}
return self;
}
$(document).ready(function () {
var googleMap = initMap();
googleMap.initialize();
});
Thanks again! PS. Sorry for the poorly structured code, still a work in progress.
Also using a django framework, and ajax with jquery
Upvotes: 3
Views: 5134
Reputation: 1237
I got lost somewhere along the way since my bread and butter server-side is php, but if this used to work, then you probably messed up the order in which the scripts are supposed to load (a function is being called that has not been loaded yet)
Try to call each part of js in the order that they used to be called before
Your <!-- not sure what this is--> comment, that one is a library for javascript (jQuery) which you will probably need to load before all other scripts on the page.
P/S i'm sorry for not being that much of a help but i don't have time to setup django right now to test things out.
Upvotes: 1