如何在文本搜索后返回long / lat

时间:2012-09-10 20:18:11

标签: javascript html bing-maps

示例可在以下网址找到:

http://projects.snowshtechnologies.com/findhrr/_admin/_geolocate/content.php

查看完整创意的来源。

我试图让JS在文本搜索结果重新定位地图后立即填充long / lat字段。

链接的Map脚本有一个提交功能,它将long lat返回给父窗体(不包含在此链接中 - 所以不要担心!)

这里有两种主要方法:

首先是文本搜索 - 用于确定输入地址的地图位置。所需的修复位于此处,在用户单击搜索后,地图返回位置 - 一切都很好。但我现在还需要在文本区域中显示long / lat并启用提交按钮..您可以在下一个方法中看到该功能:

第二种方法只允许用户拖动地图,最后点击的位置将填充lon / lat字段。

txt搜索功能:

function FindLoc(numResults)
{
    try
    {
       results = map.Find(document.getElementById('txtWhat').value,
                      document.getElementById('txtWhere').value,
                      null,
                      null,
                      index,
                      numResults,
                      true,
                      true,
                      true,
                      true,
                      MoreResults);
        index = parseInt(index)+9;

    }
    catch(e)
    {
        alert(e.message);
    }

    map.AttachEvent("onchange", DisplayCoords);
}

我有一个函数displayCoords,它会将点击的长/纬度位置输出到页面,我已将它包含在文本搜索功能中,但没有任何乐趣。

map.AttachEvent("onchange", DisplayCoords);

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我无法更改您的来源,因此我无法对此进行测试,但是,我相信您要执行以下操作:

1)摆脱FindLoc中的“map.AttachEvent”,因为这会在每次用户执行查找时附加一个新事件,无论如何它不是地图类型的正确事件名称所以它不会是触发。有效事件列表为here

2)更改MoreResults回调,使其读取resultsArray参数,该参数是查找结果的数组,并从数组元素中读取LatLong属性。该类记录为here。例如,像:

var pos;
if (resultsArray && resultsArray.length > 0) {
    pos = resultsArray[0].LatLong;
    if (pos) {
        document.getElementById("mapLatitude").value = pos.Latitude;
        document.getElementById("mapLongitude").value = pos.Longitude;
        document.getElementById('getLongLat').style.display='block';
    }
}

编辑:resultsArray似乎总是为null,因此您需要使用places数组。将其添加到MoreResults的开头:

if (places && places.length > 0) {
    document.getElementById("mapLatitude").value = places[0].LatLong.Latitude;
    document.getElementById("mapLongitude").value = places[0].LatLong.Longitude;
    document.getElementById('getLongLat').style.display='block';
}

我测试了这个替代方案,它对我有用。

相关问题