从地理编码对象获取谷歌纵横 [英] Getting Latitude from Geocode Object

查看:76
本文介绍了从地理编码对象获取谷歌纵横的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 <!DOCTYPE html>我想从地理编码器返回的结果对象中获取经度和纬度。 
< html>
< head>
< meta name =viewportcontent =initial-scale = 1.0,user-scalable = no/>
< meta http-equiv =content-typecontent =text / html; charset = UTF-8/>
< title>智能头脑< / title>
< link href =http://code.google.com/apis/maps/documentation/javascript/examples/default.css =stylesheettype =text / css/>
< script type =text / javascriptsrc =http://maps.googleapis.com/maps/api/js?sensor=false>< / script>
< script type =text / javascript>
var geocoder;
var map;
函数initialize(){
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(-34.397,150.644);
var myOptions = {
zoom:4,
center:latlng,
mapTypeId:google.maps.MapTypeId.ROADMAP
}
map = new google .maps.Map(document.getElementById(map_canvas),myOptions);
}

函数codeAddress(){
var address = document.getElementById(address)。value;
geocoder.geocode({'address':address},function(results,status){
if(status == google.maps.GeocoderStatus.OK){
map.setCenter(results [0] .geometry.location);
var marker = new google.maps.Marker({
map:map,
position:results [0] .geometry.location
});



var inp = results [0] .geometry.location;


alert(inp);
$ alert(由于以下原因,Geocode不成功:+ status;
}
});
}
< / script>
< / head>
< body onLoad =initialize()>


<! - < / div> - >
<! - < div id =map_canvasstyle =height:80%; top:30px; right:40%; left:20%>< / div> - >
< div id =map_canvasstyle =border-width:0; width:500px; height:500px; left:40px; right:60px; top:20px>< / div>

< input id =addresstype =textboxvalue =style =margin-top:60px; margin-left:400px; placeholder =输入您的位置>
< input type =buttonvalue =Get me ThereonClick =codeAddress()>
< / body>
< / html>

结果[0] .geometry.location 包含纬度和经度,但我想获得经度和纬度的单独值。我试图在 results [0] .geometry.location 上使用javascript函数,如split()和slice(),但这不起作用。任何人都可以告诉我我该怎么做?

p>

  var lat = results [0] .geometry.location.lat(); 
var lng = results [0] .geometry.location.lng();


I want to get the latitude and longitude from the result object returned by geocoder.

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no"/>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>Smart Minds</title>
<link href="http://code.google.com/apis/maps/documentation/javascript/examples/default.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
  var geocoder;
  var map;
  function initialize() {
    geocoder = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng(-34.397, 150.644);
    var myOptions = {
      zoom: 4,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
  }

  function codeAddress() {
    var address = document.getElementById("address").value;
    geocoder.geocode( { 'address': address}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        map.setCenter(results[0].geometry.location);
        var marker = new google.maps.Marker({
            map: map,
            position: results[0].geometry.location
        });



        var inp=results[0].geometry.location;


        alert(inp);
      } else {
        alert("Geocode was not successful for the following reason: " + status);
      }
    });
  }
</script>
</head>
<body onLoad="initialize()">


  <!--</div>-->
<!--<div id="map_canvas" style="height:80%;top:30px;right:40%;left:20%"></div>-->
 <div id="map_canvas" style="border-width:0;width:500px;height:500px;left:40px;right:60px;top:20px"></div>

  <input id="address" type="textbox" value="" style="margin-top:60px;margin-left:400px;" placeholder="Enter your Location">
    <input type="button" value="Get me There" onClick="codeAddress()">
</body>
</html>

The results[0].geometry.location contains both the latitude and the longitude but I want to get separate values of longitude and latitude. I had tried to use a javascript function like split() and slice() on results[0].geometry.location but that is not working. Can anyone please tell me how I can do that?

解决方案

You should be able to access the individual values like this:

var lat = results[0].geometry.location.lat();
var lng = results[0].geometry.location.lng();

这篇关于从地理编码对象获取谷歌纵横的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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