I am trying to make a $http request with angular js to get a json object from google maps.
$http.get('http://maps.googleapis.com/maps/api/geocode/json?address=' + data[ 'street' ] + ',' + data[ 'city' ] + ',Deutschland' + '&sensor=true').success(function(mapData) {
angular.extend($scope, mapData);
});
I read that I need to "inject" $http first, but I just cant get my head around how that works? I tried to do something like this:
angular.module('wmw', [])
.run(['$scope', '$http', function($scope, $http){
function getTargetCords(data, $scope, $http) {
var city = data[ 'city' ];
return $http.get('http://maps.googleapis.com/maps/api/geocode/json?address=' + data[ 'street' ] + ',' + data[ 'city' ] + ',Deutschland' + '&sensor=true').success(function(mapData) {
angular.extend($scope, mapData);
});
}
}]);
But in this case it is saying "getTargetCords is not defined" when I am trying to use it outside of this. I tried multiple different solutions and cant get my head around how to get this to work.
Edit: I think the reason why I need this function is maybe quite confusing, so here is my other code:
var onSuccess = function(position) {
currentLat = position.coords.latitude ;
currentLng = position.coords.longitude;
var thecoords = [];
$('#filter-list').empty();
for(i = 0; i<locations.length;i++){
thecoords = getTargetCords(locations[i]);
var distance = calculateDistance(currentLat, currentLng, thecoords[0], thecoords[1]);
addItemToList(locations[i], distance);
}
};
// onError Callback receives a PositionError object
function onError(error) {
alert('Aktueller Standort konnte nicht berechnet werden');
}
navigator.geolocation.getCurrentPosition(onSuccess, onError);
We have different locations and I need the distance for each location.
Please note: as I didn't quite finish the angular part, the "thecoords[0]", "thecoods[1]" are obviously wrong values right now