我正在使用新的 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...
}