无法删除功能的地图部分 [英] Can't remove map part of function

查看:92
本文介绍了无法删除功能的地图部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码从Google的例子中大量借鉴。我使用的全部是此代码的Google Places结果部分,因此我想将 map 部分删除。所以,当我这样做时,它会抛出错误无法读取undefined 的属性'innerHTML',因为我猜测它需要 var服务










$ var map;
var infowindow;

//创建位于中心位置的Google Map

函数initMap(纬度,经度){
var location = {lat:latitude,lng:longitude};
console.log(location);

//移除此部分$ b $ map = new google.maps.Map(document.getElementById('map'),{$ b $ center:location,
zoom: 15
});
// ^^删除此部分

var service = new google.maps.places.PlacesService(map);
service.nearbySearch({
location:location,
radius:3200,
types:['school']
},callback);
}

//列出附近的地方

function callback(results,status){
if(status === google.maps.places。 PlacesService(状态服务状态.OK){
for(var i = 0; i< results.length; i ++){
listPlaces(results [i]);




解决方案


  1. 地图不是必需的,但如果没有显示,您必须显示带有该数据的Powered by Google徽标:




如果您的应用程序在不显示Google地图的页面或视图上显示Google Places API数据,则必须显示由Google驱动标识包含该数据。





  1. PlacesService构造函数可以使用 google.maps.Map对象或HTMLDivElement(您没有 提供地图,您可以提供div来显示




构造函数

PlacesService(attrContainer: HTMLDivElement | Map)创建一个新的PlacesService实例,用于在指定的容器中呈现归属。

代码段:
$ b

函数initMap(latitude,longitude){v ar location = {lat:latitude,lng:longitude};的console.log(位置); var service = new google.maps.places.PlacesService(document.getElementById('map')); service.nearbySearch({location:location,radius:3200,types:['school']},callback);} //列出附近的位置function callback(results,status){if(status === google.maps.places。 PlacesServiceServiceStatus.OK){for(var i = 0; i< results.length; i ++){document.getElementById('places')。innerHTML + = results [i] .name +,+ results [i]。附近+< br>; // listPlaces(results [i]); }} else {document.getElementById('places')。innerHTML + =Status:+ status +< br>; }} google.maps.event.addDomListener(window,load,function(){initMap(40.7127837,-74.0059413);});

  #map {height:auto;宽度:100%; margin:0px; < script src =https://   


I have some code heavily borrowed from Google's examples. All I'm using is the Google Places results part of this code, so I'd like to remove the map part out. So, when I do, it throws an error of Cannot read property 'innerHTML' of undefined, since I'm guessing it's needed for the var service function.

Any help removing this area of code?

var map;
var infowindow;

// Create Google Map with location at center

function initMap(latitude, longitude) {
  var location = {lat: latitude, lng: longitude};
  console.log(location);

  // REMOVE THIS SECTION
  map = new google.maps.Map(document.getElementById('map'), {
    center: location,
    zoom: 15
  });
  // ^^ REMOVE THIS SECTION

  var service = new google.maps.places.PlacesService(map);
  service.nearbySearch({
    location: location,
    radius: 3200,
    types: ['school']
  }, callback);
}

// List nearby places

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

解决方案

According to the documentation:

  1. a map isn't required, but if one isn't displayed you must show a "Powered by Google" logo with that data:

If your application displays Google Places API data on a page or view that does not also display a Google Map, you must show a "Powered by Google" logo with that data.

  1. The PlacesService Constructor takes either a google.maps.Map object or a HTMLDivElement (you don't have to provide a map, you can provide a div to display the manadatory attributions)

Constructor

PlacesService(attrContainer:HTMLDivElement|Map) Creates a new instance of the PlacesService that renders attributions in the specified container.

code snippet:

function initMap(latitude, longitude) {
  var location = {
    lat: latitude,
    lng: longitude
  };
  console.log(location);

  var service = new google.maps.places.PlacesService(document.getElementById('map'));
  service.nearbySearch({
    location: location,
    radius: 3200,
    types: ['school']
  }, callback);
}

// List nearby places
function callback(results, status) {
  if (status === google.maps.places.PlacesServiceStatus.OK) {
    for (var i = 0; i < results.length; i++) {
      document.getElementById('places').innerHTML += results[i].name + ", " + results[i].vicinity + "<br>";
      // listPlaces(results[i]);
    }
  } else {
    document.getElementById('places').innerHTML += "Status: " + status + "<br>";
  }
}
google.maps.event.addDomListener(window, "load", function() {
  initMap(40.7127837, -74.0059413);
});

#map {
  height: auto;
  width: 100%;
  margin: 0px;
  padding: 0px
}

<script src="https://maps.googleapis.com/maps/api/js?libraries=places"></script>
<div id="map"></div>
<div id="places"></div>
<img src="https://developers.google.com/places/documentation/images/powered-by-google-on-white.png" alt="Google Logo" />

这篇关于无法删除功能的地图部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆