Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
help i want to make a map where we can edit and save markers with jquery but when i finished it,my map wont load. plus when i checked my code on Developer tools i got this message "Uncaught ReferenceError: $ is not defined " .please help thank you

this is my code:
HTML
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="http://www.google.com/jsapi?key=AIzaSyBw7b8lMbX80IG_WviZAJBtI9rI_vxjcDU">
<script type="text/javascript"charset="utf-8">google.load("maps","2.x"); google.load("jquery","1.3.1");</script>

<script type="text/javascript">
   $(document).ready(function() {
    var mapCenter = new google.maps.LatLng(47.6145, -122.3418); //Google map Coordinates
    var map;

    map_initialize(); // initialize google map
    
    //############### Google Map Initialize ##############
    function map_initialize()
    {
        var googleMapOptions =
        {
            center: mapCenter, // map center
            zoom: 17, //zoom level, 0 = earth view to higher value
            panControl: true, //enable pan Control
            zoomControl: true, //enable zoom control
            zoomControlOptions: {
            style: google.maps.ZoomControlStyle.SMALL //zoom control size
        },
            scaleControl: true, // enable scale control
            mapTypeId: google.maps.MapTypeId.ROADMAP // google map type
        };
    
        map = new google.maps.Map(document.getElementById("map-canvas"), googleMapOptions);
        
        //Load Markers from the XML File, Check (map_process.php)
        $.get("map_process.php", function (data) {
            $(data).find("marker").each(function () {
                 //Get user input values for the marker from the form
                  var name      = $(this).attr('name');
                  var address   = ''+ $(this).attr('address') +'';
                  var type      = $(this).attr('type');
                  var point     = new google.maps.LatLng(parseFloat($(this).attr('lat')),parseFloat($(this).attr('lng')));

                  //call create_marker() function for xml loaded maker
                  create_marker(point, name, address, false, false, false, "http://mapicons.nicolasmollet.com/wp-content/uploads/mapicons/shape-default/color-eb2828/shapecolor-color/shadow-1/border-dark/symbolstyle-white/symbolshadowstyle-dark/gradient-no/stop.png");
            });
        });
        
        //drop a new marker on right click
        google.maps.event.addListener(map, 'rightclick', function(event) {
            //Edit form to be displayed with new marker
            var EditForm = ''+
            '<form action="ajax-save.php" method="POST" name="SaveMarker" id="SaveMarker">'+
            '<label for="pName">Place Name :<input type="text" name="pName" class="save-name" placeholder="Enter Title" maxlength="40" /></label>'+
            '<label for="pDesc">Description :<textarea name="pDesc" class="save-desc" placeholder="Enter Address" maxlength="150"></textarea></label>'+
            '<label for="pType">Type : <select name="pType" class="save-type"><option value="restaurant">Rastaurant</option><option value="bar">Bar</option>'+
            '<option value="house">House</option></select></label>'+
            '</form>'+
            '<button name="save-marker" class="save-marker">Save Marker Details</button>';

            //call create_marker() function
            create_marker(event.latLng, 'New Marker', EditForm, true, true, true, "http://mapicons.nicolasmollet.com/wp-content/uploads/mapicons/shape-default/color-eb2828/shapecolor-color/shadow-1/border-dark/symbolstyle-white/symbolshadowstyle-dark/gradient-no/stop.png");
        });
    }
});
</script>
<style>
html, body, #map_canvas {
    width: 100%;
    height: 100%;
    margin: 0;
    padding: 0;
}
#map_canvas {
    position: relative;
}
</style>
<head>

<body>

    <<;h1> class="heading">My Google Map<</h1>>
    <<div> align="center">Right Click to Drop a New Marker<</div>>
    <<div >id="map-canvas" style="height:100%"><</div>>

</body>
</html>
Posted
Updated 25-Mar-14 13:00pm
v3

1 solution

The reason of the issue "$ is not defined" is that your html is not completely valid. The first <script> is not closed properly. It should be <script ............="" />. The opening <head> tag occurs twice i.e. you have <head> two times instead of <head> and </head>. Also the tags inside <body> are not formatted correctly.


Here is the modified HTML:
HTML
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="http://www.google.com/jsapi?key=AIzaSyBw7b8lMbX80IG_WviZAJBtI9rI_vxjcDU"></script>
<script type="text/javascript"charset="utf-8">google.load("maps","2.x"); google.load("jquery","1.3.1");</script>
 
<script type="text/javascript">
    $(document).ready(function () {
        var mapCenter = new google.maps.LatLng(47.6145, -122.3418); //Google map Coordinates
        var map;

        map_initialize(); // initialize google map

        //############### Google Map Initialize ##############
        function map_initialize() {
            var googleMapOptions =
        {
            center: mapCenter, // map center
            zoom: 17, //zoom level, 0 = earth view to higher value
            panControl: true, //enable pan Control
            zoomControl: true, //enable zoom control
            zoomControlOptions: {
                style: google.maps.ZoomControlStyle.SMALL //zoom control size
            },
            scaleControl: true, // enable scale control
            mapTypeId: google.maps.MapTypeId.ROADMAP // google map type
        };

            map = new google.maps.Map(document.getElementById("map-canvas"), googleMapOptions);

            //Load Markers from the XML File, Check (map_process.php)
            $.get("map_process.php", function (data) {
                $(data).find("marker").each(function () {
                    //Get user input values for the marker from the form
                    var name = $(this).attr('name');
                    var address = '' + $(this).attr('address') + '';
                    var type = $(this).attr('type');
                    var point = new google.maps.LatLng(parseFloat($(this).attr('lat')), parseFloat($(this).attr('lng')));

                    //call create_marker() function for xml loaded maker
                    create_marker(point, name, address, false, false, false, "http://mapicons.nicolasmollet.com/wp-content/uploads/mapicons/shape-default/color-eb2828/shapecolor-color/shadow-1/border-dark/symbolstyle-white/symbolshadowstyle-dark/gradient-no/stop.png");
                });
            });

            //drop a new marker on right click
            google.maps.event.addListener(map, 'rightclick', function (event) {
                //Edit form to be displayed with new marker
                var EditForm = '' +
            '<form action="ajax-save.php" method="POST" name="SaveMarker" id="SaveMarker">' +
            '<label for="pName">Place Name :<input type="text" name="pName" class="save-name" placeholder="Enter Title" maxlength="40" /></label>' +
            '<label for="pDesc">Description :<textarea name="pDesc" class="save-desc" placeholder="Enter Address" maxlength="150"></textarea></label>' +
            '<label for="pType">Type : <select name="pType" class="save-type"><option value="restaurant">Rastaurant</option><option value="bar">Bar</option>' +
            '<option value="house">House</option></select></label>' +
            '</form>' +
            '<button name="save-marker" class="save-marker">Save Marker Details</button>';

                //call create_marker() function
                create_marker(event.latLng, 'New Marker', EditForm, true, true, true, "http://mapicons.nicolasmollet.com/wp-content/uploads/mapicons/shape-default/color-eb2828/shapecolor-color/shadow-1/border-dark/symbolstyle-white/symbolshadowstyle-dark/gradient-no/stop.png");
            });
        }
    });
</script>
<style>
html, body, #map_canvas {
    width: 100%;
    height: 100%;
    margin: 0;
    padding: 0;
}
#map_canvas {
    position: relative;
}
</style>
</head>
 
<body>
    <h1 class="heading">My Google Map</h1>
    <div align="center">Right Click to Drop a New Marker</div>
    <div id="map-canvas" style="height:100%"></div>
</body>
</html>
 
Share this answer
 
v2
Comments
Member 10699106 28-Mar-14 6:27am    
HOLY sh*t YOU ARE AWESOME
Member 11122927 6-Oct-14 9:51am    
google.maps.event.addListener(map, 'rightclick', function (event) {
var EditForm = ''
//i want to inter this code but not working please help...............................
<input type="text" id="pcal1" name="pcal1"><br>

<script type="text/javascript"> var objCal1 = new AMIB.persianCalendar( 'pcal1' ); </script>

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900