Retrieving latitude and longitude data from the database and assigning it to a JavaScript function

My current issue involves integrating Google Maps on my website. I have a RadComboBox with locations populated from the database and an ASP panel displaying the Google Map on the right-hand side. I want users to select a location from the RadComboBox, which will then be reflected on the map by obtaining the latitude and longitude.

To achieve this, my plan is to save all locations into an ArrayList, serialize it, save it to a HiddenField, and then use it in JavaScript by deserializing it. However, I am running into issues as nothing seems to be happening as expected.

Below is my VB.NET backend code:

Imports System.Web.Script.Serialization.JavaScriptSerializer

Public Class LocationInfo
    Private m_LocationID As Integer = 0
    Private m_LocationName As String = Nothing
    Private m_LocationLat As String = Nothing
    Private m_LocationLng As String = Nothing

#Region "LocationInfo Properties"
    Public Property LocationID() As Integer
        Get
            Return m_LocationID
        End Get
        Set(ByVal value As Integer)
            m_LocationID = value
        End Set
    End Property

    Public Property LocationName() As String
        Get
            Return m_LocationName
        End Get
        Set(ByVal value As String)
            m_LocationName = value
        End Set
    End Property

    Public Property LocationLat() As String
        Get
            Return m_LocationLat
        End Get
        Set(ByVal value As String)
            m_LocationLat = value
        End Set
    End Property

    Public Property LocationLng() As String
        Get
            Return m_LocationLng
        End Get
        Set(ByVal value As String)
            m_LocationLng = value
        End Set
    End Property
#End Region

End Class


 Public Sub GetLocationInfo()
        Dim LocationList As New List(Of LocationInfo)
        Dim dba As New DBAccess
        Dim ds As DataSet = dba.GetUserLocationsByID(m_User.UserID)
        Dim dt As DataTable = ds.Tables(0)
        For Each dr As DataRow In dt.Rows()
            Dim locationInfo As New LocationInfo
            locationInfo.LocationName = dr("LocationName")
            locationInfo.LocationLat = dr("Lat")
            locationInfo.LocationLng = dr("lng")
            locationInfo.LocationID = dr("LocationID")
            LocationList.Add(locationInfo)
        Next
        Dim oSerilzer As New System.Web.Script.Serialization.JavaScriptSerializer
        Dim sJson As String = oSerilzer.Serialize(LocationList)
        hfLocationList.Value = sJson.ToString()


    End Sub


*****************************aspx code*****************************


     function getValueFromList() {
            var jsonString = document.getElementById('hfLocationList').value;
            var arr_from_json = JSON.parse(jsonString);

        }

        var map;
        function initialize() {
            var mapOptions = {
                zoom: 8,
                center: new google.maps.LatLng(34.052055, -118.460490)
            };
            map = new google.maps.Map(document.getElementById('map-canvas'),
      mapOptions);
        }
        google.maps.event.addDomListener(window, 'load', initialize);





                                <td>Locations:</td>
                                <td>
                                    <asp:HiddenField runat="server" ID="hfLocationList" Value="0"/>
                                    <telerik:RadComboBox ID="rcbLocations" runat="server">
                                    </telerik:RadComboBox>

<div id="map-canvas">

          <asp:Panel ID="Panel1" runat="server" Width="150px" Height="150px">

          </asp:Panel>

Answer №1

Try this method instead:

document.getElementById('hfLocationList').value

replace it with the following:

document.getElementById('<%= hfLocationList.ClientID %>').value

Hopefully, this solution works for you. Best wishes!

Answer №2

Another option is to specify the ClientIdMode property of the asp.net control as Static

<asp:HiddenField runat="server" ID="hfLocationList" Value="0" ClientIdMode="Static" />

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Show either the abbreviated or complete form of the text

Initially, the default display should show the shortened version of each element (one line with "..." included). All items must consistently be shown in either the shortened or full-length version. If all items are in shortened mode - clicking on one item ...

Try making a series of interconnected fetch requests in Redux Toolkit that rely on the completion of the previous fetch

I am still learning the ropes of Redux and I'm feeling a bit lost. My goal is to make two API calls - one to retrieve an account Id and a category Id, and another to get a list of transactions based on those IDs. The createApi function in my code lo ...

Error message related to the callback type issue in Node.js with the http.createServer

Having recently embarked on my journey with node.js, I delved into various tutorials and encountered a stumbling block while attempting to refactor some code. The tutorial that caught my attention and led me to this hiccup can be found here: http://www.tu ...

Alter the data displayed by the Radio button using Angular when the Submit button is clicked

I've encountered an issue where I need to alter a div based on the selection of a radio button. Currently, it changes instantly upon button click, rather than waiting for submission. My desired outcome is for the value to be submitted when the button ...

What steps should be taken to resolve the error message "TypeError: Cannot read properties of undefined (reading 'toLowerCase')?"

After creating a basic search bar, I encountered an issue when typing in it for the first time: TypeError: Cannot read properties of undefined (reading 'toLowerCase') However, when I closed the pop-up and tried again, the search bar worked prope ...

The Kendo UI Grid's cancel function fails to revert back to the original data

I am facing an issue with a kendo grid that is embedded inside a kendo window template. This grid gets its data from another grid on the main UI, following a model hierarchy of Fund -> Currency -> Allocations. The main UI grid displays the entire dat ...

Guide on creating a JSONP request

My goal is to perform cross-site scripting. The code snippet below shows the jsonp method, which appears to fail initially but succeeds when switched to a get request. I am trying to achieve a successful response using the jsonp method. I have confirmed th ...

How to extract the date value from an HTML date tag without using a datepicker

Utilizing Intel's app framework means there is no .datepicker method available. The following code snippet generates the date widget within my app: <label for="startdate">Start Date:</label> <input type="date" name="startdate" id="star ...

Can a JavaScript class have a property that returns an array?

To those more experienced in node red development, this may be obvious, but I'll ask anyway. Within my node red flow, I have a function node containing a javascript class that only exposes static members. Here's an example: class MeasurementsLis ...

utilizing spring mvc conditions to dynamically populate parameters in a javascript URL function

In my Spring MVC application, I have implemented a JavaScript calendar control that redirects the user to a detail page for a selected date. However, I am facing an issue where I need to include additional parameters in the URL along with the date. How can ...

Is there a way to verify the identity of two fields using an external script such as "signup.js"?

My current project involves working with Electron, and it consists of three essential files. The first file is index.html: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <link rel="styles ...

Utilizing JavaScript within a WordPress loop

I'm facing an issue with running JavaScript within my WordPress loop where it doesn't seem to recognize the PHP variables. My goal is to create a functionality where clicking on one box reveals hidden content, and clicking on the same box or anot ...

Obtain the index by clicking on an element within an HTMLCollection

Within my HTML code, I have 9 <div> elements with the class ".square". I am looking to make these divs clickable in order to track how many times each one is clicked and store that information in an array. For example, if the fifth <div> is c ...

Spinning Loader During Ajax Request

I am currently working on an ajax script that retrieves a query file and loads new content from that file whenever the database is updated. However, I want to enhance the script by adding a loading spinner. How can I achieve this? < div id = "AjaxLoa ...

Convert items to an array utilizing lodash

I need assistance converting an object into an array format. Here is the input object: { "index": { "0": 40, "1": 242 }, "TID": { "0": "11", "1": "22" }, "DepartureCity": { "0": "MCI", "1": "CVG" }, "ArrivalCity": { ...

Creating a regular expression variable in Mongoose: A step-by-step guide

I am looking for a solution to incorporate a variable pattern in mongoose: router.get('/search/:name', async(req, res) => { name = req.params.name; const products = await Product.find({ name: /.*name*/i }).limit(10); res.send(prod ...

Rotation feature is not functioning on Three.js Trackball zoom, however zoom is working

As the title suggests, I am having trouble with trackerballcontrols. I am expecting it to work similar to this example, where I can rotate the camera but for some reason, I can only zoom in and out. Of course, I would like to be able to rotate the camera. ...

Issue with Flowtype not properly refreshing when maximizing or minimizing

There is a strange behavior with my Flowtype plugin. When I load the page without maximizing the screen, the font size remains the same even after maximizing the screen. However, if I then restore the window to its original size, the font size adjusts as ...

What is the best method for performing cross-domain queries utilizing ajax and jsonp?

When attempting to send an ajax request to a specific URL, I encountered an error. Below is the code I used: $.ajax({ url: "http://webrates.truefx.com/rates/connect.html?q=ozrates&c=EUR/USD&f=csv&s=n", dataType : 'jsonp', ...

React.js: The function useDef has not been defined

Attempting to create a React.js calculator application, my initial step was to delete the entire src folder and replace it with a new one containing the necessary elements for the web app. Here is the content of the index.js file: import React,{ useEffect, ...