【问题标题】:Create multiple stops directions google maps Vue.js创建多个站点方向谷歌地图Vue.js
【发布时间】:2021-11-26 07:54:37
【问题描述】:

我编写了这部分代码来在谷歌地图上显示方向 但目前我只有开始和结束位置 我希望有更多的停靠点,并让路线尽可能好 如何输入多个站点? 这就是我到现在为止所做的

<table>
        <tr>
          <th>Start</th>
          <th><GmapAutocomplete @place_changed="setPlace" /></th>
          <th style="width: 50%;"><button class="btn" @click="addMarker(0)">Add</button></th>
        </tr>
        <tr>
          <th>End</th>
          <th><GmapAutocomplete @place_changed="setPlace" /></th>
          <th style="width: 50%;"><button class="btn" @click="addMarker(1)">Add</button></th>
        </tr>
      </table>

路线组件

import { MapElementFactory } from "vue2-google-maps";
export default MapElementFactory({
  name: "directionsRenderer",
  ctr() {
    return window.google.maps.DirectionsRenderer;
  },
  events: [],
  mappedProps: {},
  props: {
    origin: { type: [Object, Array] },
    destination: { type: [Object, Array] },
    travelMode: { type: String },
  },
  afterCreate(directionsRenderer) {
    console.log(1)
    let directionsService = new window.google.maps.DirectionsService();
    this.$watch(
      () => [this.origin, this.destination, this.travelMode],
      () => {
        let { origin, destination, travelMode } = this;
        if (!origin || !destination || !travelMode) return;
        directionsService.route(
          {
            origin,
            destination,
            travelMode,
          },
          (response, status) => {
            if (status !== "OK") return;
            // eslint-disable-next-line no-debugger
            //debugger
            directionsRenderer.setDirections(response);
          }
        );
      }
    );
  },
});
<DirectionsRenderer
        travelMode="DRIVING"
        :origin="startLocation"
        :destination="endLocation"
      />

还有我的功能

setPlace(place) {
      this.currentPlace = place;
    },
    addMarker(index) {
      const marker = {
        lat: this.currentPlace.geometry.location.lat(),
        lng: this.currentPlace.geometry.location.lng(),
      };
      if (index === 0) this.startLocation = marker;
      if (index === 1) this.endLocation = marker;
      this.center = marker;
      console.log(this.startLocation, this.endLocation)
    },

所以到目前为止我一切都还好,一切都非常顺利,但我需要更多的停留 从你所看到的,我最后只有一个变量 我该如何进行? 我能适应现在的代码吗?

【问题讨论】:

    标签: vue.js google-maps


    【解决方案1】:

    我以前遇到过这个问题,这将对您有所帮助:

    首先,您需要在 DirectionsRenderer.js 文件中将 waypointsoptimizeWaypoints 键添加到 directionsService.route 之后,您需要将此键绑定到项目中的DirectionsRenderer 组件,并且您需要在组件文件中创建一个名为 waypnt 的数组,并将所有Destination 点和stopover: true 键按如下方式:

    this.waypnt.push({
          location: { lat: marker.lat, lng: marker.lng },
          stopover: true,
        });
    

    看看这个:

    DirectionsRenderer.js

        import { MapElementFactory } from "vue2-google-maps";
    
    export default MapElementFactory({
      name: "directionsRenderer",
    
      ctr() {
        return window.google.maps.DirectionsRenderer;
      },
    
      events: [],
    
      mappedProps: {},
    
      props: {
        origin: { type: [Object, Array] },
        destination: { type: [Object, Array] },
        waypoints: {type: Array},
        travelMode: { type: String },
        optimizeWaypoints: {type: Boolean}
      },
    
      afterCreate(directionsRenderer) {
        let directionsService = new window.google.maps.DirectionsService();
    
        this.$watch(
          () => [this.origin, this.destination, this.travelMode, this.waypoints, this.optimizeWaypoints],
          () => {
            let { origin, destination, travelMode, waypoints, optimizeWaypoints } = this;
            if (!origin || !destination || !travelMode || !waypoints) return;
            directionsService.route(
              {
                origin,
                destination,
                travelMode,
                waypoints,
                optimizeWaypoints,
              },
              (response, status) => {
                if (status !== "OK") return;
                directionsRenderer.setDirections(response);
              }
            );
          }
        );
      },
    });
    

    MapContainer.vue

        <template>
      <div class="hello">
        <h1 class="mb-4">Map Project</h1>
        <div class="row">
          <div class="col-md-9">
            <!-- Adding Company Location: start -->
            <div class="company-location">
              <b-form>
                <b-input-group class="mb-3">
                  <GmapAutocomplete @place_changed="originPoint" />
                  <b-input-group-append class="ms-2">
                    <b-button class="btn-success" @click="addOriginPoint">
                      Add Location
                    </b-button>
                  </b-input-group-append>
                </b-input-group>
              </b-form>
            </div>
            <!-- Adding Company Location: end -->
    
            <!-- Map Container: start -->
            <GmapMap :center="center" :zoom="12" map-type-id="terrain" class="map">
              <GmapMarker
                :key="index"
                v-for="(m, index) in markers"
                :position="m.position"
                @click="center = m.position"
              />
              <DirectionsRenderer
                travelMode="DRIVING"
                :origin="startLocation"
                :destination="endLocation"
                :waypoints="waypnt"
                :optimizeWaypoints="true"
              />
            </GmapMap>
            <!-- Map Container: end -->
          </div>
          <div class="col-md-3">
            <!-- Add passenger' Name & Loacation: start -->
            <b-form>
              <b-form-group
                label="passenger's Name"
                label-for="passengerName"
                class="my-3 text-start"
              >
                <b-form-input
                  id="passengerName"
                  type="text"
                  placeholder="passenger's name"
                  value=""
                  required
                ></b-form-input>
              </b-form-group>
              <b-form-group
                label="passenger's Location"
                label-for="passengerLocation"
                class="my-3 text-start"
              >
                <GmapAutocomplete
                  id="passengerLocation"
                  @place_changed="setPlace"
                />
              </b-form-group>
              <b-button class="btn btn-success w-100" @click="addDestinationPoint">
                Get Direction
              </b-button>
            </b-form>
            <!-- Add passengers's Name & Loacation: end -->
          </div>
        </div>
        <div class="row">
          <div class="col-md-8">
            <!-- data table: start -->
            <div class="mt-4">
              <h3>Employees table</h3>
              <table class="table">
                <thead>
                  <tr>
                    <th>Employee name</th>
                    <th>Trip duration</th>
                    <th>Pickup point order</th>
                  </tr>
                </thead>
                <tbody>
                  <dataTable />
                </tbody>
              </table>
            </div>
            <!-- data table: end -->
          </div>
        </div>
      </div>
    </template>
    
    <script>
    import DirectionsRenderer from "./DirectionsRenderer";
    
    export default {
      name: "MapContainer",
      data() {
        return {
          center: { lat: 41.04538761823768, lng: 28.990736439754617 },
          currentPlace: null,
          originPlace: null,
          markers: [],
          destinationPlaces: [],
          originPlaceLocation: [],
          path: [],
          passengers: [],
          startLocation: null,
          endLocation: null,
          waypnt: [],
        };
      },
      methods: {
        setPlace(place) {
          this.currentPlace = place;
        },
        originPoint(originPoint) {
          this.originPlace = originPoint;
        },
        addDestinationPoint() {
          const passengerName = document.getElementById("passengerName").value;
          if (this.currentPlace || this.destinationPlaces.length <= 7) {
            const marker = {
              lat: this.currentPlace.geometry.location.lat(),
              lng: this.currentPlace.geometry.location.lng(),
            };
            const passengerInfo = {
              name: passengerName,
              pickUpPoint: marker,
            };
            this.markers.push({ position: marker });
            this.path.push({ lat: marker.lat, lng: marker.lng });
            this.destinationPlaces.push(this.currentPlace);
            this.passengers.push({ passengerInfo });
            console.log(this.passengers);
            this.center = marker;
            this.currentPlace = null;
            this.endLocation = marker;
            this.waypnt.push({
              location: { lat: marker.lat, lng: marker.lng },
              stopover: true,
            });
          } else {
            alert("You are allowed to add 8 passenger only!");
          }
        },
        addOriginPoint() {
          if (this.originPlace && this.originPlaceLocation.length == 0) {
            const originMarker = {
              lat: this.originPlace.geometry.location.lat(),
              lng: this.originPlace.geometry.location.lng(),
            };
            this.markers.push({ position: originMarker });
            this.path.push({ lat: originMarker.lat, lng: originMarker.lng });
            this.originPlaceLocation.push({ position: originMarker });
            this.center = originMarker;
            this.startLocation = originMarker;
          } else {
            alert(
              "You have alrady added an origin place! if you want to change it please reload the window"
            );
          }
        },
      },
      components: {
        DirectionsRenderer,
      },
    };
    </script>
    
    <!-- Add "scoped" attribute to limit CSS to this component only -->
    <style scoped>
    h3 {
      margin: 40px 0 0;
    }
    ul {
      list-style-type: none;
      padding: 0;
    }
    li {
      display: inline-block;
      margin: 0 10px;
    }
    a {
      color: #42b983;
    }
    .map {
      width: 100%;
      height: 400px;
    }
    </style>
    

    有关更多信息,请查看以下内容: https://developers.google.com/maps/documentation/javascript/examples/directions-waypoints#maps_directions_waypoints-javascript

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-27
      • 1970-01-01
      • 2011-04-02
      • 2017-09-18
      • 2016-01-04
      相关资源
      最近更新 更多