Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Chuck 71 posts 69 karma points
    Oct 19, 2011 @ 18:15
    Chuck
    0

    Getting Google Coordinates for Google Map Datatype

    I'm working on a class that triggers when a specific type of node is published. What I am trying to do is take the address, city, and zip code from the node, and use that information to get google map coordinates to plug into the google map datatype. I'm new to .net so I'm struggling. I came up with something I thougth would work, but it pops the following error when I publish a page.

    No mapping exists from object type System.Net.HttpWebRequest to a known managed provider native type

    So can you guys look at my code and maybe give me some pointers or links to where I might be able to get a bit more help? I've been surfing the web all morning trying to find answers already.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Web;
    using umbraco.BusinessLogic;
    using umbraco.cms.businesslogic;
    using umbraco.cms.businesslogic.web;
    using umbraco.cms.businesslogic.media;
    using umbraco.cms.businesslogic.member;


    namespace DistributorNodes
    {

    public class ApplicationBase : umbraco.BusinessLogic.ApplicationBase
    {
    /// <summary>
    /// Initializes a new instance of the <see cref="ApplicationBase"/> class.
    /// </summary>


    public ApplicationBase()
    {
    //Document.New += new Document.NewEventHandler(Document_New);
    Document.AfterPublish += new Document.PublishEventHandler(Document_New);
    }



    void Document_New(Document sender, object e)
    {
    //Document documentObject = sender;
    //Document doc = new Document(true,((CMSNode)sender).Id);

    if (sender.ContentType.Alias == "Distributor")
    {

    string distLogin = sender.getProperty("email").Value.ToString();
    string distPassword = sender.getProperty("password").Value.ToString();
    string distAddress = sender.getProperty("distributorAddress").Value.ToString();
    string distCity = sender.getProperty("distributorCity").Value.ToString();
    string distState = sender.getProperty("distributorState").Value.ToString();

    // get long and lat


    const string GeoCodeUrlFormat = "http://maps.google.com/maps/geo?q={0}&output=csv&oe=utf8&sensor=false";

    string location = distAddress + "," + distCity + "," + distState;

    string url = String.Format(GeoCodeUrlFormat, HttpUtility.UrlEncode(location));

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);


    sender.getProperty("distributorCoordinates").Value = request;




    }


    }

    }


    }

     

  • Rich Green 2246 posts 4008 karma points
    Oct 19, 2011 @ 18:23
    Rich Green
    0

    Hi,

    Looks like you're trying to set a umbraco property to a HttpWebRequest, so this line will fail

    sender.getProperty("distributorCoordinates").Value= request;

    The google maps datatype holds a value such as 213.213092130,123.1234884,8 (if you look in your data/Umbraco.config file you can see exactly what it holds).

    So you need to be setting  distributorCoordinates to a string value.

    Make sense?

    Rich

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Oct 19, 2011 @ 18:39
    Dirk De Grave
    0

    And how about using the Google maps datatype (see project section) ? No need for events to fire and do stuff?

    Cheers,

    /Dirk

  • Chuck 71 posts 69 karma points
    Oct 19, 2011 @ 18:46
    Chuck
    0

    Dirk, is there a way that the datatype can automatically get the coordinates?

    The issue is I have a contour form that the distributor fills out when they register, and I want to automate the process of finding the coordinates. So instead of having the distributor have to use the map datatype, I want to populate the map datatype using the address, city, and state that the distributor entered into his registration form. So on the publish event I want to get the coordinates and populate the google map datatype on the node. Does that make sense?

  • Rich Green 2246 posts 4008 karma points
    Oct 19, 2011 @ 18:52
    Rich Green
    0

    Hey Chuck,

    I would have a look at the source of how the Google Maps datatype gets the co-ordinates https://bitbucket.org/vertino/google-maps-for-umbraco/src then ammend your original code.

    Rich

  • Chuck 71 posts 69 karma points
    Oct 19, 2011 @ 18:54
    Chuck
    0

    Actually Rich, I already did that, and I couldn't figure it out. I'll give it another go though... thanks.

     

  • Rich Green 2246 posts 4008 karma points
    Oct 19, 2011 @ 19:00
    Rich Green
    0

    Hey,

    Having a look through the code it seems the co-ordinates are worked out in JS via the API.

    How about using trying something like http://gmaps.codeplex.com/

    Rich

  • Chuck 71 posts 69 karma points
    Oct 19, 2011 @ 21:29
    Chuck
    0

    Rich,

    That's awesome! That gmaps project was the solution, just include a couple resources from that package and it makes grabbing map data super easy. Here is the final code I came up with.

     

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Web;

    using umbraco.BusinessLogic;
    using umbraco.cms.businesslogic;
    using umbraco.cms.businesslogic.web;
    using umbraco.cms.businesslogic.media;
    using umbraco.cms.businesslogic.member;



    using Google.Api.Maps.Service.Geocoding;

    namespace DistributorNodes
    {

        public class ApplicationBase : umbraco.BusinessLogic.ApplicationBase
        {
            /// <summary>
            /// Initializes a new instance of the <see cref="ApplicationBase"/> class.
            /// </summary>


            public ApplicationBase()
            {
                //Document.New += new Document.NewEventHandler(Document_New);
                Document.AfterPublish += new Document.PublishEventHandler(Document_New);
            }



            void Document_New(Document sender, object e)
            {
                //Document documentObject = sender;
                //Document doc = new Document(true,((CMSNode)sender).Id);

                if (sender.ContentType.Alias == "Distributor")
                {

                    string distLogin = sender.getProperty("email").Value.ToString();
                    string distPassword = sender.getProperty("password").Value.ToString();
                    string distAddress = sender.getProperty("distributorAddress").Value.ToString();
                    string distCity = sender.getProperty("distributorCity").Value.ToString();
                    string distState = sender.getProperty("distributorState").Value.ToString();

                    // get long and lat


                    string location = distAddress + "," + distCity + "," + distState + ", USA";


                    var request = new GeocodingRequest();
                    request.Address = location;
                    request.Sensor = "false";
                    var response = GeocodingService.GetResponse(request);


                   var retLat = response.Results.Single().Geometry.Location.Latitude;
                   var retLong = response.Results.Single().Geometry.Location.Longitude;

                   string distCoordinates = retLat + "," + retLong + ",14";


                   sender.getProperty("distributorCoordinates").Value = distCoordinates;
                   

                }


            }

        }


    }

Please Sign in or register to post replies

Write your reply to:

Draft