我试图在我的Google地图上显示所有Home Depots

时间:2012-04-27 21:41:57

标签: jquery google-maps-api-3 geocoding google-maps-markers

我是谷歌地图新手。我正在尝试制作一张使用KmlLayer的地图,显示整个美国的销售区域。这部分是成功的。

我现在想要将所有家庭仓库显示为标记,这样理论上,您可以访问地图,查看您的地区并查看您所在地区的所有家庭仓库。

这是我到目前为止所做的:

$(document).ready(function() {
    var map, geocoder;

    init();

    $('.showVendors').click(function() {
        loadKML();
        showVendors();
    });
});

function init() {
    var myOptions = {
        center: new google.maps.LatLng(37.09024,-95.712891),
        zoom: 3,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    map = new google.maps.Map(document.getElementById('map'), myOptions);
    geocoder = new google.maps.Geocoder();
}

function loadKML() {
    var kmlOptions = {
        clickable: true,
        map: map,
        preserveViewport: true,
        suppressInfoWindows: false
    }
    var kmlLayer = new google.maps.KmlLayer('kml/territories.kml', kmlOptions);

}

function showVendors() {
    var vendor = 'Home Depot';
    geocoder.geocode( { 'premise' : vendor }, function(results, status) {
        if ( status == google.maps.GeocoderStatus.OK ) {
            map.setCenter(results[1].geometry.location);
            var marker = new google.maps.Marker({
                map: map,
                position: results[1].geometry.location
            });
        } else {
            alert ( "Geocode was unsuccessful for the following reason: " + status );
        }
    });
}

1 个答案:

答案 0 :(得分:1)

当我直接通过API运行查询时:

http://maps.googleapis.com/maps/api/geocode/json?premise=Home%20Depot&sensor=true

我明白了:

{
   "results" : [],
   "status" : "ZERO_RESULTS"
}

在地理编码API中看起来没有此信息,这是您在上面使用的代码。您可以更好地使用places API,但它仍然是实验性的:

https://developers.google.com/maps/documentation/places/

相关问题