거리 주소를 좌표로 변환하는 자바 스크립트가 있습니다. 하지만 나는 사용자가 입력 필드의 주소를 입력 할 때 javascript를이를 lat 및 lng로 변환하고 입력 필드 아래에 p div에 결과를 넣기를 원합니다.거리 주소를 좌표로 변환하고 html로 배치하십시오.
내가 가진 JS 코드는 사전에
var latitude;
var longitude;
/*
* Get the json file from Google Geo
*/
function Convert_LatLng_To_Address(address, callback) {
url = "http://maps.googleapis.com/maps/api/geocode/json?address=" + address + "&sensor=false";
jQuery.getJSON(url, function (json) {
Create_Address(json, callback);
});
}
/*
* Create an address out of the json
*/
function Create_Address(json, callback) {
if (!check_status(json)) // If the json file's status is not ok, then return
return 0;
latitude = json["results"][0]["geometry"]["location"]["lat"];
longitude = json["results"][0]["geometry"]["location"]["lng"];
callback();
}
/*
* Check if the json data from Google Geo is valid
*/
function check_status(json) {
if (json["status"] == "OK") return true;
return false;
}
<body>
Address:
<input type="text" id="address-input">
City:
<input type="text" id="city-input">
Country:
<input type="text" id="country-input">
Postcode:
<input type="text" id="address4">
<input type="submit" id="submit" value="generate" onClick="AlertLatLng()">
<p id="result"></p>
<script>
var street_address = document.getElementById('address-input').value,
city = document.getElementById('city-input').value,
country = document.getElementById('country-input').value,
postcode = document.getElementById('postcode-input').value;
var address = street_address + ' ' + city + ' ' + country + ', ' + postcode;
// AlertLatLng is a callback function which will be executed after converting address to coordinates
Convert_LatLng_To_Address(address, AlertLatLng);
/*
* Alert Latitude & Longitude
*/
function AlertLatLng() {
var result = "The latitude is " + latitude + " and longitude is " + longitude;
document.getElementById('result').innerHTML=result;
}
</scrip>
</body>
감사합니다 :)
위 코드를 편집하여 p 태그에 결과를 표시합니다. 사용자가 입력하는 입력 필드에서 정보를 얻기 위해 주소 변수에 무엇을 넣어야합니까? –
답변을 편집하여 여기에 포함시켜 드리겠습니다 ... –
질문에 대한 답변을 했습니까? 도움을 주신 덕분에 –