2

I want to show the distances between two places on page using Google Maps.

So I have found out how to write the URL, but I don't know how to decode the response and show on my website. Here's what I have so far:

string distancematrix = "http://maps.googleapis.com/maps/api/distancematrix/json?        origins=34746+Florida&destinations=Universal+Studios+Florida|Orlando+Internation+Airport&mode=driving&units=imperial&sensor=false";
Json.Write(distancematrix, Response.ToString);

Can I put each of the results into the page using an @ command?

halfer
  • 19,824
  • 17
  • 99
  • 186
Gavin5511
  • 791
  • 5
  • 31
  • Yes, you can use "@" razor syntax for that. – VoidKing Mar 11 '13 at 16:57
  • Also, if I am understanding your question correctly, you should be able to just use "Json.Decode()" to decode json. Is that what you're asking? – VoidKing Mar 11 '13 at 16:58
  • Here's the code of written so far, but it's not working. I just want to be able to pull a result out. – Gavin5511 Mar 11 '13 at 21:17
  • 'code' string distancematrix = "http://maps.googleapis.com/maps/api/distancematrix/json?origins=34746+Florida&destinations=Universal+Studios+Florida&mode=driving&units=imperial&sensor=false"; Json.Write(distancematrix, Response.Output); Json.Decode(distan); – Gavin5511 Mar 11 '13 at 21:18
  • What error are you getting? Also, first and foremost, you should check to make sure your JSON syntax is correct. You can do that very quickly and easily here: http://jsonlint.com/ – VoidKing Mar 11 '13 at 21:27
  • Your JSON is valid, however, I'm not exactly sure what you're having trouble with, so I will post a simple example of how I grab JSON values from an external .json file using WebMatrix and write it to the page with razor. It's really easy. – VoidKing Mar 11 '13 at 21:29

1 Answers1

0

This is a (simplified) example cshtml page that gets data from an external file instead of an external site, but it may help you, nonetheless:

@{
    var json = File.ReadAllText(Server.MapPath("~/TestJSON.json"));
    var data = Json.Decode(json);
}

<html>
    <head>
    </head>
    <body>
        <p>
            @data.destination_addresses <br/>
            @data.origin_addresses <br/>
            @data.rows <br/>
            @data.status
        </p>
    </body>
</html>

I am not sure (cause I'm somewhat noobish when it comes to JSON syntax written in ways I'm not used to seeing), but it seems like your use of brackets (i.e., "[" and "]") suggest the use of arrays?

If you are accessing arrays this way, it might look something like this:

@{
    var json = File.ReadAllText(Server.MapPath("~/TestJSON.json"));
    var data = Json.Decode(json);
    var i = 0;
}

<html>
    <head>
    </head>
    <body>
        <p>
            @{
                while(i < data.destination_addresses.Length)
                {
                    data.destination_addresses[i]; <br/>
                    i++;
                }
            }
        </p>
    </body>
</html>

Just let me know if this doesn't help and we can go from there.

VoidKing
  • 6,282
  • 9
  • 49
  • 81
  • @Gavin5511 Your path to the JSON data might look more like this: `var json = File.ReadAllText("http://maps.googleapis.com/maps/api/distancematrix/json? origins=34746+Florida&destinations=Universal+Studios+Florida|Orlando+Internation+Airport&mode=driving&units=imperial&sensor=false")` But I am not sure, by any means, as I have never tried to path to another url for JSON data. Also the method does say "File.ReadAllText" and it may not be expecting a complete external url like that at all, I'm not sure... – VoidKing Mar 11 '13 at 21:46