Advertisement
mrmikey

Google Maps Angular Marker issue

Jun 12th, 2014
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. angular.module('homeviewapp.controllers', [])
  2. .controller('HomeCtrl', function($scope, $http) {
  3.     $scope.lat = "43.541033";
  4.     $scope.lng = "-96.7457591";
  5.     $scope.accuracy = "0";
  6.     $scope.error = "";
  7.     $scope.model = { myMap: undefined };
  8.     $scope.listings = [];
  9.     $scope.markers = [];
  10.  
  11.     $scope.jsonpurl = "http://app.re605.com/re605applistings/json?callback=JSON_CALLBACK";
  12.  
  13.     $scope.mapoptions = {
  14.         zoom: 16,
  15.         center: new google.maps.LatLng($scope.lat, $scope.lng),
  16.         mapTypeId: google.maps.MapTypeId.ROADMAP,
  17.         panControl: true,
  18.         zoomControl: false,
  19.         scaleControl: false,
  20.         mapTypeControl: false,
  21.         streetViewControl: false,
  22.         overviewMapControl: false,
  23.         scrollwheel: false,
  24.         disableDoubleClickZoom: true
  25.     }
  26.  
  27.     $scope.map = null;
  28.  
  29.     $scope.showResult = function () {
  30.         return $scope.error == "";
  31.     }
  32.  
  33.     /* get current location */
  34.  
  35.     $scope.getLocation = function () {
  36.         /*if (navigator.geolocation) {
  37.             navigator.geolocation.getCurrentPosition($scope.showPosition, $scope.showError);
  38.         } else {   */        
  39.             $scope.initializeMap();
  40.             $scope.error = "Geolocation is not supported by this browser.";
  41.         //}
  42.     }
  43.  
  44.     $scope.showPosition = function (position) {
  45.         $scope.lat = position.coords.latitude;
  46.         $scope.lng = position.coords.longitude;
  47.         $scope.mapoptions.zoom = 17;
  48.         $scope.accuracy = position.coords.accuracy;
  49.         $scope.mapoptions.center = new google.maps.LatLng($scope.lat, $scope.lng);
  50.  
  51.         $scope.$apply();
  52.  
  53.         $scope.initializeMap();
  54.     }
  55.  
  56.     $scope.showError = function (error) {
  57.         switch (error.code) {
  58.             case error.PERMISSION_DENIED:
  59.                 $scope.error = "User denied the request for Geolocation."
  60.                 break;
  61.             case error.POSITION_UNAVAILABLE:
  62.                 $scope.error = "Location information is unavailable."
  63.                 break;
  64.             case error.TIMEOUT:
  65.                 $scope.error = "The request to get user location timed out."
  66.                 break;
  67.             case error.UNKNOWN_ERROR:
  68.                 $scope.error = "An unknown error occurred."
  69.                 break;
  70.         }
  71.         $scope.$apply();
  72.     }
  73.  
  74.     $scope.initializeMap = function() {
  75.         $scope.map = new google.maps.Map(document.getElementById("map-canvas"), $scope.mapoptions);
  76.         //$scope.doListener();
  77.  
  78.         google.maps.event.clearListeners($scope.map, 'idle');
  79.         google.maps.event.addListener($scope.map, 'idle', function(event) {
  80.             $scope.mapGetListingsByBounds();
  81.         });
  82.     }
  83.  
  84.     /* get listings by bounds */
  85.  
  86.     $scope.mapGetListingsByBounds = function() {
  87.         //return function() {
  88.             //$scope.map = new google.maps.Map(document.getElementById("map-canvas"), $scope.mapoptions);
  89.             //$scope.doListener();
  90.             var bounds = $scope.map.getBounds();
  91.  
  92.             var ne = bounds.getNorthEast();
  93.             var sw = bounds.getSouthWest();
  94.  
  95.             var nelat = ne.lat();
  96.             var nelng = ne.lng();
  97.             var swlat = sw.lat();
  98.             var swlng = sw.lng();
  99.  
  100.             //var url = "http://public-api.wordpress.com/rest/v1/sites/wtmpeachtest.wordpress.com/posts?callback=JSON_CALLBACK";
  101.  
  102.             $http.jsonp($scope.jsonpurl, {params: {
  103.                     nelat: nelat,
  104.                     nelng: nelng,
  105.                     swlat: swlat,
  106.                     swlng: swlng
  107.                 }}).success(function(data){
  108.                 //console.log(data);
  109.                 console.log("found " + data.listings.length + " listings");
  110.                 $scope.listings = data.listings;
  111.  
  112.                 $scope.createMarkers();
  113.  
  114.                 //console.log("retrieved jsonp listings");
  115.                 //console.log($scope.listings);
  116.  
  117.                 for(x in $scope.listings) {
  118.                     var marker = new google.maps.Marker($scope.listings[x].marker);
  119.                     google.maps.event.clearListeners(marker, 'mousedown');
  120.                     google.maps.event.addListener(marker, 'mousedown', $scope.markerClicked($scope.listings[x]));
  121.                 }
  122.                 console.log("done");
  123.             }).error(function(data, status) {
  124.                 console.log("bad");
  125.             });
  126.  
  127.            
  128.         //}
  129.        
  130.     }
  131.  
  132.     /* create markers from listings */
  133.  
  134.     $scope.createMarkers = function() {
  135.         console.log("creating markers");
  136.  
  137.         for(x in $scope.listings) {
  138.             //console.log(x);
  139.             $scope.listings[x].marker = {
  140.                 position: new google.maps.LatLng($scope.listings[x].latitude, $scope.listings[x].longitude),
  141.                 map: $scope.map,
  142.                 title: $scope.listings[x].address
  143.             };
  144.  
  145.             var preview = $scope.listings[x].summary;
  146.             var words = preview.split(" ");
  147.             if(words.length > 40) {
  148.                 preview = "";
  149.                 for(var i = 0; i < 40; i++) {
  150.                     preview += words[i] + " ";
  151.                 }
  152.                 preview += "...";
  153.             }
  154.  
  155.             var img = "http://listings.re605.com/images/property/" + $scope.listings[x].mls_number + "/1.jpg";          
  156.             var elementId = "img_" + $scope.listings[x].mls_number + "_1";  
  157.             //$scope.isValidImage(img, $scope.validateImage, elementId);
  158.  
  159.             $scope.listings[x].previewBody = '<div id="content">'+
  160.                 '<div id="siteNotice">'+
  161.                 '</div>' +
  162.                 '<div class="previewHeader">' +
  163.                 '<strong class="previewAddress">' + $scope.listings[x].address + '</strong>'+
  164.                 '<span class="previewPrice">$' + $scope.getCurrency($scope.listings[x].price) + '</span>'+
  165.                 '</div>' +
  166.                 '<div class="clearfix"></div>' +
  167.                 '<div id="bodyContent">'+
  168.  
  169.                 //'<p>' + $filter('currency')($scope.listings[x].price, 'XXX') + '</p>'+
  170.                
  171.                 '<div class="previewListingImage"><img id="' + elementId + '" class="img-preview" src="' + img + '" /></div>'+
  172.                 '<div class="previewText"><p>' + preview + '</p></div>'+
  173.                 '</div>'+
  174.                 '</div>';
  175.         }
  176.     }
  177.  
  178.     /* marker click event */
  179.  
  180.     $scope.markerClicked = function(listing) {
  181.         return function() {
  182.             document.getElementById("btnViewDetails").href = "#/tab/home/" + listing.mls_number;
  183.             document.getElementById("previewBody").innerHTML = listing.previewBody;
  184.             document.getElementById("preview").style.display = "block";
  185.             document.getElementById("map-canvas").style.height = "50%";
  186.  
  187.            // $scope.initializeMap();
  188.             //$scope.mapSetCenter(listing);
  189.             //console.log("clicked");
  190.             //$scope.mapGetListingsByBounds();
  191.             //$scope.infowindows[innerKey].open($scope.map, $scope.markers[innerKey]);
  192.         }
  193.     }
  194.  
  195.     /* map set center */
  196.  
  197.     $scope.mapSetCenter = function(listing) {
  198.        // console.log("setting center...");
  199.         //console.log(listing);
  200.         $scope.initializeMap();
  201.         $scope.map.setCenter(new google.maps.LatLng(listing.latitude, listing.longitude));
  202.  
  203.         //$scope.mapGetListingsByBounds();
  204.     }
  205.  
  206.     /* preview close */
  207.  
  208.     $scope.closePreview = function() {
  209.         document.getElementById("preview").style.display = "none";
  210.         document.getElementById("map-canvas").style.height = "100%";
  211.         $scope.initializeMap();
  212.     }
  213.  
  214.     $scope.getLocation();
  215.  
  216.     $scope.getCurrency = function(amount) {
  217.         var i = parseFloat(amount);
  218.         if(isNaN(i)) { i = 0.00; }
  219.         var minus = '';
  220.         if(i < 0) { minus = '-'; }
  221.         i = Math.abs(i);
  222.         i = parseInt((i + .005) * 100);
  223.         i = i / 100;
  224.         s = new String(i);
  225.         if(s.indexOf('.') < 0) { s += '.00'; }
  226.         if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
  227.         s = minus + s;
  228.         return s;
  229.     }
  230. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement