Google Maps v3 OverlayView.getProjection() [英] Google Maps v3 OverlayView.getProjection()

查看:306
本文介绍了Google Maps v3 OverlayView.getProjection()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法弄清楚为什么getProjection()返回的对象是未定义的。这里是我的代码:

  //处理矩形的完成
var ne = recBounds.getNorthEast();
var sw = recBounds.getSouthWest();
$(#map_tools_selat)。attr('value',sw.lat());
$(#map_tools_nwlat)。attr('value',ne.lat());
$(#map_tools_selng)。attr('value',ne.lng());
$(#map_tools_nwlng)。attr('value',sw.lng());

//设置缩放级别
$(#map_tools_zoomlevel)。attr('value',HAR.map.getZoom()+1);
document.getElementById(map_tools_centerLat)。value = HAR.map.getCenter()。lat();
document.getElementById(map_tools_centerLong)。value = HAR.map.getCenter()。lng();

//下面的所有垃圾都是为了获取像素坐标lat / lng = /
MyOverlay.prototype = new google.maps.OverlayView();
MyOverlay.prototype.onAdd = function(){}
MyOverlay.prototype.onRemove = function(){}
MyOverlay.prototype.draw = function(){}
function MyOverlay(地图){this.setMap(地图); }
var overlay = new MyOverlay(HAR.map);
var projection = overlay.getProjection();
// END - 所有垃圾
var p = projection.fromLatLngToContainerPixel(recBounds.getCenter());
alert(p.x +,+ p.y);

我的错误是:无法从未定义的调用方法'fromLatLngToContainerPixel'其实,我之所以会发生这种情况是因为投影对象是在平移/缩放后地图闲置后创建的。因此,更好的解决方案是监听 google.maps.Map 对象的空闲事件,并获取对该投影的引用:

  //创建您的地图并覆盖
var map;
MyOverlay.prototype = new google.maps.OverlayView();
MyOverlay.prototype.onAdd = function(){}
MyOverlay.prototype.onRemove = function(){}
MyOverlay.prototype.draw = function(){}
function MyOverlay(地图){this.setMap(地图); }

var overlay = new MyOverlay(map);
var projection;

//等待空闲地图
google.maps.event.addListener(map,'idle',function(){
//获取投影
投影= overlay.getProjection();
})


I cannot seem to figure out why the object returned by getProjection() is undefined. Here is my code:

 // Handles the completion of the rectangle
  var ne = recBounds.getNorthEast();
  var sw = recBounds.getSouthWest();
  $("#map_tools_selat").attr( 'value', sw.lat() );
  $("#map_tools_nwlat").attr( 'value', ne.lat() );
  $("#map_tools_selng").attr( 'value', ne.lng() );
  $("#map_tools_nwlng").attr( 'value', sw.lng() );

  // Set Zoom Level
  $("#map_tools_zoomlevel").attr( 'value', HAR.map.getZoom()+1 );
  document.getElementById("map_tools_centerLat").value = HAR.map.getCenter().lat();
  document.getElementById("map_tools_centerLong").value = HAR.map.getCenter().lng(); 

  // All this junk below is for getting pixel coordinates for a lat/lng  =/
  MyOverlay.prototype = new google.maps.OverlayView();
  MyOverlay.prototype.onAdd = function() { }
  MyOverlay.prototype.onRemove = function() { }
  MyOverlay.prototype.draw = function() { }
  function MyOverlay(map) { this.setMap(map); }
  var overlay = new MyOverlay(HAR.map);
  var projection = overlay.getProjection();
  // END - all the junk
  var p = projection.fromLatLngToContainerPixel(recBounds.getCenter());
  alert(p.x+", "+p.y);

My error is: Cannot call method 'fromLatLngToContainerPixel' of undefined

解决方案

Actually, i the reason why this happens is because the projection object is created after the map is idle after panning / zooming. So, a better solution is to listen on the idle event of the google.maps.Map object, and get a reference to the projection there:

// Create your map and overlay
var map;
MyOverlay.prototype = new google.maps.OverlayView();
MyOverlay.prototype.onAdd = function() { }
MyOverlay.prototype.onRemove = function() { }
MyOverlay.prototype.draw = function() { }
function MyOverlay(map) { this.setMap(map); }

var overlay = new MyOverlay(map);
var projection;

// Wait for idle map
google.maps.event.addListener(map, 'idle', function() {
   // Get projection
   projection = overlay.getProjection();
})

这篇关于Google Maps v3 OverlayView.getProjection()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆