Here's a JSFiddle of how it works. Essentially place your function above the call to Google API
WhenIhavethefunctioninitmap(){}(withbrackets)themapdoesnotloadandIgettheerror.
Whenremovethefunctioninitmap{}andjust:
varmap=newgoogle.maps.Map(document.getElementById('map'),{
center:{lat:34.397,lng:150.644},
scrollwheel:false,
zoom:2
});
Themaploads,howeverIstillgettheerror:
initMapisnotafunction
Itmighthavesomethingtodowithacallback.Ihavenotwrittenjavascriptinawhile.ButIjustneedtogetpastthiserror.Thanks
javascriptjqueryhtmlgoogle-maps
Share
Improvethisquestion
Follow
askedDec9,2016at17:26
EB.EB.
2,56788goldbadges3333silverbadges5353bronzebadges
1
Isthatamistakeincopyingoryoudidn'tcloseinitMapproperly?
– Stefan
Dec9,2016at17:35
Addacomment
|
6Answers
6
Sortedby:
Resettodefault
Highestscore(default)
Trending(recentvotescountmore)
Datemodified(newestfirst)
Datecreated(oldestfirst)
12
Here'saJSFiddleofhowitworks.
EssentiallyplaceyourfunctionabovethecalltoGoogleAPI
Alsothefunctionwasn'tproperlyclosed.
Share
Improvethisanswer
Follow
answeredDec9,2016at17:38
StefanStefan
7001010silverbadges2222bronzebadges
Addacomment
|
11
TL;DR
Inthefiddle,youhaveasyntaxerrorinnotclosingtheiniMap()with}.Besides,thefunctionisdefinedaftertheDOMloads.Tofixthis:
setloadtypetowrapinor
fixsyntaxerror
Here'showtodealwiththese.
Globalscope
PlaceinitMapintheglobalscope.donotwrapthefunctiondefinitionwithinaprivateoranonymousscopelike
(function(){
functioninitMap(){
//
}
})();
DOMonload
Definethefunctionintheifpossible.oratleastbeforeloadingtheAPIin.AndneverdefineinitMapafterthedomcompletelyloadslikewindow.onloadanddocument.addEventListener('ondomready',callback)
Goodpractice
Badpractices:
window.onload
And
Or$(function(){})ifyouareusingjQuery
Share
Improvethisanswer
Follow
answeredDec9,2016at17:40
AdamAzadAdamAzad
11k55goldbadges2727silverbadges6868bronzebadges
Addacomment
|
2
Ihavebeenlookingforananswerinmanyforums,butIjustfoundinmycodetherewasalost';'.Afterdeletingthe';'everythingworkedwell.
Iknowit'snotthebestanswerbutIhopeithelpssomeone.
Share
Improvethisanswer
Follow
answeredJun19,2018at3:37
CarlosZCarlosZ
87911goldbadge88silverbadges1515bronzebadges
Addacomment
|
2
YouarecallinginitMapbeforeitwasdeclared.
https://jsfiddle.net/mp7j7gy0/60/
varinitMap=function(){
varmap=newgoogle.maps.Map(document.getElementById('map'),{
center:{lat:34.397,lng:150.644},
scrollwheel:false,
zoom:2
});
}
initMap()
Share
Improvethisanswer
Follow
editedDec8,2018at4:26
AtulBaldaniya
57755silverbadges1414bronzebadges
answeredDec9,2016at17:40
BelowtheRadarBelowtheRadar
7,0471111goldbadges5959silverbadges135135bronzebadges
Addacomment
|
1
Istumbleduponthisproblemtoo,butinmycasetheculpritseemstobemod_pagespeed.
Ithinkitmodifiestheorderofscriptsinthepage,thusprevioussolutionsdidnotwork.ImanagedtosolveitbydynamicallyaddingtheMap'sscripttag.
Share
Improvethisanswer
Follow
editedMay3,2018at0:27
StephenRauch♦
45.7k3030goldbadges105105silverbadges126126bronzebadges
answeredMay2,2018at23:57
WorganWorgan
1111bronzebadge
Addacomment
|
0
Iwasusingleaflet.jsandGooglegavemethisasmydefaultscript
BasicallyjustremovetheCallbackQueryStringParameterbecauseleafletissupposedtocallit.
Share
Improvethisanswer
Follow
answeredSep20,2021at16:55
EnkodeEnkode
4,25533goldbadges3333silverbadges4848bronzebadges
Addacomment
|
YourAnswer
ThanksforcontributingananswertoStackOverflow!Pleasebesuretoanswerthequestion.Providedetailsandshareyourresearch!Butavoid…Askingforhelp,clarification,orrespondingtootheranswers.Makingstatementsbasedonopinion;backthemupwithreferencesorpersonalexperience.Tolearnmore,seeourtipsonwritinggreatanswers.
Draftsaved
Draftdiscarded
Signuporlogin
SignupusingGoogle
SignupusingFacebook
SignupusingEmailandPassword
Submit
Postasaguest
Name
Email
Required,butnevershown
PostYourAnswer
Discard
Byclicking“PostYourAnswer”,youagreetoourtermsofservice,privacypolicyandcookiepolicy
Nottheansweryou'relookingfor?Browseotherquestionstaggedjavascriptjqueryhtmlgoogle-mapsoraskyourownquestion.
TheOverflowBlog
Fiveninesuptimewithoutdeveloperburnout(Ep.488)
WehateScrumandAgile…whenit’sdonewrong(Ep.489)
FeaturedonMeta
RecentColorContrastChangesandAccessibilityUpdates
Revieweroverboard!Orarequesttoimprovetheonboardingguidancefornew...
ShouldIexplainotherpeople'scode-onlyanswers?
Linked
-1
errorInvalidValueError
1
Webcamvideostreamnotshowinginhtml5videotag
Related
5434
HowcanIvalidateanemailaddressinJavaScript?
7623
HowdoJavaScriptclosureswork?
4347
Which"href"valueshouldIuseforJavaScriptlinks,"#"or"javascript:void(0)"?
7138
HowdoIremoveapropertyfromaJavaScriptobject?
7477
varfunctionName=function(){}vsfunctionfunctionName(){}
3934
Whatisthe!!(notnot)operatorinJavaScript?
6065
HowdoIincludeaJavaScriptfileinanotherJavaScriptfile?
8264
Whatdoes"usestrict"doinJavaScript,andwhatisthereasoningbehindit?
7413
HowtocheckwhetherastringcontainsasubstringinJavaScript?
5452
For-eachoveranarrayinJavaScript
HotNetworkQuestions
Useandfunctionofslashchord
WhateffectsnotmentionedinitsspelldescriptiondoesGreaterRestorationcure?
SIngle-directionallinkedlistinC
Doesstandingupfrombeingproneindifficultterrainconsumeallofyourmovement?
Whyissendingtroopsdifferentfromsendingmilitaryequipment?
Howdoesthislibgcryptelgamaldecryptionwork?
WhichrunwayshouldIuse,andhowcanIrecogniseitinfurtherflights?
Smallvs.capitallettersformatrixentries
Asquarenumberthatisaconcatenationofatleast25squares
GrayscaleusingLED
Advantagesofalawlicenseotherthantheabilitytopracticelaw
What'sawordforfakepoliteness?
Whydometalutensilsscrapeteflonpansmorethanwooden?
Frenchequivalentoftheidiom'flogadeadhorse'
Possibleexplanationsforabnormalday/nightcycle?
Howdoyoudetermineifthereisasignificantrelationshipbetweentwovariableswithseveralfactorsaffectingit,usingR?
mainwatersupply'sgroundingwires
Centerofamonoidring
Whatdoyoucallitwhenyouaresayingsomethingpalpablyuntrueonpurpose?
Whyusingapartialderivativeforthelossfunction?
Whydoweneedthe“ProgramFiles”folderinWindows?
Mastermind:Winintwo
Canasingletympanaleardetectthedirectionordistancefromwhichasoundcomes?
Whatexactlydidthe"UNFORMAT"MS-DOScommanddo?
morehotquestions
Questionfeed
SubscribetoRSS
Questionfeed
TosubscribetothisRSSfeed,copyandpastethisURLintoyourRSSreader.
default
Yourprivacy
Byclicking“Acceptallcookies”,youagreeStackExchangecanstorecookiesonyourdeviceanddiscloseinformationinaccordancewithourCookiePolicy.
Acceptallcookies
Customizesettings