Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- angular.module('homeviewapp.controllers', [])
- .controller('HomeCtrl', function($scope, $http) {
- $scope.lat = "43.541033";
- $scope.lng = "-96.7457591";
- $scope.accuracy = "0";
- $scope.error = "";
- $scope.model = { myMap: undefined };
- $scope.listings = [];
- $scope.markers = [];
- $scope.jsonpurl = "http://app.re605.com/re605applistings/json?callback=JSON_CALLBACK";
- $scope.mapoptions = {
- zoom: 16,
- center: new google.maps.LatLng($scope.lat, $scope.lng),
- mapTypeId: google.maps.MapTypeId.ROADMAP,
- panControl: true,
- zoomControl: false,
- scaleControl: false,
- mapTypeControl: false,
- streetViewControl: false,
- overviewMapControl: false,
- scrollwheel: false,
- disableDoubleClickZoom: true
- }
- $scope.map = null;
- $scope.showResult = function () {
- return $scope.error == "";
- }
- /* get current location */
- $scope.getLocation = function () {
- /*if (navigator.geolocation) {
- navigator.geolocation.getCurrentPosition($scope.showPosition, $scope.showError);
- } else { */
- $scope.initializeMap();
- $scope.error = "Geolocation is not supported by this browser.";
- //}
- }
- $scope.showPosition = function (position) {
- $scope.lat = position.coords.latitude;
- $scope.lng = position.coords.longitude;
- $scope.mapoptions.zoom = 17;
- $scope.accuracy = position.coords.accuracy;
- $scope.mapoptions.center = new google.maps.LatLng($scope.lat, $scope.lng);
- $scope.$apply();
- $scope.initializeMap();
- }
- $scope.showError = function (error) {
- switch (error.code) {
- case error.PERMISSION_DENIED:
- $scope.error = "User denied the request for Geolocation."
- break;
- case error.POSITION_UNAVAILABLE:
- $scope.error = "Location information is unavailable."
- break;
- case error.TIMEOUT:
- $scope.error = "The request to get user location timed out."
- break;
- case error.UNKNOWN_ERROR:
- $scope.error = "An unknown error occurred."
- break;
- }
- $scope.$apply();
- }
- $scope.initializeMap = function() {
- $scope.map = new google.maps.Map(document.getElementById("map-canvas"), $scope.mapoptions);
- //$scope.doListener();
- google.maps.event.clearListeners($scope.map, 'idle');
- google.maps.event.addListener($scope.map, 'idle', function(event) {
- $scope.mapGetListingsByBounds();
- });
- }
- /* get listings by bounds */
- $scope.mapGetListingsByBounds = function() {
- //return function() {
- //$scope.map = new google.maps.Map(document.getElementById("map-canvas"), $scope.mapoptions);
- //$scope.doListener();
- var bounds = $scope.map.getBounds();
- var ne = bounds.getNorthEast();
- var sw = bounds.getSouthWest();
- var nelat = ne.lat();
- var nelng = ne.lng();
- var swlat = sw.lat();
- var swlng = sw.lng();
- //var url = "http://public-api.wordpress.com/rest/v1/sites/wtmpeachtest.wordpress.com/posts?callback=JSON_CALLBACK";
- $http.jsonp($scope.jsonpurl, {params: {
- nelat: nelat,
- nelng: nelng,
- swlat: swlat,
- swlng: swlng
- }}).success(function(data){
- //console.log(data);
- console.log("found " + data.listings.length + " listings");
- $scope.listings = data.listings;
- $scope.createMarkers();
- //console.log("retrieved jsonp listings");
- //console.log($scope.listings);
- for(x in $scope.listings) {
- var marker = new google.maps.Marker($scope.listings[x].marker);
- google.maps.event.clearListeners(marker, 'mousedown');
- google.maps.event.addListener(marker, 'mousedown', $scope.markerClicked($scope.listings[x]));
- }
- console.log("done");
- }).error(function(data, status) {
- console.log("bad");
- });
- //}
- }
- /* create markers from listings */
- $scope.createMarkers = function() {
- console.log("creating markers");
- for(x in $scope.listings) {
- //console.log(x);
- $scope.listings[x].marker = {
- position: new google.maps.LatLng($scope.listings[x].latitude, $scope.listings[x].longitude),
- map: $scope.map,
- title: $scope.listings[x].address
- };
- var preview = $scope.listings[x].summary;
- var words = preview.split(" ");
- if(words.length > 40) {
- preview = "";
- for(var i = 0; i < 40; i++) {
- preview += words[i] + " ";
- }
- preview += "...";
- }
- var img = "http://listings.re605.com/images/property/" + $scope.listings[x].mls_number + "/1.jpg";
- var elementId = "img_" + $scope.listings[x].mls_number + "_1";
- //$scope.isValidImage(img, $scope.validateImage, elementId);
- $scope.listings[x].previewBody = '<div id="content">'+
- '<div id="siteNotice">'+
- '</div>' +
- '<div class="previewHeader">' +
- '<strong class="previewAddress">' + $scope.listings[x].address + '</strong>'+
- '<span class="previewPrice">$' + $scope.getCurrency($scope.listings[x].price) + '</span>'+
- '</div>' +
- '<div class="clearfix"></div>' +
- '<div id="bodyContent">'+
- //'<p>' + $filter('currency')($scope.listings[x].price, 'XXX') + '</p>'+
- '<div class="previewListingImage"><img id="' + elementId + '" class="img-preview" src="' + img + '" /></div>'+
- '<div class="previewText"><p>' + preview + '</p></div>'+
- '</div>'+
- '</div>';
- }
- }
- /* marker click event */
- $scope.markerClicked = function(listing) {
- return function() {
- document.getElementById("btnViewDetails").href = "#/tab/home/" + listing.mls_number;
- document.getElementById("previewBody").innerHTML = listing.previewBody;
- document.getElementById("preview").style.display = "block";
- document.getElementById("map-canvas").style.height = "50%";
- // $scope.initializeMap();
- //$scope.mapSetCenter(listing);
- //console.log("clicked");
- //$scope.mapGetListingsByBounds();
- //$scope.infowindows[innerKey].open($scope.map, $scope.markers[innerKey]);
- }
- }
- /* map set center */
- $scope.mapSetCenter = function(listing) {
- // console.log("setting center...");
- //console.log(listing);
- $scope.initializeMap();
- $scope.map.setCenter(new google.maps.LatLng(listing.latitude, listing.longitude));
- //$scope.mapGetListingsByBounds();
- }
- /* preview close */
- $scope.closePreview = function() {
- document.getElementById("preview").style.display = "none";
- document.getElementById("map-canvas").style.height = "100%";
- $scope.initializeMap();
- }
- $scope.getLocation();
- $scope.getCurrency = function(amount) {
- var i = parseFloat(amount);
- if(isNaN(i)) { i = 0.00; }
- var minus = '';
- if(i < 0) { minus = '-'; }
- i = Math.abs(i);
- i = parseInt((i + .005) * 100);
- i = i / 100;
- s = new String(i);
- if(s.indexOf('.') < 0) { s += '.00'; }
- if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
- s = minus + s;
- return s;
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement