0

I'm trying to show a custom map using the Google Maps Javascript API. In particular, I'm trying to show it using a couple of jquery scripts to allow rotation of the map. I'm not sure if that part is relevant.

When running the code using standard Google Map data (ie, tiles of Earth), it works fine. So here is my map-creation code:

     // Standard google map
     var map = new google.maps.Map2(document.getElementById('map'));
     map.setCenter(new google.maps.LatLng(37.4419, -122.1419), 13);

This works fine. However, when I try to use a custom map, feeding it my own tiles (which I have already achieved in another project, without map rotation), I get "undefined is not a function". Here is the code that has been added to use custom tiles instead of standard Google Maps data:

google.load("maps", "2.x");

var customTypeOptions = {
        getTileUrl: function (coord, zoom) {
            var normalizedCoord = getNormalizedCoord(coord, zoom);
            if (!normalizedCoord) {
                return null;
            }
            var bound = Math.pow(2, zoom);
            return "http://MYTILESURL/" + zoom + "/" + normalizedCoord.x + "/" + (bound - normalizedCoord.y - 1) + ".jpg";
        },
        tileSize: new google.maps.Size(256, 256),
        maxZoom: 3,
        minZoom: 0,
        radius: 1024,
        name: 'custom'
    };

var customMapType = new google.maps.ImageMapType(customTypeOptions);

and then in my initialize() function:

// Custom map
  var myLatlng = new google.maps.LatLng(0, 0);
  var myOptions = {
    center: myLatlng,
    zoom: 0,
    streetViewControl: false,
    mapTypeControlOptions: {
    mapTypeIds: ['custom']
    }
 };

 var map = new google.maps.Map(document.getElementById('map'), myOptions);
 map.mapTypes.set('custom', customMapType);
 map.setMapTypeId('custom');

The error I'm getting is showing "line 46", which in my code is "tileSize: new google.maps.Size(256, 256),". I'm not sure, however, if the 46 reported here is the same 46 I'm reading, ie, the 46th line of the file.

Does anyone have any ideas why I'm getting this "undefined is not a function" error?

7
  • It looks like you're loading the GMap library asynchronously, in which case google.maps.Size may not exist at the time you're calling it? If so you should use the callback property of the 3rd argument to google.load to make sure the object exists first. Commented Jul 11, 2012 at 15:44
  • I have "google.setOnLoadCallback(initialize);" as the last line in my script, would that achieve the same thing? Commented Jul 11, 2012 at 15:50
  • Yes, but you'll need to move the customTypeOptions = ... code inside that method too. I've added this as a proper answer for you. Commented Jul 11, 2012 at 15:53
  • You should be adding an answer here, not just commenting on my question :) I can't upvote or tick an answer that doesn't exist (if it works)! Commented Jul 11, 2012 at 16:02
  • @breadbin which javascript you used in your example. Please give me that one. i need it.. Commented Oct 23, 2012 at 12:58

1 Answer 1

4

This is happening because google.maps.Size is being accessed before the GMap library has finished loading. Simply move anything that refers to google.maps inside your initialise function (or use the callback option in your google.load call to do the same thing).

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

3 Comments

I'm not getting the error anymore, but my maps still aren't loading :(
Probably best to ask another question if you can't figure out why they won't load.
True, thanks your input anyway, pretty sure that's that issue fixed.

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.