Hi alex0230,
Concadinate the address, city, state and country and pass to GetLatLong method and return Latitude and Longitude from the complete address.
Then bind the data to repeater to add marker to the map.
Refer below code.
HTML
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=PAI_Key"></script>
<script type="text/javascript">
var markers = [
<asp:Repeater ID="rptMarkers" runat="server">
<ItemTemplate>
{
"title": '<%# Eval("Name") %>',
"lat": '<%# Eval("Latitude") %>',
"lng": '<%# Eval("Longitude") %>',
"description": '<%# Eval("Description") %>'
}
</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);
infoWindow.open(map, marker);
});
})(marker, data);
}
}
</script>
<div id="dvMap" style="width: 500px; height: 500px" >
</div>
Namespaces
C#
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using System.Net;
using Newtonsoft.Json;
VB.Net
Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient
Imports System.Net
Imports Newtonsoft.Json
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = this.GetData("SELECT * FROM Locations");
DataTable dt1 = new DataTable();
dt1.Columns.Add("Name");
dt1.Columns.Add("Latitude");
dt1.Columns.Add("Longitude");
dt1.Columns.Add("Description");
foreach (DataRow dr in dt.Rows)
{
string address = string.Format("{0}+{1}+{2}+{3}", dr["Address"].ToString(), dr["City"].ToString(), dr["State"].ToString(), dr["Zip"].ToString());
string name = dr["Address"].ToString();
double latitude = GetLatLon(address).Latitude;
double longitude = GetLatLon(address).Longitude;
string description = dr["Address"].ToString();
dt1.Rows.Add(name, latitude, longitude, description);
}
rptMarkers.DataSource = dt1;
rptMarkers.DataBind();
}
public LatLon GetLatLon(string address)
{
string url = string.Format("https://maps.googleapis.com/maps/api/geocode/json?address={0}&key=YOUR_API_KEY", address);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
System.IO.MemoryStream ms = new System.IO.MemoryStream();
System.IO.Stream responseStream = response.GetResponseStream();
byte[] buffer = new byte[2049];
int count = responseStream.Read(buffer, 0, buffer.Length);
while (count > 0)
{
ms.Write(buffer, 0, count);
count = responseStream.Read(buffer, 0, buffer.Length);
}
responseStream.Close();
ms.Close();
byte[] responseBytes = ms.ToArray();
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
string coords = encoding.GetString(responseBytes);
Root result = JsonConvert.DeserializeObject<Root>(coords);
return new LatLon
{
Latitude = result.results[0].geometry.location.lat,
Longitude = result.results[0].geometry.location.lng
};
}
return null;
}
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;
}
}
}
}
public class LatLon
{
public double Latitude { get; set; }
public double Longitude { get; set; }
}
public class Root
{
public List<Result> results { get; set; }
public string status { get; set; }
}
public class Result
{
public List<AddressComponent> address_components { get; set; }
public string formatted_address { get; set; }
public Geometry geometry { get; set; }
public string place_id { get; set; }
public List<string> types { get; set; }
}
public class Geometry
{
public Bounds bounds { get; set; }
public Location location { get; set; }
public string location_type { get; set; }
public Viewport viewport { get; set; }
}
public class AddressComponent
{
public string long_name { get; set; }
public string short_name { get; set; }
public List<string> types { get; set; }
}
public class Location
{
public double lat { get; set; }
public double lng { get; set; }
}
public class Northeast
{
public double lat { get; set; }
public double lng { get; set; }
}
public class Southwest
{
public double lat { get; set; }
public double lng { get; set; }
}
public class Bounds
{
public Northeast northeast { get; set; }
public Southwest southwest { get; set; }
}
public class Viewport
{
public Northeast northeast { get; set; }
public Southwest southwest { get; set; }
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim dt As DataTable = Me.GetData("SELECT * FROM Locations")
Dim dt1 As DataTable = New DataTable()
dt1.Columns.Add("Name")
dt1.Columns.Add("Latitude")
dt1.Columns.Add("Longitude")
dt1.Columns.Add("Description")
For Each dr As DataRow In dt.Rows
Dim address As String = String.Format("{0}+{1}+{2}+{3}", dr("Address").ToString(), dr("City").ToString(), dr("State").ToString(), dr("Zip").ToString())
Dim name As String = dr("Address").ToString()
Dim latitude As Double = GetLatLon(address).Latitude
Dim longitude As Double = GetLatLon(address).Longitude
Dim description As String = dr("Address").ToString()
dt1.Rows.Add(name, latitude, longitude, description)
Next
rptMarkers.DataSource = dt1
rptMarkers.DataBind()
End Sub
Public Function GetLatLon(ByVal address As String) As LatLon
Dim url As String = String.Format("https://maps.googleapis.com/maps/api/geocode/json?address={0}&key=YOUR_API_KEY", address)
Dim request As HttpWebRequest = CType(WebRequest.Create(url), HttpWebRequest)
Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)
If response.StatusCode = HttpStatusCode.OK Then
Dim ms As System.IO.MemoryStream = New System.IO.MemoryStream()
Dim responseStream As System.IO.Stream = response.GetResponseStream()
Dim buffer(2048) As Byte
Dim count As Integer = responseStream.Read(buffer, 0, buffer.Length)
While count > 0
ms.Write(buffer, 0, count)
count = responseStream.Read(buffer, 0, buffer.Length)
End While
responseStream.Close()
ms.Close()
Dim responseBytes As Byte() = ms.ToArray()
Dim encoding As System.Text.ASCIIEncoding = New System.Text.ASCIIEncoding()
Dim coords As String = encoding.GetString(responseBytes)
Dim result As Root = JsonConvert.DeserializeObject(Of Root)(coords)
Return New LatLon With {
.Latitude = result.results(0).geometry.location.lat,
.Longitude = result.results(0).geometry.location.lng
}
End If
Return Nothing
End Function
Private Function GetData(ByVal query As String) As DataTable
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim cmd As SqlCommand = New SqlCommand(query)
Using con As SqlConnection = New SqlConnection(conString)
Using sda As SqlDataAdapter = New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Using dt As DataTable = New DataTable()
sda.Fill(dt)
Return dt
End Using
End Using
End Using
End Function
Public Class LatLon
Public Property Latitude As Double
Public Property Longitude As Double
End Class
Public Class Root
Public Property results As List(Of Result)
Public Property status As String
End Class
Public Class Result
Public Property address_components As List(Of AddressComponent)
Public Property formatted_address As String
Public Property geometry As Geometry
Public Property place_id As String
Public Property types As List(Of String)
End Class
Public Class Geometry
Public Property bounds As Bounds
Public Property location As Location
Public Property location_type As String
Public Property viewport As Viewport
End Class
Public Class AddressComponent
Public Property long_name As String
Public Property short_name As String
Public Property types As List(Of String)
End Class
Public Class Location
Public Property lat As Double
Public Property lng As Double
End Class
Public Class Northeast
Public Property lat As Double
Public Property lng As Double
End Class
Public Class Southwest
Public Property lat As Double
Public Property lng As Double
End Class
Public Class Bounds
Public Property northeast As Northeast
Public Property southwest As Southwest
End Class
Public Class Viewport
Public Property northeast As Northeast
Public Property southwest As Southwest
End Class