简体   繁体   English

谷歌地图的地方没有定义

[英]google maps places is not defined

I am having a bit of an issue with trying to use google maps in my node application. 我在尝试在节点应用程序中使用谷歌地图时遇到了一些问题。 I have the map loading and getting my location so far. 到目前为止,我有地图加载并获取我的位置。 I am trying to implement google code so I can allow users to search and local places appear. 我正在尝试实施谷歌代码,以便我可以允许用户搜索和本地地方出现。 I am using the code from the google API page and have run into an issue. 我正在使用google API页面中的代码并遇到了问题。 When I run the code its returns "google.maps.places is undefined". 当我运行代码时,它返回“google.maps.places is undefined”。 I have been looking all morning for the issue and can seem to fix it. 我整个上午一直在寻找这个问题,似乎可以解决它。 Below is the code, thanks in advance! 以下是代码,提前谢谢!

function initialize() {
    var mapProp = {
        center:new google.maps.LatLng(51.508742,-0.120850),
        zoom:16,
        mapTypeId: 'roadmap'
    };
    var map = new google.maps.Map(document.getElementById("googleMap"), mapProp);
    var infoWindow = new google.maps.InfoWindow({map: map});

    //GEOLOCATION START
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function(position) {
            var pos = {
                lat: position.coords.latitude,
                lng: position.coords.longitude
            };

            infoWindow.setPosition(pos);
            infoWindow.setContent('Location found.');
            map.setCenter(pos);
        }, function() {
            handleLocationError(true, infoWindow, map.getCenter());
        });
    } else {
        // Browser doesn't support Geolocation
        handleLocationError(false, infoWindow, map.getCenter());
    }
}

function handleLocationError(browserHasGeolocation, infoWindow, pos) {
    infoWindow.setPosition(pos);
    infoWindow.setContent(browserHasGeolocation ?
        'Error: The Geolocation service failed.' :
        'Error: Your browser doesn\'t support geolocation.');
}
//GEOLOCATION END

//PLACES START
var input = document.getElementById('pac-input');
var searchBox = new google.maps.places.Autocomplete(input);
map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);

map.addListener('bounds_changed', function() {
    searchBox.setBounds(map.getBounds());
});

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.getPlace();

    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) {
        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);
});



google.maps.event.addDomListener(window, 'load', initialize);
}
]);

For anyone wonder aswell I have this on the page that the map loads on. 对于任何人都不知道我是否在地图加载的页面上有这个。 I have my API key on the actual page 我在实际页面上有我的API密钥

<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?v=3&key=......&sensor=false&callback=initializeMap&libraries=places"></script>

For AngularJS i am using the GoogleMapAPI . 对于AngularJS,我正在使用GoogleMapAPI In the documentation you can see that you have to wait until the api has been fully initialized. 文档中,您可以看到必须等到api完全初始化。

I think that you have the same problem but with this approach you can solve it at an higher level. 我认为你有同样的问题但是通过这种方法你可以在更高的层次上解决它。

Hope this could help you 希望这可以帮到你

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

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