1

I am working on a small app that plots points on a Google Map using their maps API (coupled with their geolocation API).

Here is my code:

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");
    }
  });
}

When I load the page, about every 2 seconds the page auto-refreshes and the url looks like: http://localhost:3000/[object%20Object]... not sure what I am doing wrong

4

1 回答 1

4

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

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

于 2014-03-12T01:02:56.687 回答