nearbySearchで検索した結果を元にgetDetailsで詳細情報を取得しようとした際、
nearbySearchの検索結果数とgetDetailsでの取得結果数に相違が発生してしまいます。
例:
・nearbySearchで検索 → 検索結果20件
・検索結果を元にgetDetailsで詳細を取得 → 9件
ただ単に詳細情報が無いのかと思いましたが、本家googleMapには詳細情報も登録されていました。
どなたか知見のある方がいらっしゃいましたらご教示願います。
import $ from 'jquery'; $(function () { 'use strict'; let map; let service; let infowindow; let place; let place_id; //初期値は東京駅に設定 let pyrmont = new google.maps.LatLng(35.681236, 139.767125); geoLocationInit(); createMap(pyrmont); // 現在地取得 document.getElementById('getCurrentLocation').onclick = function () { geoLocationInit(); }; function geoLocationInit() { if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(success, fail); } else { createMap(pyrmont); } }; // success function success(position) { let currentLat = position.coords.latitude; let currentLng = position.coords.longitude; let pyrmont = new google.maps.LatLng(currentLat, currentLng); createMap(pyrmont); CurrentPositionMarker(pyrmont); }; // fail function fail(pyrmont) { alert('位置情報の取得を許可して下さい。'); createMap(pyrmont); }; function createMap(pyrmont) { map = new google.maps.Map(document.getElementById('map'), { center: pyrmont, zoom: 16 }); document.getElementById('getCafeLocation').onclick = function () { nearbySearchCafe(pyrmont); } }; function createMarker(latlng, icn, place) { let marker = new google.maps.Marker({ position: latlng, map: map, animation: google.maps.Animation.DROP }); let placeName = place.name; // 吹き出しにカフェの名前を埋め込む let contentString = `<div class="sample"><p id="place_name">${placeName}</p></div>`; // 吹き出し let infoWindow = new google.maps.InfoWindow({ // 吹き出しの追加 content: contentString // 吹き出しに表示する内容 }); marker.addListener('click', function () { // マーカーをクリックしたとき infoWindow.open(map, marker); // 吹き出しの表示 }); }; // 現在地のアイコンを表示 function CurrentPositionMarker(pyrmont) { let image = '/assets/img/icon_nowPos.png'; let marker = new google.maps.Marker({ position: pyrmont, map: map, icon: new google.maps.MarkerImage( image, //マーカー画像URL new google.maps.Size(60, 60), //マーカー画像のサイズ ), }); marker.setMap(map); }; // 周辺のカフェを検索 function nearbySearchCafe(pyrmont) { let request_near = { location: pyrmont, radius: '500', type: ['cafe'] }; service = new google.maps.places.PlacesService(map); service.nearbySearch(request_near, callback); function callback(results, status) { if (status == google.maps.places.PlacesServiceStatus.OK) { for (let i = 0; i < results.length; i++) { place = results[i]; place_id = place.place_id; let latlng = place.geometry.location; let icn = place.icon; //nearbySearchでの検索結果 console.log(place); let request_info = { placeId: place_id, }; service.getDetails(request_info, callback_info); function callback_info(place, status) { if (status == google.maps.places.PlacesServiceStatus.OK) { //nearbySearchで検索した結果の詳細 console.log(place); createMarker(latlng, icn, place); } }; }; }; }; }; });
回答1件
あなたの回答
tips
プレビュー