서버에서 Google API를 요청하는 것이 좋습니다. 그러나 서버를 설정하지 않으려면 몇 가지 해결 방법이 있습니다.
해결 방법 1 : 저도 같은 문제에 직면하고 PlaceID
// Get Place ID from Address
function getPlaceIDFromAddress(address, callback){
var place = {};
var request = {
location: new google.maps.LatLng(myLat, myLng),
radius: '50',
query: address
};
var service = new google.maps.places.PlacesService($scope.mapContainer);
service.textSearch(request, function(results, status){
if (status == google.maps.places.PlacesServiceStatus.OK) {
var placeId = results[0].place_id;
var location = results[0].geometry.location;
return callback(null, placeId);
}
});
}
을 얻을 수있는 PlacesService의를 사용하여 문제를 해결 한
다음 LatLng를 얻을 지오 코더하기 위해 PlaceID를 전달하거나 수행 할 수 있습니다 LatLng을 GeoCode로 전달하여 주소를 가져옵니다.
(new google.maps.Geocoder()).geocode({placeId: places_id}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
console.log("Address: ", results[0].formatted_address);
}});
해결 방법 2 :
하지만 권장하지 않습니다.
로컬 호스트에서 테스트하려는 경우 this 크롬 확장 프로그램을 설치할 수 있습니다. 매트릭스 api를 호출하기 전에 이것을 켜십시오.
이 확장은 헤더에 'Access-Control-Allow-Origin'을 삽입합니다.
CORS에 대해 읽어보십시오. 기본적으로 귀하의 도메인은 API를 쿼리 할 수 없습니다. 공개 API 인 경우 API 구성에서 허용 목록에있는 도메인 목록에 도메인을 추가해야합니다. 사용자 지정 응답 헤더를 추가해도 작동하지 않습니다. –
감사합니다 @ KabirRoy 그것은 이온 애플 리케이션이며 localhost에서 환경을 테스트하고 있습니다 : 각도 코드로 어떻게 올바르게 요청 헤더를 설정하여 로컬 호스트 도메인을 수용 할 수 있습니까? –
그것은 각도와 관련이 없습니다. 응답을 통해 서버에서 브라우저로 헤더를 전송해야합니다. –