Search code examples
flashactionscript-3google-maps

Google Map API in as3, StyledMapType


I having null reference exception when instantiating StyledMapType

var styles:Array = [
new MapTypeStyle(
MapTypeStyleFeatureType.ALL,
MapTypeStyleElementType.ALL,
[
MapTypeStyleRule.visibility( "on" ),
MapTypeStyleRule.hue( 0xff0000 ),
MapTypeStyleRule.saturation( 100 ),
MapTypeStyleRule.lightness( -50 ),
MapTypeStyleRule.gamma( 1.0 )
]
)
];

var options:StyledMapTypeOptions = new StyledMapTypeOptions( {
name: 'Styled map',
alt: 'Style',
minResolution: 2,
maxResolution: 12
});

var styledMapType:StyledMapType = new StyledMapType(styles, options);

I am getting this error:

TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at com.google.maps::StyledMapType()
    at com::Main/init()
    at com::Main()

But when I omit this line:

var styledMapType:StyledMapType = new StyledMapType(styles, options);

the program runs.


Solution

  • You need to register an event listener on MAP_READY and put this code in your listener. The error is probably happening because your google map object is not fully instantiated yet.

    public function somefunction(): void
    {
        yourGoogleMapobject.addEventListener(MapEvent.MAP_READY, onMapReady);
    }
    
    private function onMapReady(event:Event):void
    {
        var styles:Array = [
        new MapTypeStyle(
        MapTypeStyleFeatureType.ALL,
        MapTypeStyleElementType.ALL,
        [
        MapTypeStyleRule.visibility( "on" ),
        MapTypeStyleRule.hue( 0xff0000 ),
        MapTypeStyleRule.saturation( 100 ),
        MapTypeStyleRule.lightness( -50 ),
        MapTypeStyleRule.gamma( 1.0 )
        ]
        )
        ];
    
        var options:StyledMapTypeOptions = new StyledMapTypeOptions( {
        name: 'Styled map',
        alt: 'Style',
        minResolution: 2,
        maxResolution: 12
        });
    
        var styledMapType:StyledMapType = new StyledMapType(styles, options);
    }