I'm trying to throw lat/long data taken from the user's location into the Google Maps API on page load but I'm stuck.
Getting an injection error. I'm pretty new to Angular and still don't entirely grok how the scope thing works, so be gentle.
My main file app.js is in a top level directory:
var app = angular.module('ForecastApp', []);
In a controllers directory I have the main controller that's calling a getZip function from below:
app.controller('ForecastController', ['$scope', 'forecast', function($scope, forecast) {
$scope.getZip = function(){
forecast.getZip($scope.zipFinder).then(function(response){
$scope.response = response.data;
});
}
}]);
I have a separate top level file, getLocation.js, that is grabbing the lat long data from the navigator:
function getLocation(position) {
navigator.geolocation.getCurrentPosition(getLocation);
var lat = position.coords.latitude;
var lng = position.coords.longitude;
var $latlng = lat + "," + lng; // will the variable carry over to the factory?
}
Finally a factory file where I'm trying include the lat lng data into a http.get request from the API. How do I get the latlng variable here? I have a feeling this is all messed up. Maybe I don't need the factory?
app.factory('forecast', ['$http', function($http) {
function getZip(zipFinder) {
$http.get('https://maps.googleapis.com/maps/api/geocode/json?latlng=' + $latlng + '&key=XXXXXXXXXX')
.success(function(data){
return data;
})
}
}]);
The html:
<body onload="getLocation()" ng-app="ForecastApp">
<div ng-controller="ForecastController">
<div class="zipFinder" ng-model="zipFinder">
<h3>Your zip code is {{ zipFinder.result_type | postal_code }}</h3>
</div>
</div>
</body
I was considering combining any getLocation.js and the factory to get the latlng variable into scope but it wasn't doing it. There must be a way to get those two together.
Thanks in advance.