javascript - 从 map 中的点坐标查找具有名称的谷歌地图地点

标签 javascript c# asp.net google-maps

我使用来 self 的数据库的坐标创建我的谷歌地图。 在我的表中,我有在我的 asp.net 页面中用 google 指针指向的坐标。但现在我想用城市名称进行搜索,我在数据库中有搜索名称的坐标,并在谷歌地图中用颜色突出显示 我有以下代码来获取我的数据库坐标:

<script type="text/javascript">
        var markers = [
        <asp:Repeater ID="rptMarkers" runat="server">
        <ItemTemplate>
                 {
                     <%--''<%# GetImageFromByte(DataBinder.Eval(Container.DataItem, "Image")) %>' '--%>
                     "title":"Survey",
                     <%--   "title": '<%# Eval("Name") %>',--%>
                     "lat": '<%# Eval("lat") %>',
                     "lng": '<%# Eval("Long") %>',
                     "description": "<div> <img src='' alt='Alternate Text1' /></div>",
                     "image": "<div> <img src='<%#Eval("photo")%>' width='150px' alt='Picture not available for this survey' /></div>",
                     "Name":'<%# Eval("Name")%>',
                     "PhoneNumber":'<%# Eval("PhoneNumber")%>'
                 }
    </ItemTemplate>
    <SeparatorTemplate>
        ,
    </SeparatorTemplate>
    </asp:Repeater>
    ];
    </script>
    <script type="text/javascript">

        window.onload = function () {
            var mapOptions = {
                center: new google.maps.LatLng(markers[0].lat, markers[0].lng),
                zoom: 8,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            };
            var infoWindow = new google.maps.InfoWindow();
            var map = new google.maps.Map(document.getElementById("dvMap"), mapOptions);
            for (i = 0; i < markers.length; i++) {
                var data = markers[i]
                var myLatlng = new google.maps.LatLng(data.lat, data.lng);
                var marker = new google.maps.Marker({
                    position: myLatlng,
                    map: map,
                    title: data.title
                });
                (function (marker, data) {
                    google.maps.event.addListener(marker, "click", function (e) {
                        infoWindow.setContent(data.description+data.image+data.Name+ 
                           '<br/>'+ data.PhoneNumber);

                        infoWindow.open(map, marker);
                    });
                })(marker, data);
            }
        }
</script>

最佳答案

要解决这个问题,首先您必须检查该标记当前是否存在于 map 上。使用以下方法可以知道所搜索的城市在 map 上是否有经纬度。

如果 map 上存在搜索城市的坐标,则此方法将返回true,否则将返回false

map.getBounds().contains(marker.getPosition());

一旦返回 true,您就可以为其分配一些颜色。

希望有帮助!!

关于javascript - 从 map 中的点坐标查找具有名称的谷歌地图地点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29804245/

相关文章:

c# - 动态给图像 C# 加水印的最有效方法?

javascript - 计算嵌套 json 中选择的元素数量

javascript - 如何只删除对象的值?

C# UNICODE 到 ANSI 转换

asp.net - 为选中的 CheckBoxField 设置默认 bool 值

c# - 我怎样才能得到按钮的命令参数?

JAVASCRIPT:IE:outerHTML

javascript - HTML 如何阻止用户输入大于 2100-01-01 的日期?

c# - 如何在 .NET 中使用 CNG(或支持 AES-NI 的指令集)?

c# - 没有堆栈跟踪的错误详细信息