如何获取通过输入框输入的城市/国家的经纬度? [英] How to get longitude and latitude of a city/country inputted through an input box?

查看:162
本文介绍了如何获取通过输入框输入的城市/国家的经纬度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码,

 < html> 
< head>
< script src =http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js>< / script>
< script type =text / javascriptsrc =https://maps.googleapis.com/maps/api/js?sensor=false>< / script>
< script type =text / javascript>
函数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 =textid =city>
< input id =btntype =buttonvalue =get Lat& Long/>
< / body>
< / html>

这将在小对话框中给我输入城市的经纬度。但我需要在同一页面或父页面。请帮助我


< head>
< script src =http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js>< / script>
< script type =text / javascriptsrc =https://maps.googleapis.com/maps/api/js?sensor=false>< / script>
< script type =text / javascript>
函数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 wrong wrong+ status);
}
});
});
}
< / script>
< style>
.push-down {margin-top:25px;}< / style>
< / head>
< body onload =myIP()>
< input type =textid =city>
< input id =btntype =buttonvalue =get Lat& Long/>
< div class =push-down>< / div>
< / body>
< / html>


I have this code,

<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>

This will give me latitude and longitude of the inputted city in small dialog box. But I need this in the same page or parent page. Please help me

解决方案

<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>

这篇关于如何获取通过输入框输入的城市/国家的经纬度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆