Ooin Kin
Ooin Kin

Reputation: 1

Google Map API (Places API) different result?

My problem is I'm trying to make a searching service. So, I'm trying to find some market like "공주 행복자전거" but, I can't find by places API, and I tried Google Map service (not API) and I find it.

I don't know why my script can't find it, and why Google Map can find.

<style>
  #map {height: 400px; width: 100%; }
</style>

<script src="https://maps.googleapis.com/maps/api/js?key=<?=$apiKey?>&libraries=places"></script>
   
<body>
  <h1>장소 검색</h1>
  <input id="location" type="text" placeholder="검색할 장소 입력" />
  <button id="search">검색</button>
  <div id="map"></div>
  <script src="script.js"></script>
</body>
  
 <script>
let map;
let service;

function initMap() {
    const location = new google.maps.LatLng(-33.867, 151.195); // 초기 위치 설정

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

    service = new google.maps.places.PlacesService(map);

    document.getElementById("search").addEventListener("click", searchPlaces);
}

function searchPlaces() {
    const locationInput = document.getElementById("location").value;
    
    const request = {
        query: locationInput,
        fields: ["name", "geometry"],
    };

    service.findPlaceFromQuery(request, (results, status) => {
        if (status === google.maps.places.PlacesServiceStatus.OK) {
            for (let i = 0; i < results.length; i++) {
                createMarker(results[i]);
            }
            map.setCenter(results[0].geometry.location);
        } else {
            alert("장소를 찾을 수 없습니다: " + status);
        }
    });
}

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

    const infowindow = new google.maps.InfoWindow({
        content: place.name,
    });

    marker.addListener("click", () => {
        infowindow.open(map, marker);
    });
}

google.maps.event.addDomListener(window, "load", initMap);


   </script>

I'm trying to many type of search on places API,

findPlaceFromQuery, nearbySearch, textSearch.. but I can't find..

Upvotes: 0

Views: 43

Answers (0)

Related Questions