我正在使用新的Google地球集成代码http://code.google.com/p/smap-suite/source/browse/fieldAnalysis2/trunk/WebContent/js/libs/google_earth_integration.js?r=1442和Google地图Api与地图V3的组合。当前结果输出到表格时,用户可以单击该表格并打开corressponding标记和infowindow。目前,这在Maps V3中完美运行,但无法获得infowindow的锚点以找到正确的位置。
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');
}
});
我需要创建一个函数,该函数将定位在表格中单击的结果的Lat和Long,并设置google earth窗口的锚点,同时还可以在地图上点击地标并在搜索栏中输入地址。
请参阅此处的示例: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...
}
答案 0 :(得分: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
});
}