/
ShowLocation2.aspx.cs
68 lines (50 loc) · 2.74 KB
/
ShowLocation2.aspx.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
public partial class ShowLocation2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
var address = Request.QueryString["Address"];
if (string.IsNullOrEmpty(address))
Response.Redirect("default.aspx");
// Get the lat/long info about the address
var results = GoogleMapAPIGetLatLong.GoogleMapAPIGeoCoding(address);
lblAddress.Text = address;
// Set the latitude and longtitude parameters based on the address being searched on
dsSearchResults.SelectParameters["Latitude"].DefaultValue = results.Element("result").Element("geometry").Element("location").Element("lat").Value;
dsSearchResults.SelectParameters["Longitude"].DefaultValue = results.Element("result").Element("geometry").Element("location").Element("lng").Value;
lvSearchResults.DataBind();
// Loop through each nearby location and build up the JavaScript to place the markers
var markers = new List<string>();
var nearbyLocations = dsSearchResults.Select(DataSourceSelectArguments.Empty) as DataView;
foreach (DataRowView location in nearbyLocations)
{
markers.Add(string.Format(
@"{{
title: ""Property #{0}"",
position: new google.maps.LatLng({1}, {2}),
url: ""PropertyDetails.aspx?PropertyID={3}""
}}",
location["PropertyID"],
location["Latitude"],
location["Longitude"],
location["PropertyID"]
)
);
}
var lat = results.Element("result").Element("geometry").Element("location").Element("lat").Value;
var lng = results.Element("result").Element("geometry").Element("location").Element("lng").Value;
var locationsJson = "[" + string.Join(",", markers.ToArray()) + "]";
// Inject the Google Maps script
ClientScript.RegisterStartupScript(this.GetType(), "Google Maps Initialization",
string.Format("init_map('map_canvas', {0}, {1}, 15, {2});", lat, lng, locationsJson), true);
}
}
}