Jquery 如何通过输入框输入城市/国家的经纬度?
我有这个密码Jquery 如何通过输入框输入城市/国家的经纬度?,jquery,html,google-maps,Jquery,Html,Google Maps,我有这个密码 <html> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=false"></script> <
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
function myIP(){
$("#btn").click(function(){
var geocoder = new google.maps.Geocoder();
geocoder.geocode( { 'address': '#city'}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
alert("location : " + results[0].geometry.location.lat() + " " +results[0].geometry.location.lng());
} else {
alert("Something got wrong " + status);
}
});
});
}
</script>
</head>
<body onload="myIP()">
<input type="text" id= "city">
<input id="btn" type="button" value="get Lat&Long" />
</body>
</html>
函数myIP(){
$(“#btn”)。单击(函数(){
var geocoder=new google.maps.geocoder();
geocoder.geocode({'address':'#city'},函数(结果,状态){
if(status==google.maps.GeocoderStatus.OK){
警报(“位置:+结果[0].geometry.location.lat()+”“+结果[0].geometry.location.lng());
}否则{
警惕(“出错”+状态);
}
});
});
}
这将给我在小对话框中输入的城市的纬度和经度。但是我需要在同一个页面或父页面中使用它。请帮帮我
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
function myIP(){
$("#btn").click(function(){
var geocoder = new google.maps.Geocoder();
geocoder.geocode( { 'address': $('#city').val()}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
$('.push-down').text("location : " + results[0].geometry.location.lat() + " " +results[0].geometry.location.lng());
} else {
$('.push-down').text("Something got wrong " + status);
}
});
});
}
</script>
<style>
.push-down {margin-top: 25px;}</style>
</head>
<body onload="myIP()">
<input type="text" id= "city">
<input id="btn" type="button" value="get Lat&Long" />
<div class="push-down"></div>
</body>
</html>
函数myIP(){
$(“#btn”)。单击(函数(){
var geocoder=new google.maps.geocoder();
geocoder.geocode({'address':$('#city').val()},函数(结果,状态){
if(status==google.maps.GeocoderStatus.OK){
$('.push down').text(“位置:“+results[0].geometry.location.lat()+”“+results[0].geometry.location.lng());
}否则{
$('.push down').text(“出现了问题”+状态);
}
});
});
}
.按下{页边距顶部:25px;}
这是您自己的代码吗?在这种情况下,你尝试了什么?不是我的代码。我试图通过php输入表单输入它,但徒劳无功。当我尝试使用它时,无论我输入什么,我都会得到相同的坐标。是的,这是正确的,任何人都可以帮助我。geocoder.geocode({‘地址’:‘城市’}。你把#城市放在你应该输入的地方,即$(‘城市’).val()但那个代码对我来说仍然不起作用。我有什么需要补充的吗?你说“不起作用”是什么意思?有错误吗?你的浏览器控制台怎么说?很抱歉,这是我的错误。它工作正常。但是你能帮助我在页面中而不是在弹出对话框中获得结果吗?我更新了代码,使消息显示在输入下方一点。你使用了“页面”所以我选择了同一页。如果这解决了你的问题,请不要忘记接受答案。当然我会接受的,非常感谢alkis,它工作得很好。
$.getJSON('https://maps.googleapis.com/maps/api/geocode/json?address=' + Loc + '&sensor=false', null, function (data) {
var p = data.results[0].geometry.location
var latlng = new google.maps.LatLng(p.lat, p.lng);
});