Link to home
Start Free TrialLog in
Avatar of ChrisMDrew
ChrisMDrew

asked on

Google Maps API v3 Adding Markers Server Side

I have been asked to look at creating a simple map application using Google Maps API V3.  The idea is to add a set an initial location and scale and then add a load of markers for bus stops around the area.

Trouble is all of the examples I have seen use JavaScript to create the map and add the markers - the information as to the address I want to display and where the markers are to be placed is all obtained server side.  Can I add markers to a map using C# server side and can I set the location to display and scale of the map server side also?
ASKER CERTIFIED SOLUTION
Avatar of Tom Beck
Tom Beck
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ChrisMDrew
ChrisMDrew

ASKER

Thanks - I had thought I would need RegisterClientScriptBlock but was rather hoping I wouldn't!
Hi tommyBoy

I thought I had this cracked but I am having a few problems getting it to work (it did once but doesn't now!).  I have a button click handler with the following code in it.


StringBuilder builder = new StringBuilder();
builder.Append("<script type='text/javascript'>");
builder.Append("Alert('hello');");
builder.Append("var sites = [[");

// First the 'roadworks' location marker
builder.Append("'Selected Location'," + latitude.ToString() + "," + longitude.ToString() + "'Location to dig']");

// Complete the markers declaration
builder.Append("];");

// Add the call to initialize the map
builder.Append("InitializeMap(" + latitude.ToString() + "," + longitude.ToString() + "');");
builder.Append("</script>");

// ...and register the script block
const string alertScript = "init1";
if (!ClientScript.IsStartupScriptRegistered(this.GetType(), alertScript))
{
      ClientScript.RegisterStartupScript(Page.GetType(), alertScript, builder.ToString(), false);
}

When I view the page source I see my added JavaScript but it does not seem to be being executed - the alert doesn't even appear.   Not sure what I am doing wrong as I have used RegisterStartupScript loads of times before with no issues.  The InitializeMap function  as it says is a JavaScript function which initializes the google map given a latitude and longitude and reads the marfkers array
RegisterStartupScript is not the problem. Your javascript syntax needs to be corrected. You have single quotes where they don't belong and a missing comma in your array. You have not posted the script that is already on the page, so I assume that part is okay.
StringBuilder builder = new StringBuilder();
            builder.Append("<script type='text/javascript'>");          
            builder.Append("alert('hello');");
            builder.Append("var sites = [");

            // First the 'roadworks' location marker
            builder.Append("['Selected Location'," + latitude.ToString() + "," + longitude.ToString() + ",'Location to dig']");

            // Complete the markers declaration
            builder.Append("];");

            // Add the call to initialize the map
            builder.Append("InitializeMap(" + latitude.ToString() + "," + longitude.ToString() + ");");
            builder.Append("</script>");

            // ...and register the script block
            const string alertScript = "init1";
            if (!ClientScript.IsStartupScriptRegistered(this.GetType(), alertScript))
            {
                ClientScript.RegisterStartupScript(Page.GetType(), alertScript, builder.ToString(), false);
            }

Open in new window