简体   繁体   English

谷歌地图api geoJSON - 没有出现描述框

[英]google maps api geoJSON - no description box appearing

I am trying to implement a simple google maps into a website.我正在尝试在网站中实现一个简单的谷歌地图。 I am successfully retrieving my coordinates store for each location from the database, and successfully writing them to the map using addGeoJson.我成功地从数据库中检索了每个位置的坐标存储,并成功地使用 addGeoJson 将它们写入地图。

My problem is there is no text appearing when you click on each location marker.我的问题是当您单击每个位置标记时没有出现文本。 I was unable to find this issue through searching so I am bringing it here!我无法通过搜索找到这个问题,所以我把它带到了这里!

my google places javascript:我的谷歌地方 javascript:

    function initAutocomplete() {
    var map = new google.maps.Map(document.getElementById('map'), {
        center: {lat: 38.789763, lng: -89.991966},
        zoom: 13,
        mapTypeId: 'roadmap',
        styles: mapStyle
    });
//load coordinates of restaurants from db
    $.get('/getPlaces', function (data) {
        placesJSON = {"type": "FeatureCollection", "features": [], "type": "FeatureCollection"};
        places = jQuery.parseJSON(data);
        for (var i = 0; i < places.length; i++) {
            placesJSON.features.push({"type": "Feature",
                "geometry": {
                    "type": "Point",
                    "coordinates": [Number(places[i].lng), Number(places[i].lat)]
                },
                "properties": {
                    "title": "Lincoln Park",
                    "description": "A northside park that is home to the Lincoln Park Zoo"
                },
            });
        }
        console.log(placesJSON);
        map.data.addGeoJson(placesJSON);
    })
            .fail(function (xhr, textStatus, errorThrown) {
                alert(errorThrown);
            });
    infoWindow = new google.maps.InfoWindow;
// Try HTML5 geolocation.
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function (position) {
            var pos = {
                lat: position.coords.latitude,
                lng: position.coords.longitude
            };
            infoWindow.setPosition(pos);
            map.setCenter(pos);
        }, function () {
            handleLocationError(true, infoWindow, map.getCenter());
        });
    } else {
// Browser doesn't support Geolocation
        handleLocationError(false, infoWindow, map.getCenter());
    }
// Create the search box and link it to the UI element.
    var input = document.getElementById('pac-input');
    var searchBox = new google.maps.places.SearchBox(input);
// Bias the SearchBox results towards current map's viewport.
    map.addListener('bounds_changed', function () {
        searchBox.setBounds(map.getBounds());
    });

// Listen for the event fired when the user selects a prediction and retrieve
// more details for that place.
    var markers = [];
// Listen for the event fired when the user selects a prediction and retrieve
// more details for that place.
    searchBox.addListener('places_changed', function () {
        var places = searchBox.getPlaces();
        if (places.length == 0) {
            return;
        }

        // Clear out the old markers.
        markers.forEach(function (marker) {
            marker.setMap(null);
        });
        markers = [];
        // For each place, get the icon, name and location.
        var bounds = new google.maps.LatLngBounds();
        places.forEach(function (place) {
            console.log(place);
            if (!place.geometry) {
                console.log("Returned place contains no geometry");
                return;
            }
            var icon = {
                url: place.icon,
                size: new google.maps.Size(71, 71),
                origin: new google.maps.Point(0, 0),
                anchor: new google.maps.Point(17, 34),
                scaledSize: new google.maps.Size(25, 25)
            };
            // Create a marker for each place.
            markers.push(new google.maps.Marker({
                map: map,
                icon: icon,
                title: place.name,
                position: place.geometry.location
            }));
            if (place.geometry.viewport) {
                // Only geocodes have viewport.
                bounds.union(place.geometry.viewport);
            } else {
                bounds.extend(place.geometry.location);
            }
        });
        map.fitBounds(bounds);
    });
}

notice i put placeholder text that will be repeated for every item.请注意,我放置了占位符文本,每个项目都会重复这些文本。 That is just for testing purposes.那只是为了测试目的。

I found the solution on another post:我在另一篇文章中找到了解决方案:

map.data.addListener('click', function (event) {
    infoWindow.setContent(event.feature.getProperty('name') + "<br>" + event.feature.getProperty('description'));
    infoWindow.setPosition(event.latLng);
    infoWindow.setOptions({pixelOffset: new google.maps.Size(0, -34)});
    infoWindow.open(map);
});

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM