Undefined function - Uncaught Referenceerror: codeAddress is not defined

Asked

Viewed 51 times

0

I’m running a script to get the location of some accredited stores of the company I work for (using the Google Maps JS API). The code works in 2 pages normally, but on the last page where I will implement this function, it is giving this error.. The code in JS is this:

<script>








var geocoder = null;
var map = null;
var customerMarker = null;
var gmarkers = [];
var closest = [];

function initialize() {
  // alert("init");
  geocoder = new google.maps.Geocoder();
  map = new google.maps.Map(document.getElementById('map'), {
    zoom: 9,
    center: new google.maps.LatLng(14.730751, -89.940637),    /* 52.6699927, -0.7274620*/
    mapTypeId: google.maps.MapTypeId.ROADMAP
  });
  var infowindow = new google.maps.InfoWindow();
  var marker, i;
  var bounds = new google.maps.LatLngBounds();
  for (i = 0; i < locations.length; i++) {
    var coordStr = locations[i][4];
    var coords = coordStr.split(",");
    var pt = new google.maps.LatLng(parseFloat(coords[0]), parseFloat(coords[1]));
    bounds.extend(pt);
    marker = new google.maps.Marker({
      position: pt,
      map: map,
      icon: locations[i][5],
      address: locations[i][2],
      title: locations[i][0],
      html: locations[i][0] + "<br>" + locations[i][2]
    });
    gmarkers.push(marker);
    google.maps.event.addListener(marker, 'click', (function(marker, i) {
        return function() {
          infowindow.setContent(marker.html);
          infowindow.open(map, marker);
        }
      })
      (marker, i));
  }
  map.fitBounds(bounds);

}

function codeAddress() {
  var numberOfResults = 25;
  var numberOfDrivingResults = 5;
  var address = document.getElementById('address').value;
  geocoder.geocode({
    'address': address
  }, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
      map.setCenter(results[0].geometry.location);
      if (customerMarker) customerMarker.setMap(null);
      customerMarker = new google.maps.Marker({
        map: map,
        position: results[0].geometry.location
      });
      closest = findClosestN(results[0].geometry.location, numberOfResults);
      // get driving distance
      closest = closest.splice(0, numberOfResults);
      calculateDistances(results[0].geometry.location, closest, numberOfDrivingResults);
    } else {
      alert('Geocode was not successful for the following reason: ' + status);
    }
  });
}

function findClosestN(pt, numberOfResults) {
  var closest = [];
  /*document.getElementById('info').innerHTML += "processing " + gmarkers.length + "<br>";*/
  for (var i = 0; i < gmarkers.length; i++) {
    gmarkers[i].distance = google.maps.geometry.spherical.computeDistanceBetween(pt, gmarkers[i].getPosition());
    /*document.getElementById('info').innerHTML += "process " + i + ":" + gmarkers[i].getPosition().toUrlValue(6) + ":" + gmarkers[i].distance.toFixed(2) + "<br>";*/
    gmarkers[i].setMap(null);
    closest.push(gmarkers[i]);
  }
  closest.sort(sortByDist);
  return closest;
}

function sortByDist(a, b) {
  return (a.distance - b.distance)
}

function calculateDistances(pt, closest, numberOfResults) {
  var service = new google.maps.DistanceMatrixService();
  var request = {
    origins: [pt],
    destinations: [],
    travelMode: google.maps.TravelMode.DRIVING,
    unitSystem: google.maps.UnitSystem.METRIC,
    avoidHighways: false,
    avoidTolls: false
  };
  for (var i = 0; i < closest.length; i++) {
    request.destinations.push(closest[i].getPosition());
  }
  service.getDistanceMatrix(request, function(response, status) {
    if (status != google.maps.DistanceMatrixStatus.OK) {
      alert('Error was: ' + status);
    } else {
      var origins = response.originAddresses;
      var destinations = response.destinationAddresses;
      var outputDiv = document.getElementById('side_bar');
      outputDiv.innerHTML = '';

      var results = response.rows[0].elements;
      // save title and address in record for sorting
      for (var i = 0; i < closest.length; i++) {
        results[i].title = closest[i].title;
        results[i].address = closest[i].address;
        results[i].idx_closestMark = i;
      }
      results.sort(sortByDistDM);
      debugger;
      
      
      
      for (var i = 0; ((i < numberOfResults) && (i < closest.length)); i++) { 
        if (results[i].adress == "R. Indalécio Rezende, 850 - Jardim Primavera"){
          
        outputDiv.innerHTML +=  " <div class='espaco'   style='margin-left:40px; margin-top:10px'> <a href='javascript:google.maps.event.trigger(closest[" + results[i].idx_closestMark  + shows_cred_pf() + "],\"click\");'>" + results[i].title + '</a><br>' +   results[i].address + "<br>" + results[i].distance.text + ' aproximadamente ' + results[i].duration.text + '<br><hr></div>';
        
     
        closest[i].setMap(map);
        i = numberOfResults;
   
       /* outputDiv.innerHTML +=  " <div class='espaco'   style='margin-left:40px; margin-top:10px'> <a href='javascript:google.maps.event.trigger(closest[" + results[i].idx_closestMark  + "],\"click\");'>" + results[i].title + '</a><br>' +   results[i].address + "<br>" + results[i].distance.text + ' aproximadamente ' + results[i].duration.text + '<br><hr></div>';
        
  
        closest[i].setMap(map);
        i = numberOfResults;
*/

      }
    }
  });
}

function sortByDistDM(a, b) {
  return (a.distance.value - b.distance.value)
}

google.maps.event.addDomListener(window, 'load', initialize);
// Store Name[0],delivery[1],Address[2],Delivery Zone[3],Coordinates[4] data from FusionTables pizza stores example
var locations = [
  

    
      ["Rio Claro - Rio Pneus", "no", "R. Quatorze, 2180 - Vila Elizabeth (Bnh)", "<coordinates></coordinates>", "-22.403572, -47.573710" , "http://maps.google.com/mapfiles/ms/icons/blue.png"],
      ["Porto Ferreira - Zanon Centro Automotivo", "no", "R. Indalécio Rezende, 850 - Jardim Primavera", "<coordinates></coordinates>", "-21.858959, -47.488320" , "http://maps.google.com/mapfiles/ms/icons/blue.png"],
      ["Pirassununga - Bosch Multicenter", "no", "Av. Padre Antônio Vann Ess, 1566 - Rosário", "<coordinates></coordinates>", "-21.998977, -47.448747" , "http://maps.google.com/mapfiles/ms/icons/blue.png"],
      ["Limeira - TS Pneus Frotas", "no", "R. André Scarpa, 8 - Jardim Sao Pedro", "<coordinates></coordinates>", "-22.578118, -47.387098" , "http://maps.google.com/mapfiles/ms/icons/blue.png"],
      ["Cordeirópolis - Deluca Auto Center", "no", "R. João Batista Spanhol, 655 - Bombocado", "<coordinates></coordinates>", "-22.496744, -47.450244" , "http://maps.google.com/mapfiles/ms/icons/blue.png"],
      ["Araras - Casa do Amortecedor", "no", "Av. Loreto, 919 - Jardim das Flores", "<coordinates></coordinates>", "-22.360491, -47.366686" , "http://maps.google.com/mapfiles/ms/icons/blue.png"],
      ["Campinas - TS Pneus", "no", "Rua Salto Grande, 55 - Jardim do Trevo", "<coordinates></coordinates>", "-22.928338, -47.067516" , "http://maps.google.com/mapfiles/ms/icons/blue.png"],
   
    
 /*["John Paul's Pizzeria", "no", "1100 El Camino Real, Belmont, CA", "<Polygon><outerBoundaryIs><LinearRing><coordinates>-122.304268,37.516534,0 -122.300835,37.505096,0 -122.262383,37.481669,0 -122.242813,37.502917,0 -122.244186,37.534232,0 -122.269249,37.550021,0 -122.291222,37.545122,0 -122.302895,37.537499,0 -122.304268,37.516534,0 </coordinates></LinearRing></outerBoundaryIs></Polygon>", "37.520436,-122.275978", "http://maps.google.com/mapfiles/ms/icons/red.png"],
  */ 

  
  
  
  
]; 
</script>


The mistake is this:
Uncaught Referenceerror: codeAddress is not defined At Htmlbuttonelement.onclick (offer)


and he’s on this line:

<button type="button" class="btn btn-primary btn-lg btn-block" onclick="codeAddress();" style="margin-top: 7%; width: 80%; height: 130px; background-color: #FDCB1A;font-size: 150%; color: #0E4090; font-weight: 700; letter-spacing: 2px" >BUSCAR CREDENCIADA MAIS PRÓXIMA</button>
  • only with this it is difficult to know... the script is included in the page? saw in the browser tool if there is an error to load the script? puts the script here and see if it shows errors: http://beautifytools.com/javascript-validator.php

  • yes the script is on the page, no error to load, the strange thing is that on the other pages it works perfectly, only in that (I know it is not the ideal way, put 2 identical scripts in 2 different pages, instead of simply calling you when necessary, but this is how it is done), and the errors that the site accused are practically unimpressive things, as "Expected an Identifier and Instead saw '<'."

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.