google.maps.event.addDomListener(window, "load", initMap);
//Array of all map markers
var markers = [];
var mxTracks = [
["Brighton, UK", 0, 50.82253000000001, -0.13716299999998682],
["Cambridge, UK", 1, 52.205337, 0.12181699999996454]
];
// Loading Map
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
center: {
lat: 54.188,
lng: -2.878
},
zoom: 6
});
//Creating a Geocoder object
var geocoder = new google.maps.Geocoder();
//Calling a
document.getElementById('submit').addEventListener('click', function() {
geocodeAddress(geocoder, map);
});
//Geocoding function
function geocodeAddress(geocoder, resultsMap) {
//Removing all markers form the map each time this function is called
console.log(markers.length);
for (var i = 0; i < markers.length; i++) {
markers[i].setMap(null);
//
console.log(markers.length);
}
markers = [];
var address = document.getElementById('address').value;
var distance = document.getElementById('distance').value;
geocoder.geocode({
'address': address
}, function(results, status) {
if (status === 'OK') {
resultsMap.setCenter(results[0].geometry.location);
var userMarker = new google.maps.Marker({
map: resultsMap,
position: results[0].geometry.location,
// icon: 'img/bluedot.png'
});
markers.push(userMarker);
//Creating track markers
for (var i = 0; i < mxTracks.length; i++) {
var trackMarker = new google.maps.Marker({
position: new google.maps.LatLng(mxTracks[i][2], mxTracks[i][3])
});
markers.push(trackMarker);
//If markers are within a set distance add them to a map
if (google.maps.geometry.spherical.computeDistanceBetween(userMarker.getPosition(), trackMarker.getPosition()) < parseInt(distance) * 1609.34) {
trackMarker.setMap(resultsMap);
...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Fiddle Pages
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Get Fiddle Pages with PRO
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.