【发布时间】:2016-10-30 18:39:02
【问题描述】:
我有一个城市名称列表。如果用户选择一个,我想将我的 angular-google-map 的中心设置为那个特定的城市。
console.log 工作正常,我得到了正确的城市名称,但如果我将它传递给一个函数,它会替换重音符号。
$scope.selectCity = function (city) {
console.log(city.name) //it works fine. The log: Abaújkér
locationService.getLatLngByCityName(city.name).then(function (result){
var cityLatLng = result.data.results[0].geometry.location;
console.log(cityLatLng);
}).catch(function (error) {
console.log(error);
});
};
locationService.getLatLngByCityName 返回错误请求:
GET http://maps.googleapis.com/maps/api/geocode/json?address=Aba%FAjk%E9r&sensor=true 400(错误请求)
工厂:
factory.getLatLngByCityName = function (city) {
console.log(city); // it works fine. The log: Abaújkér
var API_URL = 'http://maps.googleapis.com/maps/api/geocode/json?address=' + city;
return $http({
method: 'GET',
url: API_URL,
params: { sensor: true }
})
.catch(function () {
return $q.reject();
});
}
我猜它与编码有关。如何修复 googleapis 的 URL?
【问题讨论】:
-
使用 encodeURI(uri);关于城市名称
-
非常感谢@Vanojx1!有用。如果您可以将其发布为答案,我会接受。
标签: javascript angularjs google-maps google-maps-api-3 utf-8