123

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Routing in North America | ThinkGeo</title>
</head>

<body>

    <!-- This <div> is the container into which our map control will be loaded. -->
    <div id="map">
        <!-- Set up the coordinates inupting group. -->
        <div class="point">
            <i class="switch"></i>
            <ul id="dragable-list">
                <li><i class="drag"></i>
                    <label></label>
                    <input data-origin="" placeholder="Start" />
                    <span class="hide"></span>
                    <a class="closer start-closer hide"></a>
                </li>
                <li><i class="drag"></i>
                    <label></label>
                    <input data-origin="" placeholder="Destination" />
                    <span class="hide"></span>
                    <a class="closer end-closer hide"></a>
                </li>
            </ul>

            <div id="add-point"><i>+</i> Add destination</div>
            <button id="go">Go</button>

            <!-- Set up the loading animation. -->
            <div class="loading hide">
                <div></div>
                <div></div>
                <div></div>
            </div>
        </div>

        <!-- Set up the result container. -->
        <div class="sidebar empty">
            <div id="result" class="transition-height">
                <p id="total"></p>
                <div id="boxes"></div>
            </div>
        </div>
    </div>

    <!-- Set up context menu when right click the map. -->
    <div id="ol-contextmenu" class="hide">
        <ul>
            <li id="add-startpoint">
                Directions from here
            </li>
            <li id="add-endpoint">
                Directions to here
            </li>
            <li id="context-add-point">
                Add to route
            </li>
            <li id="clear">
                Clear
            </li>
        </ul>
    </div>

    <!-- Set up error message tip for tile loading error. -->
    <div id="error-modal" class="hide">
        <div class="modal-content">
            <p>We're having trouble communicating with the ThinkGeo Cloud. Please check the API key being used in this sample's JavaScript source code, and ensure it has access to the ThinkGeo Cloud services you are requesting. You can create and manage
                your API keys at <a href="https://cloud.thinkgeo.com" target="_blank" rel="noopener">https://cloud.thinkgeo.com</a>.</p>
            <button>OK</button>
        </div>
    </div>

    <!-- Set up error message tip for coordinates inputing error. -->
    <div id="input-error">
        <p></p>
    </div>

    <!-- Set up instruction tip. -->
    <div id="instruction-tip">
        <p class="pc-tip hide">Right-click on the map to set your start and end points.</p>
        <p class="mobile-tip hide">Long-press on the map to set your start and end points.</p>
    </div>
</body>

</html>
            
          
!
            
              body {
  margin: 0;
  position: relative;
  font-family: 'Lucida Grande', 'Helvetica Neue', Helvetica, Arial, sans-serif;
}

.hide {
  display: none !important;
}

h3 {
  text-align: center;
}

hr {
  outline: 1px solid #eee;
}

.sidebar {
  width: 300px;
  /* height: calc(100% - 150px); */
  /* height: 0; */
  position: absolute;
  left: 1em;
  background-color: rgba(58, 58, 58, 0.85);
  color: #fff;
  top: 150px;
  bottom: 30px;
  z-index: 9;
}

.sidebar.empty{
  display: none;
}

.empty#result{
  display: none;
}

.point {
  position: absolute;
  left: 1em;
  top: 1em;
  z-index: 4;
  box-shadow: 0 0 0 2px rgba(0, 0, 0, 0.1);
  border-radius: 3px;
  background: #fff;
  width: 280px;
  padding: 10px;
}

#dragable-list {
  list-style: none;
  margin: 0;
  padding-left: 0;
}

#dragable-list .drag {
  cursor: grab;
  margin-left: 5px;
  line-height: 25px;
  vertical-align: middle;
  width: 7px;
  height: 2px;
  padding-top: 5px;
  display: inline-block;
  background-image: url(data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSI2cHgiIGhlaWdodD0iN3B4IiB2aWV3Qm94PSIwIDAgNiA3Ij48dGl0bGU+QXJ0Ym9hcmQgMTwvdGl0bGU+PHBhdGggZmlsbD0iIzc2NzY3NiIgZD0iTTAgMGg2djFoLTZ6TTAgM2g2djFoLTZ6TTAgNmg2djFoLTZ6Ii8+PHBhdGggZmlsbD0ibm9uZSIgZD0iTTAgMGg2djdoLTZ6Ii8+PC9zdmc+);
}

.dragging {
  background-color: #e9e9e9;
}

.point input {
  font-size: 12px;
  width: 160px;
  border: 0;
  background-color: transparent;
  height: 30px;
  margin: 0;
  color: rgba(0, 0, 0, .5);
  margin-left: 35px;
  padding: 0 40px 0px 0px;
  text-overflow: ellipsis;
  white-space: nowrap;
  overflow: hidden;
  border-bottom: 1px solid #ccc;
  border-radius: 0;
}

.point input:focus {
  outline: none;
}

.point li {
  position: relative;
}

#add-point {
  display: inline-block;
  padding: 5px;
  font-size: .9rem;
  cursor: pointer;
}

#add-point i {
  display: inline-block;
  height: 1rem;
  width: 1rem;
  line-height: 1rem;
  border: 1px solid #202020;
  border-radius: 50%;
  text-align: center;
  font-family: none;
  font-weight: bold;
}

#go {
  width: 70px;
  margin-left: 20px;
  cursor: pointer;
}

#dragable-list li:first-child,
#dragable-list li:last-child {
  margin-top: 0;
  margin-right: 0;
  margin-left: 0;
  position: relative;
}

#dragable-list li {
  margin-bottom: 5px;
  line-height: 30px;
}

#dragable-list li:last-child {
  margin-bottom: 6px;
}

#dragable-list label {
  text-align: center;
  line-height: 30px;
  font-weight: bolder;
  display: inline-block;
  width: 30px;
  height: 30px;
  background-size: cover;
  position: absolute;
  top: 0;
  left: 15px
}

#dragable-list li:not(:first-child):not(:last-child) label::after {
  content: ':'
}

#dragable-list li:first-child label {
  background-image: url('https://samples.thinkgeo.com/cloud/example/image/starting.png');
}

#dragable-list li:last-child label {
  background-image: url('https://samples.thinkgeo.com/cloud/example/image/ending.png');
}

#add-point {
  display: inline-block;
  padding: 5px;
  font-size: 1rem;
  cursor: pointer;
}

#add-point i {
  display: inline-block;
  height: 1rem;
  width: 1rem;
  border: 1px solid #202020;
  border-radius: 50%;
  text-align: center;
}

.switch {
  background-image: url('https://samples.thinkgeo.com/cloud/example/image/directions.svg');
  background-position-x: -527px;
  background-size: cover;
  display: inline-block;
  width: 30px;
  height: 30px;
  position: absolute;
  right: 0.8em;
  top: calc(50% - 15px);
  cursor: pointer;
  z-index: 9;
}

.switch.hide {
  display: none;
}

#result {
  position: relative;
  height: calc(100% - 40px);
}

#total {
  background-color: rgba(58, 58, 58, 1) !important;
  margin: 0;
  padding: 10px 12px;
  font-size: inherit;
  font-weight: 500;
}

.format-distance {
  margin-left: 4px;
}

.format-duration {
  color: rgba(255, 255, 255, 0.5);
  margin: 0 8px;
}

#input-error {
  position: absolute;
  top: -150px;
  left: 50%;
  width: auto;
  min-width: 300px;
  margin-left: auto;
  text-align: center;
  transform: translate(-50%, 0);
  transition: top 0.6s;
  z-index: 10;
}

#input-error.show {
  top: 15px;
  transition: top 0.6s;
}

#input-error p {
  line-height: 40px;
  padding-left: 10px;
  padding-right: 10px;
  border-radius: 3px;
  border-color: #f5c6cb;
  background-color: #f8d7da;
  color: #721c24;
}

.error-message {
  text-align: left;
  height: 30px;
  line-height: 30px;
  font-size: 18px;
  padding: 20px;
  padding-top: 40px;
}

.error-message a {
  color: #44a8ff;
}

#menu,
#closeMenu {
  margin-bottom: 15px;
  margin-right: 40px;
  width: 25%;
  color: #fff;
  background-color: #1890ff;
  border-color: #1890ff;
  text-shadow: 0 -1px 0 rgba(0, 0, 0, 0.12);
  -webkit-box-shadow: 0 2px 0 rgba(0, 0, 0, 0.045);
  box-shadow: 0 2px 0 rgba(0, 0, 0, 0.045);
  transition: all 0.3s cubic-bezier(0.645, 0.045, 0.355, 1);
  -webkit-transition: all 0.3s cubic-bezier(0.645, 0.045, 0.355, 1);
  height: 24px;
  padding: 0 7px;
  font-size: 14px;
  border-radius: 4px;
  cursor: pointer;
}

#menu,
#closeMenu {
  display: none
}

.loading {
  position: absolute;
  top: 40%;
  left: 0;
  width: 100%;
  height: 20px;
  z-index: 9;
}

.loading div {
  display: inline-block;
  border-radius: 50%;
  height: 20px;
  width: 20px;
  background-color: #ccc;
  margin-left: 10px;
}

.loading div:nth-child(1) {
  animation: loading 1s ease-in-out infinite;
}

.loading div:nth-child(2) {
  animation: loading 1s ease-in-out infinite;
  animation-delay: -0.8s;
}

.loading div:nth-child(3) {
  animation: loading 1s ease-in-out infinite;
  animation-delay: -1.6s;
}

@keyframes loading {
  0% {
    transform: scale(0);
  }
  50% {
    transform: scale(1);
  }
  100% {
    transform: scale(0);
  }
}

.loading div:nth-child(1) {
  margin-left: calc(50% - 50px);
}

.loading.hide {
  display: none;
}

.warnings {
  color: #f09032;
  width: 100% !important;
  padding-left: 20px;
  background: url(https://samples.thinkgeo.com/cloud/example/image/warning.svg) 0% center no-repeat;
  padding-top: 0 !important;
  margin-top: 10px;
  margin-bottom: 0px;
  font-size: 14px
}

.warnings-small {
  right: 40px;
  position: absolute;
  bottom: 10px;
  display: inline-block;
  font-size: 12px;
  color: #f09032;
  padding-left: 18px;
  background: url(https://samples.thinkgeo.com/cloud/example/image/warning.svg) 0% center no-repeat
}

.warnings-icon-small {
  display: inline-block;
  width: 15px;
  height: 15px;
  background: url(https://samples.thinkgeo.com/cloud/example/image/warning.svg) 50% center no-repeat
}

#boxes {
  height: calc(100% - 30px);
  overflow: auto;
  overflow-x: hidden;
}

#boxes::-webkit-scrollbar {
  width: 8px;
}

#boxes::-webkit-scrollbar-track {
  background: #3a3a3abf;
}

#boxes::-webkit-scrollbar-thumb {
  background: rgb(143, 143, 143);
}

.box {
  width: 280px;
  height: 45px;
  padding: 5px 10px;
  border-top: 1px dashed #787878;
  color: #b6b6b6;
  position: relative;
  cursor: pointer;
}

.box:hover {
  background: #4b4b4b
}

.box:hover .direction-wrap {
  border: 1px solid #565656;
  border-radius: 50%;
  background: #646464
}

.selectBox {
  background: #292929 !important
}

.box .instruction {
  position: absolute;
  top: 0;
  display: inline-block;
  width: 240px;
  height: 40px;
  line-height: 40px;
  font-size: 14px;
  font-weight: bold;
  color: #e1e1e1;
  text-overflow: ellipsis;
  overflow: hidden;
  white-space: nowrap;
}

.box .distance,
.box .duration {
  position: absolute;
  bottom: 10px;
  line-height: 12px;
  display: inline-block;
  font-size: 12px;
}

.box .duration {
  left: 120px;
}

.box .endPoint {
  line-height: 4;
}

.direction-wrap {
  display: inline-block;
  padding: 2px 4px;
  margin-right: 10px;
  margin-top: 10px;
}

i.direction {
  display: inline-block;
  width: 20px;
  height: 20px;
}

i.direction {
  background-image: url('https://samples.thinkgeo.com/cloud/example/image/directions.svg');
  background-size: cover;
}

i.direction.straight_on {
  background-position-x: 0;
}

i.direction.sharp_right {
  background-position-x: -31px;
}

i.direction.right {
  background-position-x: -61px;
}

i.direction.slight_right {
  background-position-x: -90px;
}

i.direction.turn-back {
  background-position-x: -122px;
}

i.direction.sharp_left {
  background-position-x: -150px;
}

i.direction.left {
  background-position-x: -180px;
}

i.direction.slight_left {
  background-position-x: -210px;
}

i.direction.back {
  background-position-x: -239px;
}

i.direction.around_circle_straight {
  background-position-x: -271px;
}

i.direction.start {
  background-position-x: -298px;
  margin-bottom: 0px !important;
}

i.direction.end {
  background-position-x: -323px;
}

#map {
  width: 100%;
  height: 100%;
  position: relative;
}

.ol-zoom {
  bottom: .5em;
  right: 0.5em;
  top: unset;
  left: unset;
}

.ol-zoom button {
  width: 1.2em !important;
  height: 1.2em !important;
}

#ol-contextmenu {
  width: 165px;
  position: absolute;
  z-index: 999;
  top: 30px;
  left: 300px;
  background-color: white;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.2);
  padding: 5px 0;
  border-radius: 2px;
  border: 1px solid #cccccc;
}

/* #result.hide, */
#ol-contextmenu.hide {
  display: none;
}

#ol-contextmenu ul {
  margin: 0;
  padding: 0;
}

#ol-contextmenu li {
  list-style: none;
  padding: 5px 0 5px 10px;
  border-bottom: 1px solid #e1e1e1;
}

#ol-contextmenu li:last-child{
  border-bottom: none;
}

#ol-contextmenu li:hover {
  cursor: pointer;
  background-color: #e1e1e1;
}

.closer {
  display: inline-block;
  text-decoration: none;
  margin-left: -20px;
  width: 10px;
  height: 10px;
  color: #333333;
}

.closer:hover {
  cursor: pointer;
}

.closer::after {
  font-size: 12px;
  content: "✖";
}

.transition-height {
  transition: height .5s;
}

#error-modal {
  position: fixed;
  top: 0;
  height: 100%;
  width: 100%;
  z-index: 99;
  background-color: #0000006e;
}

.modal-content {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  -ms-transform: translate(-50%, -50%);
  -webkit-transform: translate(-50%, -50%);
  -moz-transform: translate(-50%, -50%);
  -o-transform: translate(-50%, -50%);
  width: 600px;
  height: auto;
  text-align: right;
  padding: 10px;
  border-radius: 5px;
  font-size: 1.1rem;
  line-height: 1.5;
  background-color: #f0f0f0;
  border: 1px solid #b8b8b8;
}

.modal-content p {
  text-align: left;
}

.modal-content button {
  color: #fff;
  background-color: #3d3d3e;
  border-color: #3d3d3e;
  border-radius: 4px;
  padding: 0 7px;
  height: 30px;
  width: 60px;
  cursor: pointer;
  margin-right: 20px;
}

#instruction-tip {
  width: max-content;
  position: fixed;
  bottom: 20px;
  margin-left: 50%;
  transform: translateX(-50%);
  -ms-transform: translateX(-50%);
  -webkit-transform: translateX(-50%);
  -moz-transform: translateX(-50%);
  -o-transform: translateX(-50%);
  color: #fff;
}

#instruction-tip p {
  background-color: rgba(58, 58, 58, 0.85);
  border-radius: 3px;
  padding: 10px 20px;
}

#instruction-tip.gone {
  bottom: -100px;
  opacity: 0;
  transition: bottom 500ms ease-out, opacity 300ms ease-out;
}
/* Mobile phone */

@media (max-width: 767px) {
  .sidebar {
    height: 0;
    height: fit-content !important;
  }
  .point {
    width: 100%;
    left: 0;
    top: 0;
    padding-left: 0;
    padding-right: 0;
    border-radius: 0;  
  }
  .point label {
    margin-left: 10px;
    width: 25px;
    height: 25px;
  }
  .point input {
    height: 20px;
    margin-left: 40px;
    width: 60%;
  }
  .switch {
    width: 20px;
    height: 18px;
    top: calc(50% - 8px);
    background-position-x: -315px;
    right: 1em;
  }
  input::-webkit-input-placeholder {
    /* WebKit browsers */
    color: #cfcfcf;
  }
  input:-moz-placeholder {
    /* Mozilla Firefox 4 to 18 */
    color: #cfcfcf;
  }
  input::-moz-placeholder {
    /* Mozilla Firefox 19+ */
    color: #cfcfcf;
  }
  input:-ms-input-placeholder {
    /* Internet Explorer 10+ */
    color: #cfcfcf;
  }
  .start label {
    background-position-x: -400px;
  }
  .end label {
    background-position-x: -824px;
  }
  .btn-wrap {
    bottom: 0;
  }
  .warnings-small {
    bottom: 17px
  }
  #dragable-list {
    max-height: 100px;
    overflow: auto;
  }
  #dragable-list .drag {
    cursor: grab;
    margin-left: 15px;
    line-height: 25px;
    vertical-align: middle;
    width: 7px;
    height: 2px;
    padding-top: 5px;
    display: inline-block;
    background-image: url(data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSI2cHgiIGhlaWdodD0iN3B4IiB2aWV3Qm94PSIwIDAgNiA3Ij48dGl0bGU+QXJ0Ym9hcmQgMTwvdGl0bGU+PHBhdGggZmlsbD0iIzc2NzY3NiIgZD0iTTAgMGg2djFoLTZ6TTAgM2g2djFoLTZ6TTAgNmg2djFoLTZ6Ii8+PHBhdGggZmlsbD0ibm9uZSIgZD0iTTAgMGg2djdoLTZ6Ii8+PC9zdmc+);
  }

  #add-point,
  #go{
    display: none;
  }

  #menu {
    width: auto;
    margin-right: 0;
    margin-bottom: 0;
    display: inline-block;
    padding-left: 20px;
    background: url(https://samples.thinkgeo.com/cloud/example/image/up.svg) 2px no-repeat #1890ff;
    position: absolute;
    right: 0.6em;
    top: 0.5em;
  }
  #closeMenu {
    width: auto;
    margin-right: 0;
    margin-bottom: 0;
    padding-left: 20px;
    background: url(https://samples.thinkgeo.com/cloud/example/image/down.svg) 2px no-repeat #1890ff;
    position: absolute;
    top: 0.6em;
    right: 1em
  }
  .menu-wrap {
    width: 47% !important;
  }
  #map {
    width: 100%;
  }
  #result {
    position: fixed;
    bottom: 0;
    left: 0;
    z-index: 100;
    height: 0px;
    overflow: hidden;
    width: 100%;
    background-color: #3a3a3a;
  }
  #result.error-on-mobile {
    height: 116px !important;
  }
  #result.error-on-mobile #boxes {
    height: 100%;
  }
  #result.error-on-mobile #total {
    display: none;
  }
  #result.error-on-mobile .error-message {
    height: auto;
    line-height: 20px;
  }
  #boxes {
    height: calc(100% - 53px);
    -webkit-overflow-scrolling: touch
  }
  .box {
    width: unset;
    padding: 10px 20px;
    margin-left: 0px;
    height: unset;
    margin-bottom: 0;
    border-top: 0;
  }
  #result>p>span {
    display: inline-block;
    width: 24%;
    padding-top: 0px;
    text-align: center;
  }
  .box .instruction {
    height: unset;
    line-height: 12px;
    top: unset;
    width: 270px;
  }
  i.direction {
    margin-top: 0;
  }
  .box .distance,
  .box .duration {
    bottom: unset;
    line-height: 49px;
  }
  .ol-zoom {
    right: .5em;
    top: unset;
    left: unset;
    bottom: 5em;
    padding: 0px;
    transition: bottom .5s;
  }
  .ol-rotate {
    right: .5em;
    padding: 0px;
    top: 13em;
    left: unset;
    transition: bottom .5s;
    bottom: unset
  }
  .error-message {
    text-align: left;
    height: 30px;
    line-height: 30px;
    font-size: 14px;
    padding: 20px;
  }
  .modal-content {
    width: 80vw;
  }
  #instruction-tip p {
    font-size: 12px;
  }
}

@media (max-width: 576px) {
  #clear {
    right: 0.5em;
  }
  #result>p>span {
    width: auto
  }
  .menu-wrap {
    width: 53% !important;
  }
  .box {
    width: unset;
    padding: 10px 3px;
  }
}
            
          
!
            
              /*===========================================================================*/
// Routing in North America
// Sample map by ThinkGeo
//
//   1. ThinkGeo Cloud API Key
//   2. Map Control Setup
//   3. ThinkGeo Map Icon Fonts
//   4. Routing Setup
//   5. Routing Features Handler Setup
//   6. Result Rendering
//   7. Error Event Handlers
//   8. UI control setup
//   9. Derive the Custom Class Drag
//   10. Event Listeners
/*===========================================================================*/

/*---------------------------------------------*/
// 1. ThinkGeo Cloud API Key
/*---------------------------------------------*/

// First, let's define our ThinkGeo Cloud API key, which we'll use to
// authenticate our requests to the ThinkGeo Cloud API.  Each API key can be
// restricted for use only from a given web domain or IP address.  To create your
// own API key, you'll need to sign up for a ThinkGeo Cloud account at
// https://cloud.thinkgeo.com.
const apiKey = 'WPLmkj3P39OPectosnM1jRgDixwlti71l8KYxyfP2P0~';

/*---------------------------------------------*/
// 2. Map Control Setup
/*---------------------------------------------*/

// Here's where we set up our map.  We're going to create layers, styles,
// and define our initial view when the page first loads.

// In this custom object, we're going to define eight styles:
//   1. The appearance of the start point icon.
//   2. The appearance of the end point icon.
//   3. The appearance of the waypoint icon.
// 	 4. The appearance of the route line.
//   5. The appearance of the route line halo.
//   6. The appearance of the line of start point to snap point.
//   7. The appearance of the radius circle when hovering the segment route.
//   8. The appearance of the arrow when clicking the target segment route.
const styles = {
    start: new ol.style.Style({
        image: new ol.style.Icon({
            anchor: [0.5, 0.9],
            anchorXUnits: 'fraction',
            anchorYUnits: 'fraction',
            opacity: 1,
            crossOrigin: 'Anonymous',
            src: 'https://samples.thinkgeo.com/cloud/example/image/starting.png'
        })
    }),
    end: new ol.style.Style({
        image: new ol.style.Icon({
            anchor: [0.5, 0.9],
            anchorXUnits: 'fraction',
            anchorYUnits: 'fraction',
            opacity: 1,
            crossOrigin: 'Anonymous',
            src: 'https://samples.thinkgeo.com/cloud/example/image/ending.png'
        })
    }),
    mid: new ol.style.Style({
        image: new ol.style.Circle({
            radius: 10,
            fill: new ol.style.Fill({
                color: [255, 255, 255, 19]
            }),
            stroke: new ol.style.Stroke({
                color: [29, 93, 48, 1],
                width: 6
            })
        })
    }),
    line: new ol.style.Style({
        stroke: new ol.style.Stroke({
            width: 6,
            color: [34, 109, 214, 0.9]
        })
    }),
    line_halo: new ol.style.Style({
        stroke: new ol.style.Stroke({
            width: 10,
            lineCap: 'round',
            color: [34, 109, 214, 1]
        })
    }),
    walkLine: new ol.style.Style({
        stroke: new ol.style.Stroke({
            width: 2,
            lineDash: [5, 3],
            color: [34, 109, 214, 1]
        })
    }),
    resultRadius: new ol.style.Style({
        image: new ol.style.Circle({
            radius: 15,
            fill: new ol.style.Fill({
                color: [255, 102, 0, 0.4]
            }),
            stroke: new ol.style.Stroke({
                color: [255, 102, 0, 0.8],
                width: 1
            })
        })
    }),
    arrowLine: new ol.style.Style({
        stroke: new ol.style.Stroke({
            color: [10, 80, 18, 1],
            width: 6
        })
    })
};

// Now we'll create the base layer for our map.  The base layer uses the ThinkGeo
// Cloud Maps Vector Tile service to display a detailed street map.  For more
// info, see our wiki:
// https://wiki.thinkgeo.com/wiki/thinkgeo_cloud_maps_vector_tiles
const lightLayer = new ol.mapsuite.VectorTileLayer('https://cdn.thinkgeo.com/worldstreets-styles/3.0.0/light.json', {
    apiKey: apiKey,
    layerName: 'light'
});

// Create a default view for the map when it starts up.
const view = new ol.View({
    // Center the map on the United States and start at zoom level 3.
    center: ol.proj.fromLonLat([-96.7962, 42.79423]),
    maxResolution: 40075016.68557849 / 512,
    progressiveZoom: false,
    zoom: 3,
    minZoom: 2,
    maxZoom: 19
});

// This function will create and initialize our interactive map.
// We'll call it later when our POI icon font has been fully downloaded,
// which ensures that the POI icons display as intended.
let map;
let vectorSource;
let curCoord;
// Define a name space: app.
let app = {};
const initializeMap = () => {
    map = new ol.Map({
        renderer: 'webgl',
        loadTilesWhileAnimating: true,
        loadTilesWhileInteracting: true,
        // Add our previously-defined ThinkGeo Cloud Vector Tile layer to the map.
        layers: [lightLayer],
        // States that the HTML tag with id="map" should serve as the container for our map.
        target: 'map',
        view: view,
        // Add an interaction to map that allows drag point icons.
        interactions: ol.interaction.defaults().extend([new app.Drag()])
    });

    addRoutingLayer();
    mobileCompatibility();

    // Add a "pointermove" listener to map which is when the pointer is moving over the start, end and mid point, the cursor should be "pointer" appearance.
    map.on('pointermove', function (e) {
        if (e.dragging) {
            return;
        }
        const pixel = map.getEventPixel(e.originalEvent);
        const options = {
            // Only find feature on the routing layer not the base vector tile layer.
            layerFilter: function (layer) {
                if (layer instanceof ol.layer.VectorTile) {
                    return false;
                }
                return true;
            }
        };
        const hit = map.hasFeatureAtPixel(pixel, options);
        let cursor = false;
        if (hit) {
            const features = map.getFeaturesAtPixel(pixel, options);
            features.some((feature) => {
                let featureName = feature.get('name');
                if (featureName === 'start' || featureName === 'end' || featureName === 'mid') {
                    cursor = true;
                    return true;
                }
            });
        } else {
            cursor = false;
        }
        map.getTargetElement().style.cursor = cursor ? 'pointer' : '';
    });
};

// Do some compatibility on mible and IOS client.
const mobileCompatibility = () => {
    let u = navigator.userAgent;
    const isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1;
    const isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
    let left, top;
    let clientWidth = document.documentElement.clientWidth;
    let clientHeight = document.documentElement.clientHeight;
    const contextmenu = document.querySelector('#ol-contextmenu');
    const insTip = document.querySelector('#instruction-tip');
    let timeOutEvent;
    const contextWidth = 165;
    const contextHeight = 127;

    // Show the right click context menu on different platform.
    if (isiOS) {
        map.getViewport().addEventListener('gesturestart', function (e) {
            clearTimeout(timeOutEvent);
            timeOutEvent = 0;
            return false;
        });

        map.getViewport().addEventListener('touchstart', function (e) {
            e.preventDefault();
            if (e.touches.length != 1) {
                clearTimeout(timeOutEvent);
                timeOutEvent = 0;
                return false;
            }
            timeOutEvent = setTimeout(function () {
                if (e.touches.length == 1) {
                    timeOutEvent = 0;
                    left =
                        e.changedTouches[0].clientX + contextWidth > clientWidth ?
                        clientWidth - contextWidth - 3 :
                        e.changedTouches[0].clientX;
                    top =
                        e.changedTouches[0].clientY + contextHeight > clientHeight ?
                        clientHeight - contextHeight - 13 :
                        e.changedTouches[0].clientY;
                    contextmenu.style.left = left + 'px';
                    contextmenu.style.top = top + 'px';
                    let point = map.getEventCoordinate(e);
                    curCoord = point;
                    hideOrShowContextMenu('show');
                    insTip.classList.add('gone');
                }
            }, 500);
        });

        map.getViewport().addEventListener('touchend', function (event) {
            clearTimeout(timeOutEvent);
            if (timeOutEvent != 0) {
                hideOrShowContextMenu('hide');
            }
            return false;
        });

        map.getViewport().addEventListener('touchmove', function (event) {
            clearTimeout(timeOutEvent);
            timeOutEvent = 0;
            return false;
        });
    } else {
        map.getViewport().addEventListener('contextmenu', (e) => {
            hideOrShowContextMenu('show');
            insTip.classList.add('gone');
            left = e.clientX + contextWidth > clientWidth ? clientWidth - contextWidth - 3 : e.clientX;
            top =
                e.clientY + contextmenu.offsetHeight > clientHeight ?
                clientHeight - contextmenu.offsetHeight - 1 :
                e.clientY;

            contextmenu.style.left = left + 'px';
            contextmenu.style.top = top + 'px';
            let point = map.getEventCoordinate(e);
            curCoord = point;
        });
    }

    // Show the mobile instruction tip on Android and IOS, and show pc tip on PC.
    if (isiOS || isAndroid) {
        document.querySelector('.mobile-tip').classList.remove('hide');
    } else {
        document.querySelector('.pc-tip').classList.remove('hide');
    }
};

// Create the routing layer and add it to map.
const addRoutingLayer = () => {
    vectorSource = new ol.source.Vector();
    let routingLayer = new ol.layer.Vector({
        source: vectorSource,
        layerName: 'routing'
    });
    map.addLayer(routingLayer);
};

/*---------------------------------------------*/
// 3. ThinkGeo Map Icon Fonts
/*---------------------------------------------*/

// Finally, we'll load the Map Icon Fonts using ThinkGeo's WebFont loader.
// The loaded Icon Fonts will be used to render POI icons on top of the map's
// background layer.  We'll initalize the map only once the font has been
// downloaded.  For more info, see our wiki:
// https://wiki.thinkgeo.com/wiki/thinkgeo_iconfonts

WebFont.load({
    custom: {
        families: ['vectormap-icons'],
        urls: ['https://cdn.thinkgeo.com/vectormap-icons/2.0.0/vectormap-icons.css'],
        testStrings: {
            'vectormap-icons': '\ue001'
        }
    },
    // The "active" property defines a function to call when the font has
    // finished downloading.  Here, we'll call our initializeMap method.
    active: initializeMap
});

/*---------------------------------------------*/
// 4. Routing Setup
/*---------------------------------------------*/

// At this point we'll built up the methods and functionality that will
// actually perform the routing using the ThinkGeo Cloud and then
// display the results on the map.

// We use thinkgeocloudclient.js, which is an open-source Javascript SDK for making
// request to ThinkGeo Cloud Service. It simplifies the process of the code of request.

// We need to create the instance of Routing client and authenticate the API key.
const routingClient = new tg.RoutingClient(apiKey);

// Get some items which we'll use to judge if we should perform the routing service or show error tips.
const findRoute = (showError, notClearAll) => {
    if (!notClearAll) {
        vectorSource.clear();
    }
    hideOrShowResultBox('hide');
    const points = getAllPoints();
    const inputsCount = document.querySelectorAll('.point input');
    if (points && points.length >= 2 && inputsCount.length === points.length) {
        // Add the point which is not added to map.
        const pointsLength = points.length;
        points.forEach((point, index) => {
            if (pointsLength - 1 === index) {
                type = 'end';
            } else if (0 === index) {
                type = 'start';
            } else {
                type = 'mid';
            }
            addPointFeature(type, point);
        });
        performRouting();
    } else if (showError) {
        showErrorTip('Please input correct coordinates!');
    }
};

// This method performs the actual routing using the ThinkGeo Cloud.
// By passing the coordinates of the map location, we can
// get back a the route message as we send the request.  For more details, see our wiki:
// https://wiki.thinkgeo.com/wiki/thinkgeo_cloud_routing
const performRouting = () => {
    const points = getAllPoints();
    const inputsCount = document.querySelectorAll('.point input');
    if (points && points.length >= 2 && inputsCount.length === points.length) {
        hideErrorTip();
        document.querySelector('.loading').classList.remove('hide');
        const options = {
            turnByTurn: true,
            srid: 3857
        };
        const callback = (status, response) => {
            const result = document.querySelector('#result');
            if (status === 200) {
                result.classList.remove('error-on-mobile');
                document.querySelector('.loading').classList.add('hide');
                hideOrShowResultBox('show');
                handleResponse(response);
            } else {
                hideOrShowResultBox('show');
                document.querySelector('.loading').classList.add('hide');
                document.querySelector('#total').innerHTML = '';

                if (document.body.clientWidth <= 767) {
                    result.classList.add('error-on-mobile');
                }
                if (status === 400) {
                    const data = response.data;
                    let message = '';
                    Object.keys(data).forEach((key) => {
                        message = message + data[key] + '<br />';
                    });
                    result.querySelector('#boxes').innerHTML = `<div class="error-message">${message}</div>`;
                } else if (status === 401 || status === 410 || status === 404) {
                    result.querySelector('#boxes').innerHTML = `<div class="error-message">${response.error
						.message}</div>`;
                } else if (status === 'error') {
                    errorLoadingTile();
                } else {
                    result.querySelector('#boxes').innerHTML = `<div class="error-message">Request failed.</div>`;
                }
            }
        };

        const points_ = points.map((point) => {
            return {
                x: point[0],
                y: point[1]
            };
        });

        routingClient.getRoute(points_, callback, options);
    }
};

// Handle the response when we get the route result from server.
const handleResponse = (res) => {
    const data = res.data;
    const routes = data.routes[0];
    generateBox(routes);
    const waypointsCoord = data.waypoints.map((item) => {
        return [item.coordinate.y, item.coordinate.x];
    });
    addWalkLinesFeatures(waypointsCoord);
};

// Get the coordinates array from the input attribute -- data-origin.
const getCoordFromDataOrigin = (dataOriginValue) => {
    let value = dataOriginValue.split(',');
    if (value.length === 2) {
        return [Number(value[0]), Number(value[1])];
    } else {
        return [];
    }
};

// Get all the input points coordinates from the input group.
const getAllPoints = () => {
    let points = [];
    const allInputs = document.querySelectorAll('.point input');
    allInputs.forEach((input) => {
        const value = input.getAttribute('data-origin');
        value ? points.push(getCoordFromDataOrigin(value)) : null;
    });
    return points;
};

/*---------------------------------------------*/
// 5. Routing Features Handler Setup
/*---------------------------------------------*/

// This step we create several method for you to operate the features on the routing layer.
// Since all the preparation have been done, we need to do have some method to handle the
// features we have added to the map.

// Add point feature to map by passing the point name and coordinates.
const addPointFeature = (name, coord) => {
    if (name === 'start') {
        removeFeatureByName(name);
    } else if (name === 'end') {
        removeFeatureByName(name);
    }
    let feature = new ol.Feature({
        geometry: new ol.geom.Point(coord),
        name: name
    });
    feature.setStyle(styles[name]);
    vectorSource.addFeatures([feature]);
};

// Add the route line feature by passing the line wkt data from what we get from response.
const addRouteFeature = (wkt) => {
    const format = new ol.format.WKT();
    const routeFeature = format.readFeature(wkt);
    routeFeature.set('name', 'line');
    routeFeature.setStyle([styles.line, styles.line_halo]);
    vectorSource.addFeature(routeFeature);
};

// Add the lines from the point we start from to the nearest route.
const addWalkLinesFeatures = (waypointsCoord) => {
    let features = [];
    const points = getAllPoints();
    points.forEach((point, index) => {
        const feature = new ol.Feature({
            geometry: new ol.geom.LineString([point, waypointsCoord[index]]),
            name: 'line'
        });
        features.push(feature);
    });
    vectorSource.addFeatures(features);
};

// Add a radius circle the segment point where we hovering from.
const addResultRadius = (coord) => {
    removeFeatureByName('resultRadius');
    let center = coord;
    let resultRadiusFeature = new ol.Feature({
        geometry: new ol.geom.Point(center),
        name: 'resultRadius'
    });
    resultRadiusFeature.setStyle(styles.resultRadius);
    vectorSource.addFeature(resultRadiusFeature);
};

// Add the arrow icon when we zoom in to which segment route we click.
const addArrow = (penultCoord, lastCoord) => {
    removeFeatureByName('arrow');

    let feature = new ol.Feature({
        geometry: new ol.geom.Point(lastCoord),
        name: 'arrow'
    });

    const dx = lastCoord[0] - penultCoord[0];
    const dy = lastCoord[1] - penultCoord[1];

    const rotation = Math.atan2(dy, dx);

    const arrowStyle = new ol.style.Style({
        image: new ol.style.Icon({
            anchor: [0.5, 0.5],
            anchorXUnits: 'fraction',
            anchorYUnits: 'fraction',
            crossOrigin: 'Anonymous',
            src: 'https://samples.thinkgeo.com/cloud/example/image/arrow.png',
            rotateWithView: true,
            rotation: -rotation
        })
    });

    feature.setStyle(arrowStyle);
    vectorSource.addFeature(feature);
};

// Add the arrow line when we zoom in to which segment route we click.
const addTurnLine = (penultCoord, lastCoord, lineSecondCoord) => {
    let feature = new ol.Feature({
        geometry: new ol.geom.LineString([penultCoord, lastCoord, lineSecondCoord]),
        name: 'line'
    });

    feature.setStyle(styles.arrowLine);
    vectorSource.addFeature(feature);
};

// Remove a point feature by passing the coordinates.
const removeFeatureByCoord = (coord) => {
    const features = vectorSource.getFeatures();
    features.some((feature) => {
        if (feature.getGeometry().getCoordinates().toString() === coord) {
            vectorSource.removeFeature(feature);
            return true;
        }
    });
};

// Remove the point or line features by passing the feature name.
const removeFeatureByName = (featureName) => {
    if (vectorSource) {
        const features = vectorSource.getFeatures();
        for (let i = 0, l = features.length; i < l; i++) {
            let feature = features[i];
            if (feature.get('name') === featureName) {
                vectorSource.removeFeature(feature);
            }
        }
    }
};

// Get the feature by feature's name.
const getFeatureByName = (name) => {
    let feature_;
    vectorSource.getFeatures().some((feature) => {
        if (feature.get('name') === name) {
            feature_ = feature;
            return true;
        }
    });
    return feature_;
};

/*---------------------------------------------*/
// 6. Result Rendering
/*---------------------------------------------*/

// Since all we have got the result from server, we need to show the result in the left sidebar box.

// Calculate the coordinates what we use to draw the arrow lines by passing the two coordinates.
const lerp = (firstCoord, secondCoord) => {
    var resolution = view.getResolution();
    var x1 = firstCoord[0];
    var y1 = firstCoord[1];
    var x2 = secondCoord[0];
    var y2 = secondCoord[1];
    var length = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2)) / resolution;
    var x, y;

    if (length > 50) {
        var interpolate = 50 / length;
        var x = ol.math.lerp(x1, x2, interpolate);
        var y = ol.math.lerp(y1, y2, interpolate);

        return [x, y];
    }

    return secondCoord;
};

// Format the distance and duration data from what we get from response.
const formatDistanceAndDuration = (distance, duration) => {
    let distance_;
    let duration_;
    if (distance >= 1000) {
        distance_ = distance / 1000;
        distance_ = Math.round(distance_ * 10) / 10;
        distance_ = new Intl.NumberFormat().format(distance_);
        distance_ = distance_ + 'km';
    } else {
        distance_ = Math.round(distance * 10) / 10;
        distance_ = distance_ + 'm';
    }

    if (duration > 60) {
        let hours = parseInt(duration / 60);
        let min = Math.round(duration % 60);
        hours = new Intl.NumberFormat().format(hours);
        duration_ = `${hours}h ${min}min`;
    } else {
        duration_ = Math.round(duration * 10) / 10;
        duration_ = `${duration_}min`;
    }

    return {
        distance: distance_,
        duration: duration_
    };
};

// Create the sidebar result container and inner items once we have got the response from server.
const generateBox = (routes) => {
    let lastLinePoint;
    let firstLinePoint;
    const lineWkt = routes.geometry;
    let segments = routes.segments;
    let count = 0;

    let distance = Math.round(routes.distance * 100) / 100;
    let duration = Math.round(routes.duration * 100) / 100;
    let format = formatDistanceAndDuration(distance, duration);
    let warnings;
    if (routes.warnings) {
        let str = ``;
        Object.keys(routes.warnings).map((key) => {
            str += `${routes.warnings[key]}  `;
        });
        warnings = `<p class="warnings">${str} </p> `;
    } else {
        warnings = '';
    }

    let boxesDom = document.querySelector('#boxes');
    let totalDom = document.querySelector('#total');
    let total = `<span class='format-distance'>${format.distance}</span>
				 <span class='format-duration'>${format.duration}</span> 
				 ${warnings}
				  <button id='menu'></button>
				  <button id='closeMenu'></button>
				`;
    totalDom.innerHTML = total;
    boxesDom.innerHTML = '';
    addRouteFeature(lineWkt);
    let lastLinePenultCoord = [];
    let lastLineLastCoord = [];
    let isTurn = true;
    let polylineCoords = [];

    if (segments) {
        let segments_ = segments
            .map((item) => {
                let polyline = item.geometry;
                let polylineCoord = polyline.split('(')[1].split(')')[0].split(',');
                let secondPointFromStart = findSecondPointFromStart(polylineCoord);
                return secondPointFromStart ? item : false;
            })
            .filter((item) => item);

        segments_.forEach((item) => {
            count++;
            let polyline = item.geometry;
            let polylineCoord = polyline.split('(')[1].split(')')[0].split(',');
            let secondPointFromStart = findSecondPointFromStart(polylineCoord);
            let secondPointFromEnd = findSecondPointFromEnd(polylineCoord);

            let startCoord = polylineCoord[0];
            polylineCoords.push(polylineCoord);
            let instruction = item.instruction;
            const maneuverType = item.maneuverType;
            let format = formatDistanceAndDuration(item.distance, item.duration);
            distance = format.distance;
            duration = format.duration;
            let className;
            let warnStr;
            if (item.isToll) {
                warnStr = '<span class="warnings-small ">Toll road</span>';
            } else {
                warnStr = '';
            }
            isTurn = true;

            switch (maneuverType) {
                case 'turn-left':
                    className = `left`;
                    break;
                case 'sharp-left':
                    className = `sharp_left`;
                    break;
                case 'slightly-left':
                    className = `slight_left`;
                    break;
                case 'turn-right':
                    className = `right`;
                    break;
                case 'sharp-right':
                    className = `sharp_right`;
                    break;
                case 'slightly-right':
                    className = `slight_right`;
                    break;
                case 'straight-on':
                    className = `straight_on`;
                    isTurn = false;
                    break;
                case 'u-turn':
                    className = `turn-back`;
                    break;
                case 'start':
                    className = `start`;
                    isTurn = false;
                    break;
                case 'stop':
                    className = `end`;
                    isTurn = false;
                    break;
                case 'roundabout':
                    className = `around_circle_straight`;
                    break;
            }

            let boxInnerDom =
                count !== segments_.length ?
                `<span class="direction-wrap" ><i class="direction ${className}"></i></span><span title='${instruction}' class="instruction">${instruction}</span>
				<span class="distance">${distance}</span><span  class="duration">${duration}</span>${warnStr}` :
                `<span class="direction-wrap" ><i class="direction ${className}"></i></span><span class="instruction endPoint">${instruction}</span>`;
            let boxDom = document.createElement('DIV');
            boxDom.className = 'box';
            boxDom.id = count;
            if (count === 1) {
                firstLinePoint = startCoord.split(' ');
                firstLinePoint = [+firstLinePoint[0], +firstLinePoint[1]];

                let penult = secondPointFromEnd;
                penultPoint = penult.split(' ');
                penultPoint = [+penultPoint[0], +penultPoint[1]];

                let last = polylineCoord[polylineCoord.length - 1];
                lastPoint = last.split(' ');
                lastPoint = [+lastPoint[0], +lastPoint[1]];

                lastLinePenultCoord = penult;
                lastLineLastCoord = last;

                let penult_ = polylineCoord[0];
                penult_ = penult_.split(' ');

                let last_ = polylineCoord[1];
                let lastPoint_ = last_.split(' ');
                lastPoint_ = [+lastPoint_[0], +lastPoint_[1]];
            }

            if (count === segments_.length) {
                let endCoord = polylineCoord[polylineCoord.length - 1];
                lastLinePoint = endCoord.split(' ');
                lastLinePoint = [+lastLinePoint[0], +lastLinePoint[1]];
                boxDom.setAttribute('coord', endCoord);
            } else {
                boxDom.setAttribute('coord', startCoord);
            }

            if (count >= 2) {
                boxDom.setAttribute('lastLinePenultCoord', lastLinePenultCoord);
                boxDom.setAttribute('lastLineLastCoord', lastLineLastCoord);
                isTurn && boxDom.setAttribute('lineSecondCoord', secondPointFromStart);

                let penult = secondPointFromEnd;
                penultPoint = penult.split(' ');
                penultPoint = [+penultPoint[0], +penultPoint[1]];

                let last = polylineCoord[polylineCoord.length - 1];
                lastPoint = last.split(' ');
                lastPoint = [+lastPoint[0], +lastPoint[1]];

                lastLinePenultCoord = penult;
                lastLineLastCoord = last;
            }

            boxDom.setAttribute('instruction', instruction);
            boxDom.innerHTML = boxInnerDom;
            boxesDom.appendChild(boxDom);
        });
    } else {
        // The two points are too close to find the route, so there are no segments. We need to add start point and end point in the result box.
        let boxInnerDomStart = `<span class="direction-wrap" ><i class="direction start"></i></span><span title="Start" class="instruction">Start</span>
		<span class="distance">0 km</span><span  class="duration">0 min</span>`;
        let boxInnerDomEnd = `<span class="direction-wrap" ><i class="direction end"></i></span><span title="End" class="instruction">End</span>
		<span class="distance">0 km</span><span  class="duration">0 min</span>`;
        let boxDomStart = document.createElement('DIV');
        let boxDomEnd = document.createElement('DIV');
        boxDomStart.className = 'box';
        boxDomEnd.className = 'box';
        boxDomStart.innerHTML = boxInnerDomStart;
        boxDomEnd.innerHTML = boxInnerDomEnd;
        boxDomStart.setAttribute('coord', getAllPoints()[0].join(' '));
        boxDomEnd.setAttribute('coord', getAllPoints()[getAllPoints().length - 1].join(' '));
        boxesDom.appendChild(boxDomStart);
        boxesDom.appendChild(boxDomEnd);
    }

    if (document.body.clientWidth <= 767) {
        const result = document.getElementById('result');

        result.style.height = 36 + 'px';
        const menu = document.getElementById('menu');
        const closeMenu = document.getElementById('closeMenu');

        menu.addEventListener('click', () => {
            result.style.height = 240 + 'px';
            result.style.overflowY = 'auto';
            menu.style.display = 'none';
            closeMenu.style.display = 'inline-block';
        });

        closeMenu.addEventListener('click', () => {
            result.style.height = 36 + 'px';
            result.style.overflow = 'hidden';
            closeMenu.style.display = 'none';
            menu.style.display = 'inline-block';
        });
    }
};

// In order to draw the arrow or arrow line on the turn point, we need to find the points what we need.
const findSecondPointFromStart = (coordinates) => {
    for (let i = 0; i < coordinates.length - 1; i++) {
        if (coordinates[i + 1] != coordinates[i]) {
            return coordinates[i + 1];
        }
    }

    return false;
};

const findSecondPointFromEnd = (coordinates) => {
    for (let i = coordinates.length - 1; i > 0; i--) {
        if (coordinates[i - 1] != coordinates[i]) {
            return coordinates[i - 1];
        }
    }

    return false;
};

/*---------------------------------------------*/
// 7. Error Event Handlers
/*---------------------------------------------*/

// These events allow you to perform custom actions when
// a map tile encounters an error while loading.
const errorLoadingTile = () => {
    const errorModal = document.querySelector('#error-modal');
    if (errorModal.classList.contains('hide')) {
        // Show the error tips when Tile loaded error.
        errorModal.classList.remove('hide');
    }
};

const setLayerSourceEventHandlers = (layer) => {
    let layerSource = layer.getSource();
    layerSource.on('tileloaderror', function () {
        errorLoadingTile();
    });
};

setLayerSourceEventHandlers(lightLayer);

// When you are ready to perform a routing request, but some input boxes are empty. Then we'll show the
// input error tip, after 3000ms, we'l hide the error tip automatically.
let timer;
const showErrorTip = (content) => {
    if (timer) {
        clearTimeout(timer);
    }
    const tip = document.querySelector('#input-error');
    tip.querySelector('p').innerHTML = content;
    tip.classList.add('show');
    timer = setTimeout(function () {
        tip.classList.remove('show');
    }, 3000);
};

const hideErrorTip = () => {
    document.querySelector('#input-error').classList.remove('show');
};

/*---------------------------------------------*/
// 8. UI control setup
/*---------------------------------------------*/

// Get the last node from a collection nodes by passing the DOM selector.
const getLastNodeBySelector = (selector) => {
    const inputs = document.querySelectorAll(selector);
    return inputs[inputs.length - 1];
};

// When we click the clear item in the context menu, we'll mak all the input empty using this method.
const clearInputBox = () => {
    const inputs = document.querySelectorAll('.point input');
    inputs.forEach((input) => {
        input.setAttribute('data-origin', '');
        input.value = '';
    });
};

// When there are results, show the result box, otherwise, hide the result box.
const hideOrShowResultBox = (visible) => {
    const sidebar = document.querySelector('.sidebar');
    if (visible === 'show') {
        sidebar.classList.remove('empty');
        resetSidebarHeight();
    } else {
        sidebar.classList.add('empty');
    }
}

// Since add or delete the input box, the result box height will automatically
// change. Here, we use this method to refresh the result sidebar height.
const resetSidebarHeight = () => {
    const resultSidebar = document.querySelector('.sidebar');
    const topHeight = document.querySelector('.point').clientHeight + 30;
    resultSidebar.style.top = `${topHeight}px`;
};

// When click the add point button or the item of add route in the context menu,
// we'll add an input box in the sidebar input group.
const addInputBox = (coord, readonly) => {
    const inputs = document.querySelectorAll('#dragable-list input');
    if (inputs.length === 10) {
        showErrorTip('No more than 10 points.');
        return;
    }
    removeFeatureByName('line');
    removeFeatureByName('arrow');
    hideOrShowResultBox('hide');
    const lastPoint = getLastNodeBySelector('#dragable-list li');
    const lastInput = lastPoint.querySelector('input');
    const parent = document.querySelector('#dragable-list');

    const newNode = document.createElement('li');
    newNode.classList.add('via');
    let dataOrigin;
    let inputValue;
    if (coord) {
        dataOrigin = coord;
        let coord_ = new ol.proj.toLonLat(coord);
        inputValue = [coord_[1].toFixed(8), coord_[0].toFixed(8)];
    } else {
        dataOrigin = lastInput.getAttribute('data-origin');
        inputValue = lastInput.value;
        lastInput.value = '';
        lastInput.setAttribute('data-origin', '');
    }
    
    let attr = '';
    if(readonly){
        attr = 'readonly';
    }

    newNode.innerHTML = `
	<i class="drag"></i><label></label>
	<input value="${inputValue}" data-origin="${dataOrigin}" placeholder="To" ${attr}/>
	<span class=""></span>
	<a class="closer"></a>`;
    parent.insertBefore(newNode, lastPoint);
};

// Hide or show the context menu when we click or right click the map.
const hideOrShowContextMenu = (style) => {
    let contextmenu = document.querySelector('#ol-contextmenu');
    switch (style) {
        case 'hide':
            contextmenu.classList.add('hide');
            break;
        case 'show':
            contextmenu.classList.remove('hide');
    }
};

// When the view port is less than 767 pixel, make the input box not editable.
const refreshInputEditable = () => {
    const allInputs = document.querySelectorAll('input');
    if (window.matchMedia("(max-width: 767px)").matches) {
        // The view port is less than 767 pixels wide
        allInputs.forEach(input => {
            input.setAttribute('readonly', true)
        })
    } else {
        // The view port is at least 767 pixels wide 
        allInputs.forEach(input => {
            input.removeAttribute('readonly');
        })
    }
}

// We use this method to toggle switch icon or delete icon in the input group.
// Since we only show the switch icon when there are only start and end point
// input boxes, and for other instance, we hide this switch icon and show the
// delete icon after each input box.
const toggleCloserAndSwitch = () => {
    if (document.querySelectorAll('.point input').length === 2) {
        // There is no via node but only start and end input point. So we have to hide the hide icon of the input and show the switch icon.
        document.querySelectorAll('.closer').forEach((closer) => {
            closer.classList.add('hide');
        });
        document.querySelector('.switch').classList.remove('hide');
    } else {
        // When there are more than or equal to 1 via node, we need to show the closer icon and hide the switch icon.
        document.querySelectorAll('.closer').forEach((closer) => {
            closer.classList.remove('hide');
        });
        document.querySelector('.switch').classList.add('hide');
    }
};

/*---------------------------------------------*/
// 9. Derive the Custom Class Drag
/*---------------------------------------------*/

// Since we need to drag the point to change the destination or start location,
// we have to make the point draggable. At this step, we derived the custom class Drag.
let coordBeforeMove;
app.Drag = function () {
    ol.interaction.Pointer.call(this, {
        handleDownEvent: app.Drag.prototype.handleDownEvent,
        handleDragEvent: app.Drag.prototype.handleDragEvent,
        handleUpEvent: app.Drag.prototype.handleUpEvent
    });
    // Save the coordinates when the cursor click.
    this.coordinate_ = null;
    // Save the feature what cursor click at the beginnig.
    this.feature_ = null;
    this.timeEvent;
    this.flag_ = true;
};
ol.inherits(app.Drag, ol.interaction.Pointer);

// Function handling "down" events.
// If the function returns true then a drag sequence is started.
app.Drag.prototype.handleDownEvent = function (evt) {
    if (evt.dragging) {
        return;
    }
    hideOrShowContextMenu('hide');

    const options = {
        // Only find feature on the routing layer not the base vector tile layer.
        layerFilter: function (layer) {
            if (layer instanceof ol.layer.VectorTile) {
                return false;
            }
            return true;
        }
    };

    var map = evt.map;
    var feature = map.forEachFeatureAtPixel(
        evt.pixel,
        function (feature, layer) {
            clearTimeout(this.timeEvent);
            this.flag_ = true;
            let featureName = feature.get('name');
            if (featureName === 'start' || featureName === 'end' || featureName === 'mid') {
                coordBeforeMove = feature.getGeometry().getCoordinates();
                return feature;
            }
        },
        options
    );

    if (feature) {
        this.coordinate_ = evt.coordinate;
        this.feature_ = feature;
    }

    return !!feature;
};

// Function handling "drag" events.
// This function is called on "move" events during a drag sequence.
app.Drag.prototype.handleDragEvent = function (evt) {
    clearTimeout(this.timeEvent);
    this.timeEvent = 0;

    this.flag_ = true;

    var deltaX = evt.coordinate[0] - this.coordinate_[0];
    var deltaY = evt.coordinate[1] - this.coordinate_[1];

    var geometry = this.feature_.getGeometry();
    geometry.translate(deltaX, deltaY);
    const coord = geometry.getCoordinates();
    const coord_ = coord.slice();
    const featureType = this.feature_.get('name');
    this.coordinate_[0] = evt.coordinate[0];
    this.coordinate_[1] = evt.coordinate[1];
    const coordBeforeMove_ = coordBeforeMove.slice();

    this.timeEvent = setTimeout(function () {
        removeFeatureByName('line');
        hideOrShowResultBox('hide');
        removeFeatureByName('arrow');
        this.flag_ = false;
        // Update the corresponding input node value and data-origin attribute value.
        if (featureType === 'start' || featureType === 'end' || featureType === 'mid') {
            const inputs = document.querySelectorAll('.point input');
            let inputNode;
            Array.from(inputs).some((input) => {
                const inputOrigin = input.getAttribute('data-origin');
                if (inputOrigin === coordBeforeMove_.toString()) {
                    inputNode = input;
                    return true;
                }
            });
            if (inputNode) {
                coordBeforeMove = coord;
                inputNode.setAttribute('data-origin', coord);
                inputNode.value = [coord_[1].toFixed(8), coord_[0].toFixed(8)];
            }
        }
        performRouting();
        this.coordinate_ = null;
        this.feature_ = null;
        return false;
    }, 1000);
};

// Function handling "up" events.
// If the function returns false then the current drag sequence is stopped.
app.Drag.prototype.handleUpEvent = function (e) {
    clearTimeout(this.timeEvent);
    this.timeEvent = 0;
    if (this.flag_) {
        const featureType = this.feature_.get('name');
        const coord = this.feature_.getGeometry().getCoordinates();

        if (coord.toString() === coordBeforeMove.toString()) {
            return;
        }
        const coord_ = new ol.proj.toLonLat(coord);
        const coordBeforeMove_ = coordBeforeMove.slice();

        // Update the corresponding input node value and data-origin attribute value.
        if (featureType === 'start' || featureType === 'end' || featureType === 'mid') {
            const inputs = document.querySelectorAll('.point input');
            let inputNode;
            Array.from(inputs).some((input) => {
                const inputOrigin = input.getAttribute('data-origin');
                if (inputOrigin === coordBeforeMove_.toString()) {
                    inputNode = input;
                    return true;
                }
            });
            if (inputNode) {
                inputNode.setAttribute('data-origin', coord);
                inputNode.value = [coord_[1].toFixed(8), coord_[0].toFixed(8)];
            }
        }
        removeFeatureByName('line');
        removeFeatureByName('arrow');
        hideOrShowResultBox('hide');
        performRouting();
        this.coordinate_ = null;
        this.feature_ = null;
        return false;
    }
};

/*---------------------------------------------*/
// 10. Event Listeners
/*---------------------------------------------*/

// These event listeners tell the UI when it's time to execute all of the
// code we've written.

document.addEventListener('DOMContentLoaded', function () {
    // Hide the context menu of the browsers when right click on the map.
    document.querySelector('#map').oncontextmenu = () => {
        return false;
    };

    // Hide the coustom context-menu when click on the map.
    document.querySelector('#map').onclick = () => {
        hideOrShowContextMenu('hide');
    };

    // Handle the click event when click the item in the customized context menu.
    document.querySelector('#ol-contextmenu').addEventListener('click', (e) => {
        const target = e.target.id;
        switch (target) {
            case 'add-startpoint':
                addPointFeature('start', curCoord);
                hideOrShowContextMenu('hide');
                removeFeatureByName('line');
                removeFeatureByName('arrow');

                // Update the start input value and data-origin value.
                let startInput = document.querySelector('#dragable-list input');
                startInput.setAttribute('data-origin', curCoord);
                let curCoord_ = ol.proj.transform(curCoord, 'EPSG:3857', 'EPSG:4326');
                startInput.value = curCoord_[1].toFixed(8) + ', ' + curCoord_[0].toFixed(8);
                hideOrShowResultBox('hide');
                performRouting();

                break;
            case 'add-endpoint':
                addPointFeature('end', curCoord);
                hideOrShowContextMenu('hide');
                removeFeatureByName('line');
                removeFeatureByName('arrow');

                // Update the end input value and data-origin value.
                let endInput = getLastNodeBySelector('#dragable-list li').querySelector('input');
                endInput.setAttribute('data-origin', curCoord);
                let curEndCoord_ = new ol.proj.toLonLat(curCoord);
                endInput.value = curEndCoord_[1].toFixed(8) + ', ' + curEndCoord_[0].toFixed(8);
                hideOrShowResultBox('hide');
                performRouting();
                break;
            case 'context-add-point':
                addPointFeature('mid', curCoord);
                let readonly = false;
                if(window.matchMedia('(max-width: 767px)').matches){
                    readonly = true;
                }
                addInputBox(curCoord, readonly);
                toggleCloserAndSwitch();
                hideOrShowContextMenu('hide');
                document.querySelector('.switch').classList.add('hide');
                performRouting();
                break;
            case 'clear':
                vectorSource.clear();
                clearInputBox();
                const result = document.querySelector('#result');
                if (window.matchMedia('(max-width: 767px)').matches) {
                    result.style.overflowY = 'hidden';
                    result.classList.remove('transition-height');
                    result.style.height = 0 + 'px';
                }
                hideOrShowContextMenu('hide');
                hideOrShowResultBox('hide');
                performRouting();
        }
    });

    // When the pointer is moving over the item in result box, then add
    // a colored circle to the target location.
    document.querySelector('#map').addEventListener('mouseover', (e) => {
        let target = e.target;
        let boxDom;
        if (target.nodeName === 'SPAN' && target.parentNode.classList.contains('box')) {
            boxDom = target.parentNode;
        } else if (target.classList.contains('box')) {
            boxDom = target;
        }
        if (boxDom !== undefined) {
            let attrCoord = boxDom.getAttribute('coord');
            attrCoord = attrCoord.split(' ');
            let coord = [Number(attrCoord[0]), Number(attrCoord[1])];
            addResultRadius(coord);
        } else {
            removeFeatureByName('resultRadius');
        }
    });

    // When click the item in the result box, zoom in to where you click
    // and show the turn arrow or turn arrow line.
    document.querySelector('#result').addEventListener('click', (e) => {
        let target = e.target;
        let boxDom;
        const nodeList = document.querySelectorAll('.box');
        nodeList.forEach((node) => {
            if (node.classList.contains('selectBox')) {
                node.classList.remove('selectBox');
            }
        });

        if (target.nodeName === 'SPAN' && target.parentNode.classList.contains('box')) {
            target.parentNode.classList.add('selectBox');
            boxDom = target.parentNode;
        } else if (target.classList.contains('box')) {
            target.classList.add('selectBox');
            boxDom = target;
        }

        if (boxDom !== undefined) {
            removeFeatureByName('resultRadius');
            let penult = boxDom.getAttribute('lastlinepenultcoord');
            if (penult) {
                penult = penult.split(' ');
                let penultCoord = [Number(penult[0]), Number(penult[1])];

                let last = boxDom.getAttribute('lastLineLastCoord');
                last = last.split(' ');
                let lastCoord = [Number(last[0]), Number(last[1])];

                addArrow(penultCoord, lastCoord);
            }
            let attrCoord = boxDom.getAttribute('coord');
            attrCoord = attrCoord.split(' ');
            let coord = [Number(attrCoord[0]), Number(attrCoord[1])];
            view.fit(new ol.geom.Point(coord), {
                padding: [20, 20, 20, 20],
                duration: 1000,
                maxZoom: 17,
                callback: function () {
                    let penult = boxDom.getAttribute('lastlinepenultcoord');
                    if (penult) {
                        penult = penult.split(' ');
                        let penultCoord = [Number(penult[0]), Number(penult[1])];

                        let last = boxDom.getAttribute('lastLineLastCoord');
                        last = last.split(' ');
                        let lastCoord = [Number(last[0]), Number(last[1])];

                        var lineSecondCoord = boxDom.getAttribute('lineSecondCoord');
                        if (lineSecondCoord) {
                            var stringCoords = lineSecondCoord.split(' ');
                            lineSecondCoord = [+stringCoords[0], +stringCoords[1]].slice();
                            var prevCoord = lerp(lastCoord, penultCoord);
                            var secondCoord = lerp(lastCoord, lineSecondCoord);
                            addArrow(lastCoord, secondCoord);
                            addTurnLine(prevCoord, lastCoord, secondCoord);
                        } else {
                            addArrow(penultCoord, lastCoord);
                        }
                    }
                }
            });
        }
    });

    // When there are only two points in the input group, we could switch
    // the points by clicking the switch icon, which means we could switch
    // the starting point and destination point.
    document.querySelector('.switch').addEventListener('click', () => {
        let startInput = document.querySelector('#dragable-list input');
        let endInput = getLastNodeBySelector('#dragable-list li').querySelector('input');

        // Switch input value
        let t = startInput.value;
        startInput.value = endInput.value;
        endInput.value = t;

        // Switch input data-origin value
        let tOrigin = startInput.getAttribute('data-origin');
        startInput.setAttribute('data-origin', endInput.getAttribute('data-origin'));
        endInput.setAttribute('data-origin', tOrigin);

        // Get coord from input data-origin.
        let startOrigin = startInput.getAttribute('data-origin');
        let endOrigin = endInput.getAttribute('data-origin');
        let startOrigin_ = getCoordFromDataOrigin(startOrigin);
        let endOrigin_ = getCoordFromDataOrigin(endOrigin);

        if (startOrigin_.length > 0 && endOrigin_.length > 0) {
            vectorSource.clear();
            hideOrShowResultBox('hide');
            addPointFeature('start', startOrigin_);
            addPointFeature('end', endOrigin_);
            performRouting();
        }
    });

    // Hide the error modals when clicking the "OK" button.
    document.querySelector('#error-modal button').addEventListener('click', () => {
        document.querySelector('#error-modal').classList.add('hide');
    });

    // Add an input box once clicked the "Add destination" button
    document.querySelector('#add-point').addEventListener('click', function () {
        const feature = getFeatureByName('end');
        const coord = feature.getGeometry().getCoordinates();
        vectorSource.removeFeature(feature);
        addPointFeature('mid', coord);
        addInputBox();
    });

    // Delete the input box when clicking the deleting icon on the right of the input box.
    document.querySelector('.point').addEventListener('click', function (e) {
        e = window.event || e;
        const target = e.target;
        const classlist = target.classList;
        if (target === document.querySelector('.closer')) {
            // Delete the target input box(start point input).
            // Move the value and data-origin from second node to first node, and delete the second node.
            const first = target.parentNode;
            const second = document.querySelectorAll('.via')[0];
            const secondOrigin = second.querySelector('input').getAttribute('data-origin');
            first.querySelector('input').value = second.querySelector('input').value;
            first.querySelector('input').setAttribute('data-origin', secondOrigin);
            second.remove();
            removeFeatureByName('start');
        } else if (target === getLastNodeBySelector('.closer')) {
            // Delete the target input box(end point input).
            // Move the value and data-origin from penult node to last node, and delete the penult node.
            const lastInput = target.parentNode.querySelector('input');
            const allVias = document.querySelectorAll('.via');
            const penult = allVias[allVias.length - 1];
            const penultOrigin = penult.querySelector('input').getAttribute('data-origin');
            lastInput.value = penult.querySelector('input').value;
            lastInput.setAttribute('data-origin', penult.querySelector('input').getAttribute('data-origin'));
            penult.remove();
            removeFeatureByName('end');
        } else if (classlist.contains('closer')) {
            // Delete the target input box(input in the middle).
            const parentNode = target.parentNode;
            let coord = parentNode.querySelector('input').getAttribute('data-origin');
            removeFeatureByCoord(coord);
            parentNode.remove();
        }

        if (classlist.contains('closer')) {
            toggleCloserAndSwitch();
            findRoute(false);
        } else if (target.id === 'add-point') {
            toggleCloserAndSwitch();
            findRoute(false, notClearAll = true);
        }
    });

    // Update the input value to input attribute of "data-origin", which stores
    // the most accurate coordinates of the point.
    const updateDataOriginByInput = (inputNode, inputValue) => {
        if (inputValue) {
            let valueArr = inputValue.split(',');
            if (valueArr.length === 2) {
                let valueArr_ = [Number(valueArr[1]), Number(valueArr[0])]; // '12,13' => [13,12]
                inputNode.setAttribute('data-origin', new ol.proj.fromLonLat(valueArr_));
            } else {
                inputNode.setAttribute('data-origin', '');
            }
        } else {
            inputNode.setAttribute('data-origin', '');
        }
    };
    document.querySelector('.point').addEventListener('input', function (e) {
        e = window.event || e;
        const target = e.target;
        updateDataOriginByInput(target, target.value);
    });

    // When press enter, perform the routing request.
    document.querySelector('.point').addEventListener('keyup', function (e) {
        e = window.e || e;
        if (e.keyCode === 13) {
            const showError = true;
            findRoute(showError);
        }
    });

    // When click "go" button in the sidebar, performing the routing request.
    document.querySelector('#go').addEventListener('click', function () {
        const showError = true;
        findRoute(showError);
    });

    // In order to reorder the input group, we used a plugin to handle it.
    // When dragging end, we need to judge if we should perform the routing request.
    const handleDragEnd = () => {
        const inputs = document.querySelectorAll('#dragable-list input');
        const length = inputs.length;
        inputs.forEach((input, index) => {
            if (index === 0) {
                input.setAttribute('placeholder', 'Start');
            } else if (index === length - 1) {
                input.setAttribute('placeholder', 'Destination');
            } else {
                input.setAttribute('placeholder', 'To');
            }
        });

        const showError = false;
        findRoute(showError);
    };

    // Create the draggable instance.
    Sortable.create(document.getElementById('dragable-list'), {
        handle: '.drag',
        onEnd: handleDragEnd,
        animation: 150,
        ghostClass: 'dragging'
    });

    refreshInputEditable();
    let resizeTimer;
    window.addEventListener('resize', function () {
        if(resizeTimer){
            this.clearTimeout(resizeTimer);
        }
        resizeTimer = setTimeout(() => {
            refreshInputEditable();
        }, 500);
    })
});
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console