[OpenLayers-Users] map.getCenter function question

Nicholas Efremov-Kendall n.e.kendall at gmail.com
Mon Jun 20 16:01:47 EDT 2011


Hi Andreas,

Thanks for the suggestion. With the following code

var temp = map.getCenter().transform(new
OpenLayers.Projection("EPSG:4326"),map.getProjectionObject());

I now get another error, it seems to be passing a non number to the lat
field (NaN) for some reason. The full script is below...

http://search.twitter.com/search.json?=&geocode=NaN%2C-1115282612432.1%2C50km&rpp=100&callbacl=jQuery161005863115028478205_1308599949840&_=1308599980262





var feat, OpenLayers, latlon;
var tweetz = new OpenLayers.Layer.Vector("Tweetz",{styleMap: new
OpenLayers.StyleMap({externalGraphic: "OpenLayers-2.11-rc1/img/marker.png",
pointRadius: 10})});
var map = new OpenLayers.Map('map');
var tweets = [];
var tweetsQ = [];
var refreshQuery = '?=';
// Need something here for popups or marker lables
function initGIS(){
map.addControl(new OpenLayers.Control.LayerSwitcher());
var mapnik = new OpenLayers.Layer.OSM();
map.addLayer(mapnik);
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,createTWTsrcURL);
} else {
yqlgeo.get('visitor',norm_yql_resp);
}
}

function plotLoc (position){
var latlon = new
OpenLayers.LonLat(position.coords.latitude,position.coords.longitude).transform(new
OpenLayers.Projection("EPSG:4326"),map.getProjectionObject());
var marker = new OpenLayers.Layer.Markers("Marker");
map.addLayer(marker);
marker.addMarker(new OpenLayers.Marker(latlon));
map.setCenter(latlon,14);
acquireTweets();
}

function createTWTsrcURL (){
var temp = map.getCenter().transform(new
OpenLayers.Projection("EPSG:4326"),map.getProjectionObject());
return 'http://search.twitter.com/search.json' + refreshQuery + '&geocode='
+ temp.lat +'%2C' + temp.lon + '%2C50km&rpp=100&callbacl=?';
}

function acquireTweets(){
$.getJSON(createTWTsrcURL(), function(data){
if(data.results)
$.each(data.results, function(i, tweet){
if (tweet.geo || tweet.location)
tweetQ.push(tweet);
});
refreshQuery = data.refresh_url;
});
}

function parseTweetsQ(){
if (tweetsQ.length > 0) {
var tweet = tweetsQ.pop();
if (tweet.geo){
tweet.latlng = new
OpenLayers.LatLong(tweet.geo.coordinates[0],tweet.geo.coordinates[1]);
plotTweet(tweet);
}
}
}

function createInfoContent(tweet) {
var retval = '';
retval += '<div class="tweet_info">';
retval += '<img alt="' + tweet.from_user_id + '" src="' +
tweet.profile_image_url + '" class="tweet_profile"/>';
retval += '<h3>' + tweet.from_user + '</h3>';
retval += '<p>' + tweet.text + '</p>';
retval += '<p>Source: <a href="' + tweet.source + '"/>' +
tweet.source + '</a></p>';
retval += '</div>';
return retval;
}

function plotTwt(tweet){
tweet.marker = new OpenLayers.Feature.Vector(new
OpenLayers.Geometry.Point(tweet.latlng).transform(new
OpenLayers.Projection("EPSG:4326"),map.getProjectionObject()));

}

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

On Mon, Jun 20, 2011 at 2:51 PM, Andreas Hocevar <ahocevar at opengeo.org>wrote:

> Hi,
>
> try EPSG:4326 for the lat and lon you pass to the Twitter API.
>
> Andreas.
>
> On Jun 20, 2011, at 20:43 , Nicholas Efremov-Kendall wrote:
>
> > Hi all,
> >
> > Still messing around with the geolocation API. I've been playing with the
> jquery getJSON function for cross-domain requests and I'm running into an
> issue that I think is related to the coordinate system of my map, which is
> using OSM as a baselayer.
> >
> > Essentially I'm trying to apply holdener's nearby tweet's and I'm getting
> a 403 error from the getJSON function. I think this is because I'm not
> transforming the map.getCenter() properly, and I assume that the twitter API
> uses Google's EPSG 900913 as a default.
> >
> > this is the function I'm using to create the search URL
> >
> > function createTWTsrcURL (){
> > var temp = map.getCenter().transform(new
> OpenLayers.Projection("EPSG:900913"),map.getProjectionObject());
> > return 'http://search.twitter.com/search.json' + refreshQuery +
> '&geocode=' + temp.lat +'%2C' + temp.lon + '%2C50km&rpp=100&callbacl=?';
> > }
> >
> > You can see from the get string which is producing the 403 error
> (Forbidden) that the script isn't passing Lat-Longs to the search API, which
> might be causing this error.
> >
> http://search.twitter.com/search.json?=&geocode=4579425.8122326%2C-10018754.17%2C50km&rpp=100&callbacl=jQuery16105243276595138013_1308594913395&_=1308595036601
> >
> > I'm sure I'm not transforming it correctly, even if that's not what is
> causing the 403 error. Any ideas?
> > _______________________________________________
> > Users mailing list
> > Users at lists.osgeo.org
> > http://lists.osgeo.org/mailman/listinfo/openlayers-users
>
>
>
> --
> Andreas Hocevar
> OpenGeo - http://opengeo.org/
> Expert service straight from the developers.
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/openlayers-users/attachments/20110620/c69caa1a/attachment-0001.html


More information about the Users mailing list