Jump to content
sadboys

Google Custom Markers does not appear

Recommended Posts

Hello, I'm currently learning Google Maps API, I created a page that shows the Google Maps with custom markers. My problem is that the Google Maps works fine but the custom markers that I set does not work.

 

 

Here's what I did.

script async defer
        src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBSTRDm6eRdkpoVOB2VJVJgTCNgmcuDcq0&callback=initMap">
        </script>
        <script>
            function initmap() {
                map = new google.maps.Map(document.getElementById('map'), {
                    zoom: 16,
                    center: new google.maps.LatLng(14.529133, 121.021747),
                    mapTypeId: 'roadmap'
                });

                var iconBase = 'https://maps.google.com/mapfiles/kml/shapes/';
                var testIcon = 'http://maps.google.com/mapfiles/kml/paddle/';

                var icons = {

                    evac: {
                        icon: iconBase + 'ranger_station.png'
                    },

                    warehouse: {
                        icon: iconBase + 'truck.png'
                    }
                };

                var features = [
                    {
                        position: new google.maps.LatLng(14.529133, 121.021747),
                        type: 'evac'
                    }
                ];

                features.forEach(function(feature) {
                    var marker = new google.maps.Marker({
                        position: feature.position,
                        icon: icons[feature.type].icon,
                        map: map
                    });
                });
            }
        </script>
        </head>

        <body onload="initmap()">
        </body>
        <div id="map_canvas" style="width: 1100px; height: 1000px"></div>

I'm using the KML map files right now but it does not work, where did I get wrong here?

Share this post


Link to post
Share on other sites

Off the top of my head (I'm not really a human JS engine, so check your error console ;)  ) ...

1.  No opening bracket on first line "script" tag ... ;)  (being pedantic here :D )
2.  Name "map" is undeclared?

To debug this, I'd put some console logging into the lambda/anonymous function inside the foreach, to make sure the data values are what they're supposed to be ... especially "feature.position".  Hope this helps ...

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.