谷歌地图的地方没有定义

时间:2017-04-12 12:37:46

标签: javascript angularjs node.js

我在尝试在节点应用程序中使用谷歌地图时遇到了一些问题。到目前为止,我有地图加载并获取我的位置。我正在尝试实施谷歌代码,以便我可以允许用户搜索和本地地方出现。我正在使用google API页面中的代码并遇到了问题。当我运行代码时,它返回“google.maps.places is undefined”。我整个上午一直在寻找这个问题,似乎可以解决它。以下是代码,提前谢谢!

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

如果有人想知道我在地图加载的页面上有这个。我在实际页面上有我的API密钥

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

1 个答案:

答案 0 :(得分:0)

对于AngularJS,我正在使用GoogleMapAPI。 在documentation中,您可以看到必须等到api完全初始化。

我认为您遇到了同样的问题但是使用这种方法可以在更高的层次上解决它。

希望这可以帮到你