Google maps 有可能给restfulgoogleplacesapi一个边界框吗?

Google maps 有可能给restfulgoogleplacesapi一个边界框吗?,google-maps,Google Maps,我想使用或其自动完成功能,使用边界(使用Maps API地理编码调用从Google检索)查找单个地区的位置 autocomplete API有一个在中使用边界的示例,但使用我的坐标或原始示例“原样”(仅替换API键)似乎不起作用 所以:我的问题是,是否有可能为PlacesAPI搜索提供一个边界框,用于检索单个城镇内的地点?如果可能的话,自动完成API的使用会更整洁。我遇到了同样的问题,并在提交过程中使用回调函数解决了它。 这并不理想,因为它仍然允许用户选择无效的位置,但我会在提交过程中进行验证。

我想使用或其自动完成功能,使用边界(使用Maps API地理编码调用从Google检索)查找单个地区的位置

autocomplete API有一个在中使用边界的示例,但使用我的坐标或原始示例“原样”(仅替换API键)似乎不起作用


所以:我的问题是,是否有可能为PlacesAPI搜索提供一个边界框,用于检索单个城镇内的地点?如果可能的话,自动完成API的使用会更整洁。

我遇到了同样的问题,并在提交过程中使用回调函数解决了它。 这并不理想,因为它仍然允许用户选择无效的位置,但我会在提交过程中进行验证。如果他们在autocomplete函数上提供回调,那就太好了(也许他们提供了,但我看不到)

对于《疯狂》,我有一个非常简单的表单,可以根据城市搜索目的地:

Choose a City: [ San Francisco ]
Choose a Destination: [ Autocomplete Google Places API textbox  ]
[Submit Button]
首先,我设置了我的城市选择列表(我在这里使用ASP.NET MVC,但您知道了):

对SelectChange的调用非常直截了当:

function onSelectChange() {
    $('#destinationInputId').val("");

    var selected = $("#citySelectId option:selected");
    var nelat = selected[0].getAttribute('data-ne-latitude');
    var nelng = selected[0].getAttribute('data-ne-longitude');
    var swlat = selected[0].getAttribute('data-sw-latitude');
    var swlng = selected[0].getAttribute('data-sw-longitude');

    var cityLatLngBounds = new google.maps.LatLngBounds(
        new google.maps.LatLng(swlat, swlng),
        new google.maps.LatLng(nelat, nelng)
    );

    if (autocomplete == undefined) {
        autocomplete = new google.maps.places.Autocomplete(destinationInput, { bounds: cityLatLngBounds });
    } else {
        autocomplete.setBounds(cityLatLngBounds)
    }

    google.maps.event.addListener(autocomplete, 'place_changed', function () {
        var place = autocomplete.getPlace();
        if (!inCityBounds(place.geometry.location.lat(), place.geometry.location.lng())) {
            popError("The destination you selected (" + place.name + ") does not fall within the city bounds");
        }
    });
};
inCityBounds非常简单:

function inCityBounds(lat, long) {
    var selected = $("#citySelectId option:selected");
    var nelat = selected[0].getAttribute('data-ne-latitude');
    var nelng = selected[0].getAttribute('data-ne-longitude');
    var swlat = selected[0].getAttribute('data-sw-latitude');
    var swlng = selected[0].getAttribute('data-sw-longitude');

    var cityLatLngBounds = new google.maps.LatLngBounds(
        new google.maps.LatLng(swlat, swlng),
        new google.maps.LatLng(nelat, nelng)
    );

    var destination = new google.maps.LatLng(lat, long);

    return cityLatLngBounds.contains(destination);
};
popError()函数只显示一些错误消息:

$('#errorSpanId').html(errorMsg).show();
然后我的验证/提交代码再次检查它,如果该项不在范围内,则返回false


希望这有帮助

我遇到了同样的问题,并在提交过程中使用回调函数解决了它。 这并不理想,因为它仍然允许用户选择无效的位置,但我会在提交过程中进行验证。如果他们在autocomplete函数上提供回调,那就太好了(也许他们提供了,但我看不到)

对于《疯狂》,我有一个非常简单的表单,可以根据城市搜索目的地:

Choose a City: [ San Francisco ]
Choose a Destination: [ Autocomplete Google Places API textbox  ]
[Submit Button]
首先,我设置了我的城市选择列表(我在这里使用ASP.NET MVC,但您知道了):

对SelectChange的调用非常直截了当:

function onSelectChange() {
    $('#destinationInputId').val("");

    var selected = $("#citySelectId option:selected");
    var nelat = selected[0].getAttribute('data-ne-latitude');
    var nelng = selected[0].getAttribute('data-ne-longitude');
    var swlat = selected[0].getAttribute('data-sw-latitude');
    var swlng = selected[0].getAttribute('data-sw-longitude');

    var cityLatLngBounds = new google.maps.LatLngBounds(
        new google.maps.LatLng(swlat, swlng),
        new google.maps.LatLng(nelat, nelng)
    );

    if (autocomplete == undefined) {
        autocomplete = new google.maps.places.Autocomplete(destinationInput, { bounds: cityLatLngBounds });
    } else {
        autocomplete.setBounds(cityLatLngBounds)
    }

    google.maps.event.addListener(autocomplete, 'place_changed', function () {
        var place = autocomplete.getPlace();
        if (!inCityBounds(place.geometry.location.lat(), place.geometry.location.lng())) {
            popError("The destination you selected (" + place.name + ") does not fall within the city bounds");
        }
    });
};
inCityBounds非常简单:

function inCityBounds(lat, long) {
    var selected = $("#citySelectId option:selected");
    var nelat = selected[0].getAttribute('data-ne-latitude');
    var nelng = selected[0].getAttribute('data-ne-longitude');
    var swlat = selected[0].getAttribute('data-sw-latitude');
    var swlng = selected[0].getAttribute('data-sw-longitude');

    var cityLatLngBounds = new google.maps.LatLngBounds(
        new google.maps.LatLng(swlat, swlng),
        new google.maps.LatLng(nelat, nelng)
    );

    var destination = new google.maps.LatLng(lat, long);

    return cityLatLngBounds.contains(destination);
};
popError()函数只显示一些错误消息:

$('#errorSpanId').html(errorMsg).show();
然后我的验证/提交代码再次检查它,如果该项不在范围内,则返回false


希望这有帮助

问题是我正在尝试将用户位置与Places API结果相匹配。我不想根据她的位置限制用户的选择,我只想提供她附近的位置作为自动填充字段。因此,在获取结果之前,我需要边界框:如果我在获取搜索结果时应用它,我可能会忽略很多本地位置。但是,如果我以后需要进行后期验证,请记住您的解决方案是一个不错的解决方案,谢谢:)问题是我正在尝试将用户位置与Places API结果相匹配。我不想根据她的位置限制用户的选择,我只想提供她附近的位置作为自动填充字段。因此,在获取结果之前,我需要边界框:如果我在获取搜索结果时应用它,我可能会忽略很多本地位置。不过,如果我以后需要进行后期验证,您的解决方案是一个值得记住的好方案,谢谢:)