[OpenLayers-Users] layer is undefined
Nicholas Efremov-Kendall
n.e.kendall at gmail.com
Sat Sep 29 14:35:23 PDT 2012
Hi everyone,
I apologize in advance for what is probably a simple fix and spaghetti
code, but I'm starting to get bug eyed here. I'm trying to grab near-by
tweets using jquery getJSON. I had this working to where the script was
showing my new tweet locations, but somewhere in the last few iterations my
code started adding the 'tweetz' layer as undefined (it's labeled
'undefined in the layer switcher. I've run the code through jslint and
firebug, and there weren't obvious errors. Thanks again, I've stripped the
code of my comments and white space.
Code below:
var feat, OpenLayers, latlon, selectedFeature, selectControl,
selectedFeature;
var tweetz = new OpenLayers.Layer.Vector("Tweets", {styleMap: new
OpenLayers.StyleMap({externalGraphic:
"js/OpenLayers-2.11-rc1/img/marker-green.png", pointRadius: 10})});
var marker = new OpenLayers.Layer.Vector("Marker", {styleMap: new
OpenLayers.StyleMap({externalGraphic:
"js/OpenLayers-2.11-rc1/img/marker.png", pointRadius: 10})});
var map = new OpenLayers.Map('map_tweet');
var tweets = [];
var tweetsQ = [];
var refreshQuery = '?=';
function init_tweet(){
map.addControl(new OpenLayers.Control.LayerSwitcher());
var mapnik = new OpenLayers.Layer.OSM();
map.addLayer(mapnik);
var tweetz = new OpenLayers.Layer.Vector();
map.addLayer(tweetz);
map.setCenter(new OpenLayers.LonLat(-90, 38).transform(new
OpenLayers.Projection("EPSG:4326"),map.getProjectionObject()),12);
$(document).everyTime('30s', acquireTweets);
$(document).everyTime('100ms', parseTweetsQ);
getLoc();
}
//jQuery(window).ready(function(){jQuery("#btnInit").click(getLoc);});
function getLoc(){
if (Modernizr.geolocation) {
navigator.geolocation.getCurrentPosition(plotLoc,handle_error);
} else {
yqlgeo.get('visitor',norm_yql_resp);
}
}
function createTWTsrcURL (){
var temp = map.getCenter().transform(new
OpenLayers.Projection("EPSG:900913"),new
OpenLayers.Projection("EPSG:4326"));
return 'http://search.twitter.com/search.json' + refreshQuery + '&geocode='
+ temp.lat +'%2C' + temp.lon + '%2C50km&rpp=100&callback=?';
}
function plotLoc (position){
map.addLayer(marker);
var feat = new OpenLayers.Feature.Vector(new
OpenLayers.Geometry.Point(position.coords.longitude,position.coords.latitude).transform(new
OpenLayers.Projection("EPSG:4326"),map.getProjectionObject()),11);
marker.addFeatures([feat]);
var bounds = marker.getDataExtent();
map.setCenter(bounds.getCenterLonLat(),10);
acquireTweets();
}
function acquireTweets(){
$.getJSON(createTWTsrcURL(), function(data){
if(data.results)
$.each(data.results, function(i, tweet){
if (tweet.geo || tweet.location)
tweetsQ.push(tweet);
});
refreshQuery = data.refresh_url;
});
}
function parseTweetsQ(){
if (tweetsQ.length > 0) {
var tweet = tweetsQ.pop();
if (tweet.geo){
var lon = tweet.geo.coordinates[1];
var lat = tweet.geo.coordinates[0];
tweet.point = new OpenLayers.Geometry.Point(lon,lat).transform(new
OpenLayers.Projection("EPSG:4326"),map.getProjectionObject());
plotTwt(tweet);}}}
function plotTwt(tweet){
var twtfeat = new OpenLayers.Feature.Vector(tweet.point);
tweetz.addFeatures(twtfeat);
}
function handle_error(error) {
switch(error.code){case error.PERMISSION_DENIED: alert("This application
reguires you to agree to share your location. Please reload the page to see
it work properly. There is a privacy disclaimer at the bottom of this page.
This site does not currently record or store these data, and in the future
will not record unique user information without informing the user or
asking their permission. -Virtual Spatiality");
break;case error.POSITION_UNAVAILABLE: alert("could not determine
position");
break;case error.TIMEOUT: alert("timeout retrieving position");
break;default: alert("unknown error");
break;}}
function norm_yql_resp(response){if (response.error) {var error = {code :
0};handle_error(error);return;}var position = {coords:{latitude:
response.place.centroid.latitude,longitude:
response.place.centroid.longitude},address :{city:
response.place.locality2.content,region:
response.place.admin1.content,country:
response.place.country.content}};handle_geolocation_query(position);}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/openlayers-users/attachments/20120929/dce22a4f/attachment.html>
More information about the Users
mailing list