как сделать значение в функции свойством службы angularjs - PullRequest
0 голосов
/ 03 ноября 2019

У меня есть функция с именем showPosition, которая вызывается getLocation. Теперь мне нужно сделать position.coords.latitude и position.coords.longitude как часть моих сервисных ресторанов. Но я понятия не имею, как это сделать.

Вот код для showPosition

function showPosition(position) {

    $.getJSON("http://dataservice.accuweather.com/locations/v1/cities/geoposition/search?apikey=RyDbyMAwF4gdnPcabtlH0roIGcmAXaqX&q=" + position.coords.latitude + '%2C' + position.coords.longitude, function(data) {

        jsonObject = JSON.stringify(data, null, 4);
        console.log(jsonObject);
        decodedJSON = JSON.parse(jsonObject);


        restaurants.locationOfTheUser = 'You are at' + decodedJSON['EnglishName'] + 'at latitude:' +  position.coords.latitude + 'and longitude' +  
        position.coords.longitude + ' ';
      //This does not work.

        restaurants.restaurantsNearTheUser = restaurants.names[locationOfTheUser];
        alert(locationOfTheUser);
    });
}

и вот код для getLocation

function getLocation() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showPosition);
    } else {
        alert( "Geolocation is not supported by this browser.");
    }
}

И showPosition, и getLocation определены в сервисе.

Это сервис

myApp.factory('restaurants', function() {
  var restaurants = {};
  var jsonObject, decodedJSON, locationOfTheUser;
  restaurants.names = {
    'Dr Rajkumar Ward': [
      '1947 Restaurant',
      'Malleshwaram Dose Corner',
      'Veena Stores',
      'Punjab Mail',
      'Hallimane Restaurant'
    ],
    'Yelahanka': [
      'Aahaar Dosas',
      'Onesta Yelahanka',
      'Hotel Amrapali Andhra Style',
      'Gokul Veg',
      'Beijing Bites'
    ],
    'Sampangiram Nagar' : [
    'New Krishna Bhavan',
    'The GreenPath Organic State',
     'Hotel Savi International',
     'RR Biriyani',
     'Sangeet Family Restaurant & Bar',
    ],
  };
  restaurants.getLocation = function() {
      if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(showPosition);
      } else {
          alert( "Geolocation is not supported by this browser.");
      }
  }

  console.log(restaurants.getLocation);
  function showPosition(position) {        
    $.getJSON("http://dataservice.accuweather.com/locations/v1/cities/geoposition/search?apikey=RyDbyMAwF4gdnPcabtlH0roIGcmAXaqX&q=" + position.coords.latitude + '%2C' + position.coords.longitude, function(data) {

      jsonObject = JSON.stringify(data, null, 4);
      console.log(jsonObject);
      decodedJSON = JSON.parse(jsonObject);


      restaurants.locationOfTheUser = 'You are at' + decodedJSON['EnglishName'] + 'at latitude:' +  position.coords.latitude + 'and longitude' +  
              position.coords.longitude + ' ';
      restaurants.restaurantsNearTheUser = restaurants.names[decodedJSON['EnglishName']];
      alert(restaurants.restaurantsNearTheUser);
    });
  }

  return restaurants;
});//service ends
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...