Google Maps Geocoding a string - Stack Overflow

文章推薦指數: 80 %
投票人數:10人

I want to be able to geocode a string within my code and place a marker at the location, rather than a user search a location. My code so far is ... Resultsfromthe2022DeveloperSurveyarehere. Home Public Questions Tags Users Companies Collectives ExploreCollectives Teams StackOverflowforTeams –Startcollaboratingandsharingorganizationalknowledge. CreateafreeTeam WhyTeams? Teams CreatefreeTeam Collectives™onStackOverflow Findcentralized,trustedcontentandcollaboratearoundthetechnologiesyouusemost. Learnmore Teams Q&Aforwork Connectandshareknowledgewithinasinglelocationthatisstructuredandeasytosearch. Learnmore GoogleMapsGeocodingastring AskQuestion Asked 8years,4monthsago Modified 2years,1monthago Viewed 63ktimes 8 4 Ihavedonethegeocodingexampleat: https://developers.google.com/maps/documentation/javascript/examples/geocoding-simple Iwanttobeabletogeocodeastringwithinmycodeandplaceamarkeratthelocation,ratherthanausersearchalocation. Mycodesofaris:

Geocodingservice javascriptgoogle-mapsgoogle-maps-api-3geocoding Share Follow editedOct11,2019at15:07 geocodezip 154k1313goldbadges208208silverbadges238238bronzebadges askedJan28,2014at16:32 user3232726user3232726 19111goldbadge55silverbadges1212bronzebadges Addacomment  |  2Answers 2 Sortedby: Resettodefault Highestscore(default) Trending(recentvotescountmore) Datemodified(newestfirst) Datecreated(oldestfirst) 14 ThiswillchangethecodeAddressfunctiontotakeanargumentandgeocodethatargument.IfyoupasstheIrelandvariableastheargumentitwillgeocode"Dublin". varIreland="Dublin"; functioninitialize() { geocoder=newgoogle.maps.Geocoder(); varlatlng=newgoogle.maps.LatLng(53.3496,-6.3263); varmapOptions= { zoom:8, center:latlng } map=newgoogle.maps.Map(document.getElementById('map-canvas'),mapOptions); codeAddress(Ireland);//callthefunction } functioncodeAddress(address) { geocoder.geocode({address:address},function(results,status) { if(status==google.maps.GeocoderStatus.OK) { map.setCenter(results[0].geometry.location);//centerthemapovertheresult //placeamarkeratthelocation varmarker=newgoogle.maps.Marker( { map:map, position:results[0].geometry.location }); }else{ alert('Geocodewasnotsuccessfulforthefollowingreason:'+status); } }); } codesnippet: varIreland="Dublin"; functioninitialize(){ geocoder=newgoogle.maps.Geocoder(); varlatlng=newgoogle.maps.LatLng(53.3496,-6.3263); varmapOptions={ zoom:8, center:latlng } map=newgoogle.maps.Map(document.getElementById('map-canvas'),mapOptions); codeAddress(Ireland);//callthefunction } functioncodeAddress(address){ geocoder.geocode({ address:address },function(results,status){ if(status==google.maps.GeocoderStatus.OK){ map.setCenter(results[0].geometry.location);//centerthemapovertheresult //placeamarkeratthelocation varmarker=newgoogle.maps.Marker({ map:map, position:results[0].geometry.location }); }else{ alert('Geocodewasnotsuccessfulforthefollowingreason:'+status); } }); } html, body, #map-canvas{ height:100%; width:100%; padding:0; margin:0; } Share Follow editedMay17,2020at11:18 answeredJan28,2014at16:43 geocodezipgeocodezip 154k1313goldbadges208208silverbadges238238bronzebadges 1 No,itdoesoneaddressatatimeandissubjecttoaquotaandratelimit. – geocodezip Feb24at13:10 Addacomment  |  2 JustTrywiththis. GoogleMapAddress vargeocoder=newgoogle.maps.Geocoder(); varaddress="Dublin"; geocoder.geocode({'address':address},function(results,status){ if(status==google.maps.GeocoderStatus.OK){ varlatitude=results[0].geometry.location.lat(); varlongitude=results[0].geometry.location.lng(); initialize(latitude,longitude); } }); functioninitialize(latitude,longitude){ varlatlng=newgoogle.maps.LatLng(latitude,longitude); varmyOptions={ zoom:14, center:latlng, mapTypeId:google.maps.MapTypeId.ROADMAP, mapTypeControl:false }; varmap=newgoogle.maps.Map(document.getElementById("map_canvas"),myOptions); varmarker=newgoogle.maps.Marker({ position:latlng, map:map, title:"location:Dublin" }); }

GoogleMapAddress

Share Follow answeredJan28,2014at16:42 JensonMJohnJensonMJohn 5,20744goldbadges2626silverbadges4646bronzebadges 1 Itthrowsanerrorinangularthatinitializeisnotafunctions.anyideawhatwecandoforthat? – KhilenManiyar Feb27,2020at5:43 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?Browseotherquestionstaggedjavascriptgoogle-mapsgoogle-maps-api-3geocodingoraskyourownquestion. TheOverflowBlog ExpertsfromStripeandWaymoexplainhowtocraftgreatdocumentation(Ep.455) Askedandanswered:theresultsforthe2022Developersurveyarehere! FeaturedonMeta AnnouncingthearrivalofValuedAssociate#1214:Dalmarus Testingnewtrafficmanagementtool AskWizardTestResultsandNextSteps Trending:Anewanswersortingoption Linked -1 howdoIgetlocation'slatitudeandlongtitudebyaddressintext -1 Setvariablethatisoutoffunctionscope,fromwithinfunction -1 Googlemapsautoloadhiddenplaceinmarkerlocation Related 3614 HowcanIcheckforanempty/undefined/nullstringinJavaScript? 3200 HowcanIconvertastringtobooleaninJavaScript? 2695 HowcanIgetquerystringvaluesinJavaScript? 4676 HowdoImakethefirstletterofastringuppercaseinJavaScript? 3693 Sortarrayofobjectsbystringpropertyvalue 5190 HowtoreplacealloccurrencesofastringinJavaScript 2475 Generaterandomstring/charactersinJavaScript 7415 HowtocheckwhetherastringcontainsasubstringinJavaScript? 4348 WhydoesGoogleprependwhile(1);totheirJSONresponses? 706 GoogleMapsJSAPIv3-SimpleMultipleMarkerExample HotNetworkQuestions Whycan'tanMCU'sGPIOpinsdirectlycontrolpowerMOSFETs? Howdoes/procinteractwithPIDnamespaces? Explainingdifferencebetweenimproperintegralsthatconvergeanddiverge BookaboutanaliencivilizationthatsendskidstoberaisedonEarth Whathappenswhenawillconflictswithanewlaw(alawmadeafterthetestatorhasdied)? Howtallarethemushrooms? Maintainingunitformationwhilefollowingpath Howtochoosetypeface(fonts)forawebsiteindifferentlocales? Installinga48"vanitybetweentwowallsthatare51"apart.HowcanIcoverthe1.5"gapsonthecabinetmarbletopandthecabinetsides? Whatdoyoucallthepointypartsofatextbubblethat'snotthetail? Howtoplottwofunctions? Howdoyoudiplomaticallycallasituation'verybad'inaresume-likedocument? WouldpubliclyfartingonapictureoftheEnglishmonarchactuallyqualifyastreasonin1798? Arechemicalsusedtoripenfruitindevelopingcountriesharmfultothehealthofconsumers? Animatingasolarsystem:'childof'constraintresetsafter180°rotationwheninfluenceislowerthan1 Aremidi-chloriansstillcanon,despitenotbeingmentionedsinceEpisode1? Assemblermadeinassembly GirlfriendtookoffwiththecarIgotaloanfor HowdoIpolitelycoexistwithahelpvampire? Ihavetwopassportsfromthesamecountrywithadifferentnamespelling.Issueswhentraveling? TryingtoidentifysomethingIsawinthesky BestWaytoStyleRidiculouslyLongHair(formagicpurposes) WhataretheregularlyspacedspotsonthispieceofmaterialonthesurfaceofMars? Isthereawaytoputthisbentlegstraigt? morehotquestions Questionfeed SubscribetoRSS Questionfeed TosubscribetothisRSSfeed,copyandpastethisURLintoyourRSSreader. lang-js Yourprivacy Byclicking“Acceptallcookies”,youagreeStackExchangecanstorecookiesonyourdeviceanddiscloseinformationinaccordancewithourCookiePolicy. Acceptallcookies Customizesettings  



請為這篇文章評分?