0

我正在使用新的 Google 地球集成代码http://code.google.com/p/smap-suite/source/browse/fieldAnalysis2/trunk/WebContent/js/libs/google_earth_integration.js?r=1442和带有地图 V3 的 Google Places Api。将当前结果输出到表格后,用户可以单击表格,相应的标记和信息窗口将打开。目前这在 Maps V3 中运行良好,但无法让信息窗口的锚点定位在正确的位置。

function addResult(result, i) {
var results = document.getElementById("results");
var tr = document.createElement('tr');
tr.style.backgroundColor = (i% 2 == 0 ? '#F0F0F0' : '#FFFFFF');
tr.onclick = function() {
  google.maps.event.trigger(markers[i], 'click'); //end add marker listener

        }  

google.maps.event.addListener(this.map_,
                            GoogleEarth.INFO_WINDOW_OPENED_EVENT_,
                            function(infowindow) {
//If Earth is open, create balloon
if (!that.earthVisible_) {
  return;
}
    var balloon = that.instance_.createHtmlStringBalloon('');
    //TODO assuming anchor == marker == lastclicked

    var placemark = that.lastClickedPlacemark_;
    balloon.setFeature(placemark);
    balloon.setContentString(infowindow.getContent());
    that.instance_.setBalloon(balloon);
 });

 // On click of a placemark we want to trigger the map click event.
google.earth.addEventListener(ge.getGlobe(), 'click', function(event) {
var target = event.getTarget();
var overlay = that.placemarks_[target.getId()];
if (overlay) {
  event.preventDefault();
  // Close any currently opened map info windows.
  var infoWindows = that.getOverlaysForType_('InfoWindow');
  for (var i = 0, infoWindow; infoWindow = infoWindows[i]; i++) {
    infoWindow.close();
  }
  that.lastClickedPlacemark_ = target;
  google.maps.event.trigger(overlay, 'click');
}
});

我需要创建一个函数来定位在表格中点击的结果的纬度和经度,并为谷歌地球设置窗口的锚点,同时还可以处理在地图上点击的地标和在搜索栏中输入的地址。

请参阅此处的示例:http: //go3dexpansion.com/attempt101.html

编辑

好的,所以我正在尝试这样做,以便当您单击结果表时,它将打开正确的气球并将其连接到正确的地标。你在帮助我更多地了解我真正在做什么方面提供了巨大的帮助。请看一下这段代码,让我知道我能做什么。jsfiddle 上还有另一部分代码控制气球出现的时间和位置,并且基于 lastClickPlacemark_ 变量。

///My Attempt at the code
var getPosition = function(marker) {
var lat = marker.position.$a;
var lng = marker.position.ab;
var myLatlng = new google.maps.LatLng(lat, lng);


    TODO assuming anchor == marker == lastclicked
var marker = new google.maps.Marker({
  position: myLatlng,
  map: map
});
var balloon = googleEarth.instance_.createHtmlStringBalloon('');
    var placemark = marker;
    balloon.setFeature(placemark);
    balloon.setContentString(infowindow.getContent());
    googleEarth.instance_.setBalloon(balloon);

   // alert(lat + ',' + lng); // do something with it...
 }



 // The code that currently controls the window open and is based on the lastClickPlacemark_ variable


 GoogleEarth.prototype.addEarthEvents_ = function() {
 var ge = this.instance_;
 ge.getWindow().setVisibility(true);

 // add a navigation control
 var navControl = ge.getNavigationControl();
 navControl.setVisibility(ge.VISIBILITY_AUTO);

 var screen = navControl.getScreenXY(); 
 screen.setYUnits(ge.UNITS_INSET_PIXELS);
 screen.setXUnits(ge.UNITS_PIXELS);

 // add some layers
 var layerRoot = ge.getLayerRoot();
 layerRoot.enableLayerById(ge.LAYER_BORDERS, true);
 layerRoot.enableLayerById(ge.LAYER_ROADS, true);

 var that = this;
 google.maps.event.addListener(this.map_,
                            GoogleEarth.INFO_WINDOW_OPENED_EVENT_,
                            function(infowindow) {
//If Earth is open, create balloon
if (!that.earthVisible_) {
  return;
}
    var balloon = that.instance_.createHtmlStringBalloon('');
    //TODO assuming anchor == marker == lastclicked

    var placemark = that.lastClickedPlacemark_;
    balloon.setFeature(placemark);
    balloon.setContentString(infowindow.getContent());
    that.instance_.setBalloon(balloon);
});

// On click of a placemark we want to trigger the map click event.
google.earth.addEventListener(ge.getGlobe(), 'click', function(event) {
var target = event.getTarget();
var overlay = that.placemarks_[target.getId()];
if (overlay) {
  event.preventDefault();
  // Close any currently opened map info windows.
  var infoWindows = that.getOverlaysForType_('InfoWindow');
  for (var i = 0, infoWindow; infoWindow = infoWindows[i]; i++) {
    infoWindow.close();
  }
  that.lastClickedPlacemark_ = target;
  google.maps.event.trigger(overlay, 'click');
}
 });

};








//SOMETHING LIKE THIS?????


var getPosition = function(marker) {
            ge = googleEarth.instance_; 

var lat = marker.position.$a;
var lng = marker.position.ab;
 // var myLatlng = new google.maps.LatLng(lat, lng);
 var placemark = ge.getElementById(marker);   
    var balloon = ge.createHtmlStringBalloon(''); 
      balloon.setFeature(placemark); 
      ge.setBalloon(balloon); 


   // alert(lat + ',' + lng); // do something with it...
}
4

1 回答 1

0

一个函数...

“这将找到在表格中单击的结果的纬度和经度”

可能是这样的......

var getPosition = function(marker) {
    var lat = marker.position.$a;
    var lng = marker.position.ab;
    alert(lat + ',' + lng); // do something with it...
}

您可以在此处调用它,以便获得与表中项目对应的特征的位置...

tr.onclick = function() {
  google.maps.event.trigger(markers[i], 'click'); //end add marker listener
  getPosition(markers[i]); // get the position
}  

第二部分不是很清楚...

同时还为点击地图上的地标工作......

如果我理解您,那么您可以修改您的标记事件侦听器以在地图上单击标记时调用相同的函数。

google.maps.event.addListener(markers[i], 'click', function() {
  getDetails(results[i], i);
  getPosition(markers[i]); // get the position
});

最后...

和在搜索栏中输入的地址。

没有任何意义 - 除非您对地址进行地理编码,否则地址没有位置 - 而且我在您的页面中看不到任何搜索栏!

编辑

“我只需要弄清楚如何应用这些单独的 lat 和 lng 值并将它们设置为地标”

同样,我不是 100% 确定我理解,但如果你想用你的数据实际创建一个新的标记,你可以轻松地修改函数......

var createMarker = function(marker) {
    var lat = marker.position.$a;
    var lng = marker.position.ab;
    var myLatlng = new google.maps.LatLng(lat, lng);
    var marker = new google.maps.Marker({
      position: myLatlng,
      map: map
    });
}
于 2012-12-01T00:58:28.390 回答