Skip to content

Instantly share code, notes, and snippets.

Created July 5, 2016 12:22
Show Gist options
  • Save anonymous/03948a97ee6f2a881400c9bce7be0691 to your computer and use it in GitHub Desktop.
Save anonymous/03948a97ee6f2a881400c9bce7be0691 to your computer and use it in GitHub Desktop.
input-latlng-get-geo
<body>
<h1>输入经纬度值</h1>
<br><p>latitude</p>
<input id="latitude"> </input>
<br>
<p>longitude</p>
<input id="longitude"> </input>
<br>
<button class="btn btn-primary" onclick="searchIt()"> Enter</button>
<br>
<P>地址是:</P><p id="city"> </p>
</body>
function searchIt()
{
var latitude=$("#latitude").val();
var longitude=$("#longitude").val();
if(latitude===null||latitude===undefined||longitude===''||longitude===null||longitude===undefined||longitude===''){
alert("please input latitude and longitude");
}
else
{
var url="https://maps.googleapis.com/maps/api/geocode/json?latlng="+latitude+","+longitude+"&key=AIzaSyBUmxn8RqqIs74jh1wRup-FxpYNBUht4wg&sensor=true"
}
//使用alert()方法可以用来调试buG.
$(function(){
alert(url);
// var //url='https://maps.google.com/maps/api/geocode/json?latlng='+latlng+'&key=AIzaSyBUmxn8RqqIs74jh1wRup-FxpYNBUht4wg&sensor=true'
$.ajax({
type:"GET",
url:url
// dataType:'jsonp' 不能使用jsonp这样的数据类型在这里。
})
.done(function(data){
var city=data.results[0].formatted_address
//状态码用于判断是否正常很好用
$("#city").html(city);
})
.fail(function(data){
alert("failed")
})
})
}
<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
body{
text-align:center;
}
p{
font-size:30px;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment