为什么Google Map的多个标记在Ionic 6应用程序中未显示在Android模拟器中?

发布于 2025-02-10 20:09:54 字数 3844 浏览 1 评论 0原文

我是离子的新手。目前,我正在研究Google Map功能。我能够在源和目标之间绘制路线并显示多个标记。它在浏览器中工作正常,但在Android模拟器中不正常。我的目的是开发一个像“ Uber”这样的应用程序。请帮助我。我被困在这里。在我的代码下面给出了。请帮助审查它,并尝试帮助解决此问题。

    getCurrentPosition() {
    navigator.geolocation.getCurrentPosition((pos) => {

      this.updateLocation.longitude = String(pos.coords.longitude);
      this.updateLocation.phoneNumber = this.phoneNoValue.value;
      this.updateLocation.userType = UserType.Customer;
      this.updateLocationService
        .updateLocation(this.updateLocation)
        .subscribe(async (response) => {
          if (response != null || response != undefined) {
            this.driverList = response.driverList;
            this.addMarkersToMap(this.driverList);
            const toast = await this.toastController.create({
              message: 'Location Updated',
              duration: 2000,
              position: 'bottom',
              color: 'tertiary',
            });
            await toast.present();
          }
        });
    });
  }

 addMarkersToMap(driverList) {
    const centerPosition = { lat: 23.8042, lng: 90.3667 };
    this.map = new google.maps.Map(document.getElementById('map'), {
      zoom: 10,
      center: centerPosition,
    });

   driverList.forEach(element => {
        var myLatLng = new google.maps.LatLng(element.latitude, element.longitude);
        const marker = new google.maps.Marker({
          position: myLatLng,
          map:this.map,
          latitude:element.latitude,
          longitude:element.longitude
        });
        marker.setMap(this.map);
        this.cdr.detectChanges()
      });

  }

 async calculateAndDisplayRoute() {
    this.directionsService
      .route(
        {
          origin: {
            query: this.sourceLocation,
          },
          destination: {
            query: this.destinationLocation.input,
          },
          travelMode: google.maps.TravelMode.DRIVING,
        },
        (response, status) => {
          console.log('google', response)
          this.googleMapResponse = response;
          if (status == "OK") {
            this.directionsRenderer.setMap(this.map); 
            this.directionsRenderer.setDirections(response);
            this.driver.userLatitude = String(response.routes[0].legs[0].start_location.lat());
            this.driver.userLongitude = String(response.routes[0].legs[0].start_location.lng());
            this.driver.phoneNumber = this.phoneNoValue.value;
            this.driver.destinationLatitude = String(response.routes[0].legs[0].end_location.lat());
            this.driver.destinationLongitude = String(response.routes[0].legs[0].end_location.lng());
            this.driver.pickupLocation = response.routes[0].legs[0].start_address;
            this.rideRequestService
              .getDriverList(this.driver)
              .subscribe(async (res) => {
                if (res != null || res != undefined) {
                  this.driverLocaiton.phoneNumber = res.phoneNumber;
                  this.driverLocaiton.driverList = res.driverList;
                  this.driverLocaiton.rideFareModelList = res.rideFareModelList;
                  
                  const toast = await this.toastController.create({
                    message: 'Location Updated',
                    duration: 2000,
                    position: 'bottom',
                    color: 'tertiary',
                  });
                  await toast.present();
                  this.isExpand = false;
                }
                await Storage.set({key: 'google',  value:`${JSON.stringify(response)}`});
              });
              
          } else {
            window.alert("Direction req failed due to" + status);
          }
        })
    this.isExpand = false;
  }

上面有我的代码。请帮助修复上述代码,以显示Android模拟器中Google Map的多个标记

I am new in ionic. Currently I am working on google map feature. I am able to draw route between source and destination and show multiple markers. It is working fine in browser but not in android emulator. My intention is to develop an app like "UBER". Please help me out. I am stuck here. Below my codes are given. Please help to review it and try to help to fix this problem.

    getCurrentPosition() {
    navigator.geolocation.getCurrentPosition((pos) => {

      this.updateLocation.longitude = String(pos.coords.longitude);
      this.updateLocation.phoneNumber = this.phoneNoValue.value;
      this.updateLocation.userType = UserType.Customer;
      this.updateLocationService
        .updateLocation(this.updateLocation)
        .subscribe(async (response) => {
          if (response != null || response != undefined) {
            this.driverList = response.driverList;
            this.addMarkersToMap(this.driverList);
            const toast = await this.toastController.create({
              message: 'Location Updated',
              duration: 2000,
              position: 'bottom',
              color: 'tertiary',
            });
            await toast.present();
          }
        });
    });
  }

 addMarkersToMap(driverList) {
    const centerPosition = { lat: 23.8042, lng: 90.3667 };
    this.map = new google.maps.Map(document.getElementById('map'), {
      zoom: 10,
      center: centerPosition,
    });

   driverList.forEach(element => {
        var myLatLng = new google.maps.LatLng(element.latitude, element.longitude);
        const marker = new google.maps.Marker({
          position: myLatLng,
          map:this.map,
          latitude:element.latitude,
          longitude:element.longitude
        });
        marker.setMap(this.map);
        this.cdr.detectChanges()
      });

  }

 async calculateAndDisplayRoute() {
    this.directionsService
      .route(
        {
          origin: {
            query: this.sourceLocation,
          },
          destination: {
            query: this.destinationLocation.input,
          },
          travelMode: google.maps.TravelMode.DRIVING,
        },
        (response, status) => {
          console.log('google', response)
          this.googleMapResponse = response;
          if (status == "OK") {
            this.directionsRenderer.setMap(this.map); 
            this.directionsRenderer.setDirections(response);
            this.driver.userLatitude = String(response.routes[0].legs[0].start_location.lat());
            this.driver.userLongitude = String(response.routes[0].legs[0].start_location.lng());
            this.driver.phoneNumber = this.phoneNoValue.value;
            this.driver.destinationLatitude = String(response.routes[0].legs[0].end_location.lat());
            this.driver.destinationLongitude = String(response.routes[0].legs[0].end_location.lng());
            this.driver.pickupLocation = response.routes[0].legs[0].start_address;
            this.rideRequestService
              .getDriverList(this.driver)
              .subscribe(async (res) => {
                if (res != null || res != undefined) {
                  this.driverLocaiton.phoneNumber = res.phoneNumber;
                  this.driverLocaiton.driverList = res.driverList;
                  this.driverLocaiton.rideFareModelList = res.rideFareModelList;
                  
                  const toast = await this.toastController.create({
                    message: 'Location Updated',
                    duration: 2000,
                    position: 'bottom',
                    color: 'tertiary',
                  });
                  await toast.present();
                  this.isExpand = false;
                }
                await Storage.set({key: 'google',  value:`${JSON.stringify(response)}`});
              });
              
          } else {
            window.alert("Direction req failed due to" + status);
          }
        })
    this.isExpand = false;
  }

Above has my code. Please help to fix the above code for showing multiple marker of google map in android emulator

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文