/* * Always set the map height explicitly to define the size of the div element * that contains the map. */#map{height:100%;}/* * Optional: Makes the sample page fill the window. */html,body{height:100%;margin:0;padding:0;}
Git and Node.js are required to run this sample locally. Follow these instructions to install Node.js and NPM. The following commands clone, install dependencies and start the sample application.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-09-04 UTC."],[[["\u003cp\u003eThis example demonstrates creating a draggable marker on a Google Map using the Maps JavaScript API.\u003c/p\u003e\n"],["\u003cp\u003eThe marker's position updates and is displayed in an info window when the drag action ends.\u003c/p\u003e\n"],["\u003cp\u003eThe sample code utilizes the \u003ccode\u003eAdvancedMarkerElement\u003c/code\u003e and provides implementations in both JavaScript and TypeScript.\u003c/p\u003e\n"],["\u003cp\u003eUsers can interact with a live version of the map or clone the sample code to run it locally.\u003c/p\u003e\n"]]],[],null,["This example map shows a draggable marker that displays its updated position\nwhen dragging is finished (the `dragend` event is fired).\n\nRead the\n[documentation](/maps/documentation/javascript/advanced-markers/accessible-markers#make_a_marker_draggable). \n\nTypeScript \n\n```typescript\nasync function initMap() {\n // Request needed libraries.\n const { Map, InfoWindow } = await google.maps.importLibrary(\"maps\") as google.maps.MapsLibrary;\n const { AdvancedMarkerElement } = await google.maps.importLibrary(\"marker\") as google.maps.MarkerLibrary;\n\n const map = new Map(document.getElementById('map') as HTMLElement, {\n center: {lat: 37.39094933041195, lng: -122.02503913145092},\n zoom: 14,\n mapId: '4504f8b37365c3d0',\n });\n\n const infoWindow = new InfoWindow();\n\n const draggableMarker = new AdvancedMarkerElement({\n map,\n position: {lat: 37.39094933041195, lng: -122.02503913145092},\n gmpDraggable: true,\n title: \"This marker is draggable.\",\n });\n dr\u003eaggableMarker.addListener('dragend', (event) = {\n const position = draggableMarker.position as google.maps.LatLng;\n infoWindow.close();\n infoWindow.setContent(`Pin dropped at: ${position.lat}, ${position.lng}`);\n infoWindow.open(draggab \nhttps://github.com/googlemaps-samples/js-api-samples/blob/5cfc59e5155d6f67fca728c3fa60a490ff98714e/samples/advanced-markers-draggable/index.ts#L8-L38\n.map, draggableMarker);\n });\n\n}\n\ninitMap();index.ts\n```\n| **Note:** Read the [guide](/maps/documentation/javascript/using-typescript) on using TypeScript and Google Maps.\n\nJavaScript \n\n```javascript\nasync function initMap() {\n // Request needed libraries.\n const { Map, InfoWindow } = await google.maps.importLibrary(\"maps\");\n const { AdvancedMarkerElement } = await google.maps.importLibrary(\"marker\");\n const map = new Map(document.getElementById('map'), {\n center: { lat: 37.39094933041195, lng: -122.02503913145092 },\n zoom: 14,\n mapId: '4504f8b37365c3d0',\n });\n const infoWindow = new InfoWindow();\n const draggableMarker = new AdvancedMarkerElement({\n map,\n position: { lat: 37.39094933041195, lng: -122.02503913145092 },\n gmpDraggable: true,\n title: \"This marker is draggable.\",\n });\n dr\u003eaggableMarker.addListener('dragend', (event) = {\n const position = draggableMarker.position;\n infoWindow.close();\n infoWindow.setContent(`Pin dropped at: ${position.lat}, ${position.lng}`);\n infoWindow.open(dragg \nhttps://github.com/googlemaps-samples/js-api-samples/blob/5cfc59e5155d6f67fca728c3fa60a490ff98714e/dist/samples/advanced-markers-draggable/docs/index.js#L8-L33er.map, draggableMarker);\n });\n}\ninitMap();index.js\n```\n| **Note:** The JavaScript is compiled from the TypeScript snippet.\n\nCSS \n\n```css\n/* \n * Always set the map height explicitly to define the size of the div element\n * that contains the map. \n */\n#map {\n height: 100%;\n}\n\n/* \n * Optional: Makes the sample page fill the window. \n */\nhtml,\nbody {\n height: 100%;\n margin: 0;\n padding: 0;\n}\nhttps://github.com/googlemaps-samples/js-api-samples/blob/5cfc59e5155d6f67fca728c3fa60a490ff98714e/dist/samples/advanced-markers-draggable/docs/style.css#L7-L24\n```\n\nHTML \n\n```html\n\u003chtml\u003e\n \u003chead\u003e\n \u003ctitle\u003eDraggable Advanced Marker\u003c/title\u003e\n\n \u003clink rel=\"stylesheet\" type=\"text/css\" \u003ehref=\u003c\"./style.css\" /\n script \u003e\u003ctype=&q\u003euot\u003c;modu\u003ele&\u003cquot\u003e; src\u003c=\"./ind\u003e\u003cex.j\u003es\"\u003c;/script\n /head\n bod\u003ey\n \u003c div i\u003ed=&\u003equot;map\"/div\n\n !-- prettier-ignore --\n script(g={var h,a,k,p=\"The Google Maps JavaScript API\",c=\"google\",l=\"importLibrary\",q=\"__ib__\",m\u003e=document,b=window;b=b[c]||(b\u003e[c]={});var d=b.maps||(b.maps={}),r=new Set,e=new URLSearchParams,u=()=h||(h=new Promise(async(f,n)={awa\u003eit (a=m.createElement(\"script\"));e.set(\"libraries\",[...r]+\"\");for(k in g)e.set(k.replace(/[A-Z]/g,t=\u003e\"_\"+t[0].toLowerCase()),g[k]);e.set(\"callback\",c+\".maps.\"+q);a.src=`https://maps.${c}apis.com/maps/api/js?`+e;d[q]=f;a.onerror=()=h=n(Error(p+\u003e\" c&&ould not loa\u003ed.\"));a.nonce=m.querySelector(\"script[nonce]\")?.nonce||\"\";m.hea\u003cd.appen\u003ed(a\u003c)}));\u003ed\u003c[l]?c\u003ehttps://github.com/googlemaps-samples/js-api-samples/blob/5cfc59e5155d6f67fca728c3fa60a490ff98714e/dist/samples/advanced-markers-draggable/docs/index.html#L8-L22n(p+\" only loads once. Ignoring:\",g):d[l]=(f,...n)=r.add(f)u().then(()=d[l](f,...n))})\n ({key: \"AIzaSyA6myHzS10YXdcazAFalmXvDkrYCp5cLc8\", v: \"weekly\"});/script\n /body\n/htmlindex.html\n```\n\nTry Sample \n[JSFiddle.net](https://jsfiddle.net/gh/get/library/pure/googlemaps-samples/js-api-samples/tree/main/dist/samples/advanced-markers-draggable/jsfiddle)\n\nClone Sample\n\n\nGit and Node.js are required to run this sample locally. Follow these [instructions](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm) to install Node.js and NPM. The following commands clone, install dependencies and start the sample application. \n\n git clone https://github.com/googlemaps-samples/js-api-samples.git\n cd samples/advanced-markers-draggable\n npm i\n npm start"]]