Google 地图地理编码器 javascript API 非英语城市名称的结果为零
Google Maps Geocoder javascript API with zero results with non english city names
当我尝试使用 google 地图地理编码器获取位置时,我无法使用德语城市名称获取结果。只有使用英文名称我才能得到结果:
var geocoder = new google.maps.Geocoder();
var address = "schildergasse 1, köln, deutschland"; // No Results
// var address = "schildergasse 1, cologne, deutschland"; // GET Results
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK)
{
alert(JSON.stringify(results[0].geometry.location));
}else{
alert(JSON.stringify(status));
}
});
但是,如果我在 google API 页面上尝试:https://developers.google.com/maps/documentation/geocoding/intro?hl=de
德语语言请求工作正常。
我怎样才能获得德语语言请求的结果?
编辑:
只有地理编码器不起作用..
解法:
我错过了 <meta charset="utf-8">
我可以得到那个地址的结果。如果仍有问题,您可能还想考虑将 region: 'de'
添加到地理编码请求中。这是一个 example JSBin adapted from the Google Devs Geocoding Sample
<!DOCTYPE html>
<html>
<head>
<title>Geocoding service</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<style>
/* Always set the map height explicitly to define the size of the div
* element that contains the map. */
#map {
height: 100%;
}
/* Optional: Makes the sample page fill the window. */
html, body {
height: 100%;
margin: 0;
padding: 0;
}
#floating-panel {
position: absolute;
top: 10px;
left: 25%;
z-index: 5;
background-color: #fff;
padding: 5px;
border: 1px solid #999;
text-align: center;
font-family: 'Roboto','sans-serif';
line-height: 30px;
padding-left: 10px;
}
</style>
</head>
<body>
<div id="floating-panel">
<input id="address" type="textbox" value="schildergasse 1, köln, deutschland">
<input id="submit" type="button" value="Geocode">
</div>
<div id="map"></div>
<script>
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 8,
center: {lat: -34.397, lng: 150.644}
});
var geocoder = new google.maps.Geocoder();
geocodeAddress(geocoder, map);
document.getElementById('submit').addEventListener('click', function() {
geocodeAddress(geocoder, map);
});
}
function geocodeAddress(geocoder, resultsMap) {
var address = document.getElementById('address').value;
geocoder.geocode({
address: address
}, function(results, status) {
if (status === 'OK') {
resultsMap.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: resultsMap,
position: results[0].geometry.location
});
var infoWindow = new google.maps.InfoWindow({
content: results[0].formatted_address
});
infoWindow.open(map, marker);
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
}
</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?callback=initMap&key=YOUR_KEY">
</script>
</body>
</html>
当我尝试使用 google 地图地理编码器获取位置时,我无法使用德语城市名称获取结果。只有使用英文名称我才能得到结果:
var geocoder = new google.maps.Geocoder();
var address = "schildergasse 1, köln, deutschland"; // No Results
// var address = "schildergasse 1, cologne, deutschland"; // GET Results
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK)
{
alert(JSON.stringify(results[0].geometry.location));
}else{
alert(JSON.stringify(status));
}
});
但是,如果我在 google API 页面上尝试:https://developers.google.com/maps/documentation/geocoding/intro?hl=de 德语语言请求工作正常。
我怎样才能获得德语语言请求的结果?
编辑:
只有地理编码器不起作用..
解法:
我错过了 <meta charset="utf-8">
我可以得到那个地址的结果。如果仍有问题,您可能还想考虑将 region: 'de'
添加到地理编码请求中。这是一个 example JSBin adapted from the Google Devs Geocoding Sample
<!DOCTYPE html>
<html>
<head>
<title>Geocoding service</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<style>
/* Always set the map height explicitly to define the size of the div
* element that contains the map. */
#map {
height: 100%;
}
/* Optional: Makes the sample page fill the window. */
html, body {
height: 100%;
margin: 0;
padding: 0;
}
#floating-panel {
position: absolute;
top: 10px;
left: 25%;
z-index: 5;
background-color: #fff;
padding: 5px;
border: 1px solid #999;
text-align: center;
font-family: 'Roboto','sans-serif';
line-height: 30px;
padding-left: 10px;
}
</style>
</head>
<body>
<div id="floating-panel">
<input id="address" type="textbox" value="schildergasse 1, köln, deutschland">
<input id="submit" type="button" value="Geocode">
</div>
<div id="map"></div>
<script>
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 8,
center: {lat: -34.397, lng: 150.644}
});
var geocoder = new google.maps.Geocoder();
geocodeAddress(geocoder, map);
document.getElementById('submit').addEventListener('click', function() {
geocodeAddress(geocoder, map);
});
}
function geocodeAddress(geocoder, resultsMap) {
var address = document.getElementById('address').value;
geocoder.geocode({
address: address
}, function(results, status) {
if (status === 'OK') {
resultsMap.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: resultsMap,
position: results[0].geometry.location
});
var infoWindow = new google.maps.InfoWindow({
content: results[0].formatted_address
});
infoWindow.open(map, marker);
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
}
</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?callback=initMap&key=YOUR_KEY">
</script>
</body>
</html>