Search code examples
javascripthtmlangularjsgoogle-mapsurl-routing

Google Maps Routing W/ AngularJS


I have created an app that uses Google Maps, the map showed up before I did my routing. But after routing it wont display. Here is my code for the app.
JS:
search-controller.js

(function() {
  searchController.$inject = ['$scope'];
  function searchController($scope) {

    $scope.ASiteLocs = [There Is Code In here that is too long and irrelevant to the map];
    $scope.SSiteLocs = [""];
    $scope.SiteLocs = $scope.SSiteLocs.concat($scope.ASiteLocs);

    angular.forEach($scope.SiteLocs, function(location) {
      var clength = location.Point.coordinates.length;
      if (location.Point.coordinates.substring(clength - 2, clength) === ",0") {
        location.Point.coordinates = location.Point.coordinates.substring(0, clength - 2).split(",");
        Lat = location.Point.coordinates[0];
        Lon = location.Point.coordinates[1];
        Com = ",";
        location.Point.coordinates = Lon.concat(Com, Lat);
      }

      angular.forEach($scope.SSiteLocs, function(object) {
        object.carrier = 'Sprint';
      });
      angular.forEach($scope.ASiteLocs, function(object) {
        object.carrier = 'AT&T';
      });

    });
  }

  angular.module("siteLookUpApplication").controller('searchController', searchController);
}());

map-controller.js

(function() {

  mapController.$inject = ['$scope', '$routeParams'];
  function mapController($scope, $routeParams) {
    function initialize() {
      var mapOptions = {
        center: site.Point.coordinates,
        zoom: 5
      };
      var map = new google.maps.Map(document.getElementById("map-canvas"),
        mapOptions);
    }
    google.maps.event.addDomListener(window, 'load', initialize);

    // Save the locationName in the url to the scope
    $scope.locationName = $routeParams.locationName;
  }

  angular.module("siteLookUpApplication").controller("mapController", mapController);
}());

app.js

(function() {
  angular.module("siteLookUpApplication", ["ngRoute"]);

  angular.module("siteLookUpApplication").config(function($routeProvider) {
    $routeProvider
      .when("/search", {
        templateUrl: "search.html",
        controller: "searchController"
      })
      .when("/map/:locationName", {
        templateUrl: "map.html",
        controller: "mapController"
      })
      .otherwise({
        redirectTo: '/search'
      });
  });
}());

HTML:
map.html

<style>
  html { height: 100% }
  body { height: 100%; margin: 0; padding: 0 }
  #map-canvas { height: 100%
  background:#fff;}
</style>
<div>
    <div><a ng-href="#/search">Back To Search</a></div>
    <p>Map for {{locationName}}</p>
    <div id="map-canvs"></div>
</div>

index.html

<!DOCTYPE html>
<html ng-app="siteLookUpApplication">
  <head>
    <link href='http://fonts.googleapis.com/css?family=Droid+Serif' rel='stylesheet' type='text/css'>
    <link rel="stylesheet" href="style.css" type='text/css'/>
    <script data-require="angular.js@*" data-semver="1.2.17" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.17/angular.js"></script>
    <script data-require="jquery@*" data-semver="2.1.1" src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.js"></script>
    <script data-require="google-maps@1.0.0" data-semver="1.0.0" src="http://maps.google.com/maps/api/js?sensor=false"></script>
    <script data-require="angular-route@*" data-semver="1.2.17" src="http://code.angularjs.org/1.2.17/angular-route.js"></script>
    <script src="app.js"></script>
    <script src="map-controller.js"></script>
    <script src="search-controller.js"></script>
    <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyC0wdLb9-Os4YVxn_JR2sY08xEN-1aJkMM"></script>
    <title>Site ID</title>
  </head>

  <body link="white" vlink="white">
    <div class="text-center">
      <h1>Site Finder</h1>
      <div id="map-canvas"></div>
      <div ng-view></div>      
    </div>
  </body>
</html>

search.html

<div>
      <input type="text" ng-model="search" border="1" placeholder="Please enter site name..." />

      <table border="1" width="100%">
        <thead>
          <tr>
            <td>Name</td>
            <td>Carrier</td>
          </tr>
        </thead>
        <tbody>
          <tr ng-repeat="site in SiteLocs | filter : search">
            <td>
              <a ng-href="#/map/{{site.name}}">  
              {{site.name}}
              </a>
            </td>
            <td>
              {{site.carrier}}
            </td>
          </tr>
        </tbody>
      </table>
    </div>

Here is a plunk with my project if that is easier: http://plnkr.co/edit/AiVc6nccqke8Jluonpxl?p=info


Solution

  • You have a few problems:

    • The Google Maps api is loaded twice
    • Google Maps should be loaded before Angular
    • jQuery should be loaded before Angular
    • In the map.html the div id has a typo
    • In the maps controller "site" is unresolved (for the center property)
    • map-canvas exists in the index.html (this should be removed)
    • In the map controller the initialize function is declared but never called
    • The CSS on the actual map element is a little wonky and will not display -- for testing put a static height on it or something

    App Structure notes:

    • When using document and window you should instead inject $document and $window
    • Perhaps you can use a directive for the map instead of a controller -- see http://angular-google-maps.org/
    • The filtered list looks pretty long consider using ReactJS or pagination.