如何从数据库中获取谷歌地图用户的位置?

时间:2017-01-11 08:42:35

标签: c# asp.net google-maps google-maps-api-3 gps

我的代码是根据经度和纬度显示地图,但我想根据商店的位置和类型(示例药房,医院)显示地图,可以从数据库中检索。

正文内容

DateValue

页面加载

<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>
    <script type="text/javascript">
        var markers = [
        <asp:Repeater ID="rptMarkers" runat="server">
    <ItemTemplate>
             {
                 "title": '<%# Eval("Name") %>',
                 "Type": '<%# Eval("Type") %>',
                 "Location": '<%# Eval("Location") %>',
                 "description": '<%# Eval("Description") %>'
             }
    </ItemTemplate>
    <SeparatorTemplate>
        ,
    </SeparatorTemplate>
    </asp:Repeater>
        ];
        window.onload = function () {
            LoadMap();
        }
        function LoadMap() {
            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 latlngbounds = new google.maps.LatLngBounds();
            var map = new google.maps.Map(document.getElementById("dvMap"), mapOptions);
            var legend = document.getElementById("legend");
            legend.innerHTML = "";
            for (var 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,
                    icon: data.icon
                });
                (function (marker, data) {
                    google.maps.event.addListener(marker, "click", function (e) {
                        infoWindow.setContent("<div style = 'width:100px;height:40px'>" + data.description + "</div>");
                        infoWindow.open(map, marker);
                    });
                })(marker, data);
                latlngbounds.extend(marker.position);

                legend.innerHTML += "<div style = 'margin:5px'><img align = 'middle' src = '" + marker.icon + "' />&nbsp;" + marker.title + "</div>";
            }
            var bounds = new google.maps.LatLngBounds();
            map.setCenter(latlngbounds.getCenter());
            map.fitBounds(latlngbounds);
        }

    </script>
    <table border="0" cellpadding="0" cellspacing="0">
        <tr>
            <td>
                <div id="dvMap" style="width: 500px; height: 500px">
                </div>
            </td>
            <td id="legend" style="display:none;">
            </td>
            <script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAV8AiebjdcoS-Ratewz-HDkFt7XCq3zOM&libraries=places&callback=initMap"></script>
        </tr>
    </table>
</body>

获取数据

protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            DataTable dt = this.GetData("select * from GetLocations");
            rptMarkers.DataSource = dt;
            rptMarkers.DataBind();
        }
    }

Image

1 个答案:

答案 0 :(得分:0)

您可以将Lat和经度添加到数据库中,然后将其与您的代码一起显示,您可以在此处找到示例 Lat long Sample

Lat long lookup by Place Name sample

这里是反向查找服务的示例 Reverse lookup service code