Существует ошибка javascript с опубликованным кодом: Uncaught TypeError: event.feature.getGeometry(...).get is not a function
в строке:
infowindow.setPosition(event.feature.getGeometry().get()); // anchor the infowindow at the marker`
Геометрия Data.Polygon не имеет .get()
метод. Он имеет метод .getArray()
(который возвращает массив из LineStrings )
. Одно место для размещения InfoWindow
at - это точка, нажатая (находящаяся в многоугольнике) :
infowindow.setPosition(event.latLng);
(если вы хотите либо добавить фиксированную точку для infowindow в GeoJson, либо вы хотите вычислить неподвижную точку из многоугольника, вы тоже можете это сделать)
доказательство концепции fiddle
фрагмент кода:
function initialize() {
var map = new google.maps.Map(
document.getElementById("map_canvas"), {
zoom: 4,
center: {
lat: -28,
lng: 137
},
mapTypeId: google.maps.MapTypeId.ROADMAP
});
map.data.loadGeoJson('https://storage.googleapis.com/mapsdevsite/json/google.json');
map.data.setStyle({
fillColor: 'red',
strokeWeight: 1
});
var infowindow = new google.maps.InfoWindow({
content: "hello"
});
map.data.addListener('click', function(event) {
let id = event.feature.getProperty('ID');
let name = event.feature.getProperty('HORZ_ORG');
if (typeof id == "undefined") id = event.feature.getProperty('letter');
if (typeof name == "undefined") name = event.feature.getProperty('color');
let html = id + " " + name;
infowindow.setContent(html); // show the html variable in the infowindow
infowindow.setPosition(event.latLng);
infowindow.setOptions({
pixelOffset: new google.maps.Size(0, 0)
}); // move the infowindow up slightly to the top of the marker icon
infowindow.open(map);
});
}
google.maps.event.addDomListener(window, "load", initialize);
html,
body,
#map_canvas {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px
}
Как вы можете видеть из документации API, once()
выполняет запрос асинхронно и возвращает Promise, который можно использовать для получения DataSnapshot в месте запроса в in база данных. Однако вы предполагаете, что он просто возвращает значение напрямую. Вместо этого вам нужно будет использовать это обещание, чтобы получить идентификатор пользователя. Ваша функция должна выглядеть примерно так:
exports.notificationMake =
functions.database.ref('/dhabba_orders/{userId}/status').onWrite((change, context) => {
admin.database().ref(`/dhabba_orders/{userID}`).once('value')
.then(snapshot => {
const userId = snapshot.data();
console.log(userId);
const payload = {
notification: {
title: `Hi`,
body: `Hey`
}
};
return admin.messaging().sendToDevice(userId, payload);
});
});
ОБНОВЛЕНИЕ: РЕШЕНИЕ
exports.notificationMake =
functions.database.ref('/dhabba_orders/{user_uid}').onWrite((change, context) => {
let userId = context.params.user_uid;
let orderStatus = context.params.order_status;
Promise.all([userId, orderStatus]).then(others => {
console.log(userId);
console.log(orderStatus);
const payload = {
notification: {
title: `Order Accepted!`
}
};
admin.messaging().sendToDevice(userId, payload);
return 0;
}).catch(error => {
console.log(error);
});
return 0;
});