0

I am new to both JSON & google maps. SO please tell me what I am missing

   <!DOCTYPE html>
<html>
<head>
<script src="https://maps.googleapis.com/maps/api/js?v=3.15&sensor=false"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

<script>

    $function({
    $("#search").click(function(){
        $.getJSON("http://maps.googleapis.com/maps/api/geocode/json?address=awan%20town&sensor=false",function(result){
            $.each(result,function(i,field){
                $("#map-canvas").append(field + " " );
            });
        });
    });
}); 



</script>
</head>
<body>

<button  id="search">Search</button>
<div id="map-canvas" style="width:100%;height:100%;"></div>
</body>
</html>

1 Answer 1

1

Try to wrap your code inside DOM ready handler $(function() {...}); to make sure all of your DOM elements have been loaded properly before executing your jQuery code:

$(function () {
    $("#search").click(function () {
        $.getJSON("http://maps.googleapis.com/maps/api/geocode/json?address=awan%20town&sensor=false", function (result) {
            $.each(result, function (i, field) {
                $.("#map-canvas").append(field + " ");
            });
        });
    });
});

You also need to change:

$.("#search")
$.("#map-canvas")

to:

$("#search")
$("#map-canvas")

You don't need the . before selector here.

Sign up to request clarification or add additional context in comments.

4 Comments

What fields that you want to get from the returned JSON?
I guess the code I wrote would return all from this URL, I actually want to get any specific field how to do that maps.googleapis.com/maps/api/geocode/…
I'd suggest you to take a basic tutorial about JSON to understand better about how JSON works.
I did on w3school but for now if you just correct my code say I want to access formatted_address how I do that?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.