如何在数组中保存地理编码结果 [英] how to save geocode results in array

查看:44
本文介绍了如何在数组中保存地理编码结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将Geocoder请求生成的结果存储在全局数组中?比方说,我传递一个包含地址的数组,我想将LatLng结果存储在地理编码函数之外的新全局数组中?

Is it possible to store the results produced by Geocoder request in a global array? Let's say, I pass an array containing addresses, and I want LatLng results to be stored in a new global array outside the geocode function?

推荐答案

<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">
        var latLong = []; 
        var count = 0;
            
        function getLatLong(){


(#btn)。unbind('click')。click(function(e){
e .stopPropagation();
e.preventDefault();

var geocoder = new google.maps.Geocoder();
geocoder.geocode({'address':
("#btn").unbind('click').click(function(e) { e.stopPropagation(); e.preventDefault(); var geocoder = new google.maps.Geocoder(); geocoder.geocode( { 'address':


('#city')。val()},function(results){
latLong [count] = new Array();

latLong [ count] ['lat'] = results [0] .geometry.location.lat();
latLong [count] ['lng'] = results [0] .geometry.location.lng();

++ count;
});

console.log(latLong);
});
}
< / script>
< style>
.push-down {margin-top:25px;}
< / style>
< / head>

< body onload =getLatLong()>
< input type =textid =city>
< input id =btntype =buttonvalue =get Lat& Long/>
< div class =push-down>< / div>
< / body>
< / html>
('#city').val()}, function(results) { latLong[count] = new Array(); latLong[count]['lat'] = results[0].geometry.location.lat(); latLong[count]['lng'] = results[0].geometry.location.lng(); ++count; }); console.log(latLong); }); } </script> <style> .push-down {margin-top: 25px;} </style> </head> <body onload="getLatLong()"> <input type="text" id= "city"> <input id="btn" type="button" value="get Lat&Long" /> <div class="push-down"></div> </body> </html>





如果您将运行上述代码,结果将输出为=>



latLong [0] ['lat'] = 19.2898089

latLong [0] ['lng'] = 84.87185109999996

latLong [1] ['lat'] = 19.2947663

latLong [2] ['lng'] = 84.81182179999996



希望这个会帮助你:)。



If you will run the above code, the result will output as =>

latLong[0]['lat'] = 19.2898089
latLong[0]['lng'] = 84.87185109999996
latLong[1]['lat'] = 19.2947663
latLong[2]['lng'] = 84.81182179999996

Hope this will help you :).


这篇关于如何在数组中保存地理编码结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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