Created
June 23, 2014 23:33
-
-
Save stariqmi/8b4fbd51a2bca15b9498 to your computer and use it in GitHub Desktop.
Geocoded Data from Angualr service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Creating a new angular module | |
var app = angular.module("GeocodeApp", []); | |
// Angular Service | |
// Assuming the variable $GoogleAPI represents the Google API to geocode data | |
app.service("GecodeDataService", function($GoogleAPI) { | |
var that = this; | |
this.listings = {}; | |
// Set some sample listing | |
this.listings["address_1"] = {address: "address_1", html: "<p>Address 1</p>"}; | |
this.listings["address_2"] = {address: "address_2", html: "<p>Address 2</p>"}; | |
this.listings["address_3"] = {address: "address_3", html: "<p>Address 3</p>"}; | |
this.getListingCoords = function() { | |
// Use Google API as $GoogleAPI | |
for (var address in that.listings) { | |
// Make sure that the address is not an inherited property from the Object.prototype | |
if (that.listings.hasOwnProprty(address)) { | |
// Assume that $GoogleAPI takes in the address and returns the coords | |
var coords = $GoogleAPI(address); | |
// Set the coords of that particular listing | |
that.listings[address].coords = coords; | |
} | |
} | |
} | |
}); | |
// Injecting the GeocodeDataService in the controller | |
app.controller("GeocodeCtrl", function($scope, GeocodeDataService) { | |
// Call the GeocodeDataService.getListingCoords() | |
GeocodeDataService.getListingCoords(); | |
// USE THIS AS THE FINAL PRODUCT IN YOUR HTML | |
var final_listings = GeocodeDataService.listings; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment