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

时间:2023-02-05 20:16:56

I've code to show map as per longitude and latitude, but instead of that, I want to show map as per location and type of the store(example pharmacy, hospital) which could be retrieved from database.

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

Body Content

<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>

Page load

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

Get Data

private DataTable GetData(string query)
    {
        string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        SqlCommand cmd = new SqlCommand(query);
        using (SqlConnection con = new SqlConnection(conString))
        {
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.Connection = con;

                sda.SelectCommand = cmd;
                using (DataTable dt = new DataTable())
                {
                    sda.Fill(dt);
                    return dt;
                }
            }
        }
    }

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

1 个解决方案

#1


0  

You can add Lat and longitude to your database then display this with your code, you can find a sample here Lat long Sample

您可以将Lat和经度添加到数据库中,然后使用您的代码显示它,您可以在这里找到一个示例Lat long Sample

Lat long lookup by Place Name sample

通过地名样本进行拉长搜索

And here a sample of a reverse lookup service Reverse lookup service code

这里是反向查找服务的示例反向查找服务代码

#1


0  

You can add Lat and longitude to your database then display this with your code, you can find a sample here Lat long Sample

您可以将Lat和经度添加到数据库中,然后使用您的代码显示它,您可以在这里找到一个示例Lat long Sample

Lat long lookup by Place Name sample

通过地名样本进行拉长搜索

And here a sample of a reverse lookup service Reverse lookup service code

这里是反向查找服务的示例反向查找服务代码