My goal is to send a request using $http
with angular js in order to retrieve 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 have come across information suggesting that I need to "inject" $http
first. However, I am struggling to understand how this process works. I attempted the following:
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);
});
}
}]);
However, when trying to use it outside of this context, I receive an error message stating that "getTargetCords is not defined." Despite attempting various solutions, I am unable to grasp how to make this work.
Furthermore, here is additional code explaining why I require this function:
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);
The objective is to obtain the distance of each location from our current position.
Please keep in mind: as I have yet to finalize the angular aspect, the values of "thecoords[0]," and "thecoods[1]" are currently incorrect.