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

How to get the long/lat returned after a text search

本文关键字:返回 long lat 搜索 文本      更新时间:2023-09-26

示例可以在以下位置找到:

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

查看完整想法的来源。

一旦文本搜索结果重新定位了地图,我就试图让JS填充long/lat字段。

链接的Map脚本有一个提交功能,可以将长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) 去掉FindLoc中的"map.AttachEvent",因为每次用户执行查找时,它都会附加一个新事件,而且它不是映射类型的正确事件名称,因此不会触发它。有效事件列表在这里。

2) 更改MoreResults回调,使其读取resultsArray参数(查找结果的数组),并从数组元素中读取LatLong属性。这个类记录在这里。例如:

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';
}

我测试了这个替代方案,它确实对我有效。