gpt4 book ai didi

angular - Google Map 上带有 ionic 3 的多个标记

转载 作者:行者123 更新时间:2023-12-02 07:04:34 25 4
gpt4 key购买 nike

我正在尝试在简单的谷歌地图上获取多个标记。我正在使用使用 Angular 4 的 Ionic 3。

我正在这样加载 map :

import { Component, ViewChild, ElementRef } from '@angular/core';
import { GoogleMaps } from '@ionic-native/google-maps';

declare var google;

export class SearchResultsPage extends BasePage {

@ViewChild('map') mapElement: ElementRef;
private map: any;

constructor(public navCtrl: NavController,
public navParams: NavParams,
private translateService: TranslateService,
private googleMaps: GoogleMaps) {}


ionViewDidLoad() {
setTimeout(()=>{
this.loadMap();
}, 1000)
}

loadMap() {
let latLng = new google.maps.LatLng(48.8509695, 2.3861870000000636);
let latLng2 = new google.maps.LatLng(48.8504541, 2.3865487000000485);

let mapOptions = {
center: latLng,
zoom: 15,
mapTypeId: google.maps.MapTypeId.ROADMAP
}

this.map = new google.maps.Map(this.mapElement.nativeElement, mapOptions);

var marker = new google.maps.Marker({
position: latLng,
map: this.map,
title: 'Hello World!'
},{
position: latLng2,
map: this.map,
title: 'Check the World!'
});
}

我找到了这个(但不起作用)

addMarkers(data, callback) {
var markers = [{
'position': {
lat: 48.8513735,
lng: 2.3861292
},
'icon': '#ff0000'
}];
function onMarkerAdded(marker) {
console.log(marker);
markers.push(marker);

if (markers.length === data.length) {
callback(markers);
} else {
console.log('in the else');
}
}

data.forEach(function(markerOptions) {
console.log('in foreach');
this.map.addMarker(markerOptions, onMarkerAdded);
});
}

map 显示出来了,但是我没有成功添加标记。
我试图按照官方文档(v1 和 v2)进行操作,但它不起作用。如果有人有想法,请提前致谢。我看到很多人都在尝试做同样的事情,但是每个代码都不一样..

最佳答案

loadMap() {
let latLng = new google.maps.LatLng(48.8513735, 2.3861292);

let mapOptions = {
center: latLng,
zoom: 15,
mapTypeId: google.maps.MapTypeId.ROADMAP
}

this.map = new google.maps.Map(this.mapElement.nativeElement, mapOptions);

var locations = [
['Bondi Beach', -33.890542, 151.274856, 4],
['Coogee Beach', -33.923036, 151.259052, 5],
['Cronulla Beach', -34.028249, 151.157507, 3],
['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
['Maroubra Beach', -33.950198, 151.259302, 1]
];

var infowindow = new google.maps.InfoWindow();

var marker, i;

for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
map: map
});

google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent(locations[i][0]);
infowindow.open(map, marker);
}
})(marker, i));
}
}

关于angular - Google Map 上带有 ionic 3 的多个标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45354597/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com