【问题标题】:MeteorJS Infinite Page RefreshMeteorJS 无限页面刷新
【发布时间】:2014-03-12 00:32:23
【问题描述】:

我正在开发一个小应用程序,该应用程序使用他们的地图 API(加上他们的地理定位 API)在谷歌地图上绘制点。

这是我的代码:

map.js:

Template.gmap.rendered = function() {
    if(! Session.get('map'))
        gmaps.initialize();

  gmaps.mapCities();
}

Template.gmap.destroyed = function() {
    Session.set('map', false);
}

gmap.js:

gmaps = {
    map: null,

    initialize: function() {
        console.log("[+] Initializing Google Maps...");

        var mapOptions = {
            zoom: 4,
            center: new google.maps.LatLng(38.7, -95),
            mapTypeId: google.maps.MapTypeId.ROADMAP,
            mapTypeControl: false,
            streetViewControl: false,
            draggable: false,
            zoomControl: true,
            zoomControlOptions: {
                style: google.maps.ZoomControlStyle.SMALL
            }
        };

        this.map = new google.maps.Map(
            document.getElementById('map-canvas'),
            mapOptions
        );

        this.map.setOptions({styles: window.mapStyles});

        Session.set('mapLoaded', true);
    },

    mapCities: function(){
      var t = this;
      console.log("[+] city mapping begins");
      $.each(cities, function(index, c){
        city_name = c.split(", ").join("+");
        city = Cities.findOne({city_name: city_name});

        if(!city) {
          console.log("NO CITY FOUND. SAVING DA CITY: " + city_name);

          Meteor.call("retrieveLatLng", city_name, function(error, json){
            results = json.data.results[0];
            location = results.geometry.location;
            id = createCity(city_name, location);
            city = Cities.findOne({_id: id});
            t.addMarker(city, city.id);
          });
        } else {
          t.addMarker(city, city.id);
        }

      })
    },

    _parseGeoLocation: function(job) {
        var location, t = this;

        Meteor.call("retrieveLatLng", job.address, function(error, json){
            results = json.data.results[0];
            location = results.geometry.location;
            t.addMarker(location, job.id, true);
        });
    },

    addMarker: function(marker, id, update) {
        var gLatLng = new google.maps.LatLng(marker.lat, marker.lng);

        if(typeof(update) !== "undefined" && update) {
          Jobs.update(
            {_id: id},
            {$set: {lat: location.lat, lng: location.lng}}
          );
        }

        var gMarker = new google.maps.Marker({
            position: gLatLng,
            map: this.map,
            title: marker.title,
            icon: 'http://maps.google.com/mapfiles/ms/icons/blue-dot.png'
        });

        return gMarker;
    },

    jobExists: function(jvid) {
        return Jobs.findOne({jvid: id});
    }
}

main.js:

if (Meteor.isServer) {

  Meteor.methods({
    retrieveJobs: function(url){
      this.unblock();
      var url = url + "&cn=100";
      return HTTP.get("http://api.us.jobs/?key=API_KEY_HERE&" + url);
    },
    retrieveLatLng: function(address){
      this.unblock();
      return HTTP.get("https://maps.googleapis.com/maps/api/geocode/json?address=" + address + "&components=country:US&sensor=false&key=API_KEY_HERE");
    }
  });
}

当我加载页面时,页面大约每 2 秒自动刷新一次,并且 url 看起来像:http://localhost:3000/[object%20Object]... 不知道我做错了什么

【问题讨论】:

    标签: google-maps-api-3 meteor google-geolocation


    【解决方案1】:

    您正在设置变量location,即window.location。因此,您告诉浏览器替换其位置/重定向到位置对象的方式。 (因此,为什么您将 [object Object] 视为您被重定向到的位置)

    如果您为 location 变量使用不同的名称,例如 var location_var,应该没问题。

    【讨论】:

      猜你喜欢
      • 2021-11-06
      • 1970-01-01
      • 1970-01-01
      • 2016-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-10
      • 1970-01-01
      相关资源
      最近更新 更多