[OpenLayers-Users] Map.setcenter over google with reprojection

Nicholas Efremov-Kendall n.e.kendall at gmail.com
Sat Jun 4 15:33:08 EDT 2011


Hello all,

I have a quick question regarding the map.setcenter function. I'm piecing
together a script, and at the moment, I just want it to center my map on
these particular coordinates 39.427,-91.324, prior to implementing the
geolocation feature. I'm not sure what I'm doing wrong, but the map seems to
zoom to the max extent, which shows empty map space on the top and bottom of
the screen. So, I'm not sure what I'm doing wrong with this, but I'd
appreciate any pointers, or help.
best, code is below, the section I'm concerned with is at line 33.

Thanks in advance.

function getLoc(){
if (Modernizr.geolocation) {
 navigator.geolocation.getCurrentPosition(showMap,handle_error);
} else {
yqlgeo.get('visitor',norm_yql_resp);
}
}


function showMap(position)
{
alert('Lat: ' + position.coords.latitude + ' ' + 'Lon: ' +
position.coords.longitude + ' ' + 'Acc: ' + position.coords.accuracy);

var map = new OpenLayers.Map('map',
{controls:[
new OpenLayers.Control.Navigation(),
new OpenLayers.Control.PanZoomBar(),
new OpenLayers.Control.LayerSwitcher(),
new OpenLayers.Control.Attribution(),
new OpenLayers.Control.OverviewMap()],
sphericalMercator: true,
  maxExtent: new
OpenLayers.Bounds(-20037508.34,-20037508.34,20037508.34,20037508.34),
  maxResolution: 156543.0399,
  numZoomLevels: 21,
  units: 'm',
  projection: new OpenLayers.Projection("EPSG:900913"),
  displayProjection: new OpenLayers.Projection("EPSG:4326")
}
);

var google_map = new OpenLayers.Layer.Google('Google Hybrid',{type:
google.maps.MapTypeId.HYBRID});
    map.addLayer(google_map);
map.setCenter(new OpenLayers.LonLat(39.427,-91.324).transform(new
OpenLayers.Projection("EPSG:4326"),map.getProjectionObject(),16));
}

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/20110604/37f6aaed/attachment.html


More information about the Users mailing list