Javascript 谷歌地图邮政编码打印位置错误?

Javascript 谷歌地图邮政编码打印位置错误?,javascript,php,google-maps,Javascript,Php,Google Maps,我试图从我的表格中提取数据,并在谷歌地图上绘图 我正在使用以下有效的 function addPostCode(zip) { geocoder.geocode( { 'address': zip}, function(results, status) { if (status == google.maps.GeocoderStatus.OK) { map.setCenter(results[0

我试图从我的表格中提取数据,并在谷歌地图上绘图

我正在使用以下有效的

function addPostCode(zip) {
            geocoder.geocode( { 'address': zip}, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK)
            {
                map.setCenter(results[0].geometry.location);
                var marker = new google.maps.Marker({
                map: map,
                position: results[0].geometry.location,
                name: zip
            });
            markers.push(marker);
            } else {
                alert("Geocode was not successful for the following reason: " + status);
            }
            });
        }

只有当我的邮政编码是LA14时,例如,它在德国绘制了一条名为LA14的道路,与英国的巴罗-因弗内斯相反,没有人知道如何解决这个问题?

你传递的是含糊不清的信息。如果你通过“伦敦”,试图通过“加拿大伦敦”而不是更流行的“英国伦敦”,同样的事情也会发生

选项1-如果您的所有邮政编码都在英国

您可以将
{'address':zip}
更改为
{'address':zip+',United Kingdom'}
。这只是指定邮政编码在英国。逗号是可选的

选项2-如果只有一些在英国

您可以向函数添加可选的
country
参数,然后在不明确的情况下向其传递“United Kingdom”:

function addPostCode(zip, country) {

    var address;

    if (typeof(country) === 'undefined')  // no country passed
        address = zip;
    else
        address = zip + ', ' + country;

    geocoder.geocode({'address': address}, function(results, status) {
...

我会选择选项2,即使您的值仅限英国,以防万一您稍后将其国际化。

您传递的是含糊不清的信息。如果你通过“伦敦”,试图通过“加拿大伦敦”而不是更流行的“英国伦敦”,同样的事情也会发生

选项1-如果您的所有邮政编码都在英国

您可以将
{'address':zip}
更改为
{'address':zip+',United Kingdom'}
。这只是指定邮政编码在英国。逗号是可选的

选项2-如果只有一些在英国

您可以向函数添加可选的
country
参数,然后在不明确的情况下向其传递“United Kingdom”:

function addPostCode(zip, country) {

    var address;

    if (typeof(country) === 'undefined')  // no country passed
        address = zip;
    else
        address = zip + ', ' + country;

    geocoder.geocode({'address': address}, function(results, status) {
...
我会选择选项2,即使你的价值观仅限于英国,以防万一你以后将其国际化