简体   繁体   English

如何使用Google Maps API在node.js中单击按钮获得用户输入?

[英]How to get user input on click of button in node.js using google maps api?

I have a map that places a marker on the user's current location using the Google Maps Geolocation api. 我有一张地图,使用Google Maps Geolocation api在用户的当前位置上放置了一个标记。 But I also want the user to be able to search for a name and for the app to return another marker (in addition to the marker at the current location) at the first result of closest locations that fit the user input. 但我也希望用户能够搜索名称,并使应用程序在适合用户输入的最接近位置的第一个结果处返回另一个标记(除当前位置的标记外)。 (The google maps api returns an array of closest locations). (Google Maps api返回最近位置的数组)。

Here is the HTML for the search bar and the submit button: 这是搜索栏和提交按钮的HTML:

<form>
<p> Enter item here:
<input name = 'location1' id = "location2"/>
</p>
<button type = 'submit' id = "submitButton" ><center> Submit</center>
</button>
</form>

And here are parts of the javascript (in ejs) that contain the function for the map and the function (getStoreLocation) that is supposed to get the user input once the submit button has been clicked: 这是javascript的一部分(在ejs中),其中包含地图的功能和应该在单击提交按钮后获取用户输入的功能(getStoreLocation):

if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(function(position) {
              pos = {
                lat: position.coords.latitude,
                lng: position.coords.longitude
              };
              var marker = new google.maps.Marker({
                position: pos,
                map: map
              });
              map.setCenter(pos);
              document.getElementById('submitButton').onclick = function() {getStoreLocation()}
}, function() {
              handleLocationError(true, infoWindow, map.getCenter());
            });

          } else {
            // Browser doesn’t support Geolocation
            handleLocationError(false, infoWindow, map.getCenter());
          }


        }
function getStoreLocation(){
              var input = document.getElementById('location2').value
              console.log(input)
              var apiKey = 'AIzaSyDUWTdknyhqy6iOYA4y8zbR6FJN_tamIaA'
              var url = `https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=${pos.lat},${pos.lng}&radius=500&keyword=${input}&key=${apiKey}`              

              fetch(url)
              .then(function(data){
                return data.json()
              })
              .then(function(json){
                console.log(json)
                var latitudeOfInput = json.results[0].geometry.location.lat
                var longitudeOfInput = json.results[0].geometry.location.lng
                var pos2 = {
                  lat: latitudeOfInput,
                  lng: longitudeOfInput
                };

                console.log(pos2)

                var marker2 = new google.maps.Marker({
                  position: pos2,
                  map: map
                });
              })
  }

But even after I input a name and press the Submit Button, the page returns the map with only one marker at the current location, and does not return a second marker. 但是即使输入了名称并按了Submit按钮,页面也会返回仅在当前位置带有一个标记的地图,而不会返回第二个标记。 Does anyone know how to fix this or what I'm doing wrong? 有人知道如何解决此问题或我做错了什么吗?

The button is causing the page to refresh change the type of the button to be. 该按钮导致页面刷新,将按钮的类型更改为。

<button type="button">

If that doesnt work then change the button element to. 如果那不起作用,则将button元素更改为。

<input type="button">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Express 和 Node.js 的谷歌地图 API - Google Maps API using Express and Node.js 如何使用 Node.js 通过单击按钮获取下一行数据库结果? - How do I get the next row of database results from a button click using Node.js? 如何使JavaScript控制台用户输入工作(node.js)? - How to get JavaScript console user input working (node.js)? 您如何从 Node.js 获取用户输入? - How do you get user input from Node.js? 如何获取Google网站管理员api(Node.js)的访问令牌 - How to get access token for google webmasters api (Node.js) Google Maps Api标记数组node.js - Google Maps Api markers array node.js 如何使用Node.js从Google Calendar API获取当前年份的所有假期? - How to get all the current year holidays from google calendar API using Node.js? 使用Node.js中的Google Maps制作地图 - Making a map using google maps in node.js 如何在不超过用户速率限制的情况下使用node.js向Google Drive API发出3千个请求? - How can I make 3 thousand requests to Google Drive API using node.js without exceeding User Rate Limit? 用户单击按钮时如何在 node.js 和哈巴狗中删除上一个问题时显示新问题? - How to display new question when user click button and previous question delete in node.js and pug?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM