gpt4 book ai didi

javascript - Meteor 实现 Google map 和 Google Places

转载 作者:行者123 更新时间:2023-11-29 19:16:07 26 4
gpt4 key购买 nike

我正在尝试在我的 Meteor 应用程序上实现一个 Google map ,它将获取用户的位置,然后找到用户附近提供食物的地方。我开始实现 example

由谷歌提供,我这样做时效果很好;但是我试图通过将它添加到实际的 Javascript 文件中来正确实现它,但它现在给我一个“Google 未定义”错误。

menuList = new Mongo.Collection('items');
if (Meteor.isClient) {

var pls;
var map;
var infowindow;
Meteor.startup(function () {
//get user location and return location in console
var options = {
enableHighAccuracy: true,
timeout: 5000,
maximumAge: 0
};

function success(pos) {
var crd = pos.coords;

console.log('Your current position is:');
console.log('Latitude : ' + crd.latitude);
console.log('Longitude: ' + crd.longitude);
console.log('More or less ' + crd.accuracy + ' meters.');
pls = {lat: crd.latitude, lng: crd.longitude};
};

function error(err) {
console.warn('ERROR(' + err.code + '): ' + err.message);
};

navigator.geolocation.getCurrentPosition(success, error, options);
})

Meteor.methods({
callback: function (results, status) {
if (status === google.maps.places.PlacesServiceStatus.OK) {
for (var i = 0; i < results.length; i++) {
createMarker(results[i]);
}
}
},

createMarker: function (place) {
var placeLoc = place.geometry.location;
var marker = new google.maps.Marker({
map: map,
position: place.geometry.location
});

google.maps.event.addListener(marker, 'click', function () {
infowindow.setContent(place.name);
infowindow.open(map, this);
});
}


})

Template.searchIt.helpers({
'initMap': function () {
console.log("HERE");
//Dummy values I placed for StackOverflow
var pyrmont = {lat: -33.234, lng: 95.343};

map = new google.maps.Map(document.getElementById('map'), {
center: pyrmont,
zoom: 15
});

infowindow = new google.maps.InfoWindow();
var service = new google.maps.places.PlacesService(map);
service.nearbySearch({
location: pyrmont,
radius: 500,
types: ['food']
}, callback);
}


})
}
<head>
<title>Place searches</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<div id="map"></div>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyACgaDFJrh2pMm-bSta1S40wpKDDSpXO2M
&signed_in=true&libraries=places" async defer></script>
<style>
html, body {
height: 100%;
margin: 0;
padding: 0;
}
#map {
height: 100%;
}
</style>

</head>
<body>
{{>searchIt}}

</body>


<template name="searchIt">
{{initMap}}
</template>

最佳答案

您应该尝试 dburles:google-maps 包。

这是其作者编写的示例:http://meteorcapture.com/how-to-create-a-reactive-google-map/

玩得开心!

关于javascript - Meteor 实现 Google map 和 Google Places,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35246452/

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