简体   繁体   English

如何在此Google Maps JavaScript中提醒纬度?

[英]How do I alert the latitude in this Google Maps javascript?

As you can see, there is a line in there that does an alert. 如您所见,那里有一条线路可以发出警报。 It's " undefined ". 它是“ undefined ”。 Whereas if I just alert results[0].geometry.location , it alerts (41.321, 41.556) . 然而,如果我只是提醒results[0].geometry.location ,它会发出警报(41.321, 41.556)

I just want to alert it and set it (as an int) to one of my id_lat and id_longs... 我只是想提醒它并将它(作为一个int)设置为我的id_lat和id_longs ...

$("#geocodesubmit").click(function(){
        $("#id_lat").val("");
        $("#id_long").val("");
        var address = $("#addressinput").val();
        geocoder.geocode( { 'address': address}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            $("#badlocation_holder").hide();
            $("#map_canvas").show();
            $("#map_canvas_holder").show().css("background-color", "#E6E6FA").animate({"background-color":"#f5f5f5"}, 800);
            ;
            var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);  
            map.setCenter(results[0].geometry.location);

            alert(results[0].geometry.location[1]); //this is undefined.

            $("#id_lat").val(results[0].geometry.location[0]);
            $("#id_long").val(results[0].geometry.location[1]);


            var marker = new google.maps.Marker({
                map: map, 
                position: results[0].geometry.location,
                draggable:true
            });
          } else {
              $("#map_canvas_holder").hide();
              $("#badlocation_holder").show().css("background-color","#F08080").animate(
              {"background-color":"#f5f5f5"},800);
          }
        });
        return false;
    });

使用results[0].geometry.location.lat()获取纬度并获得经度的results[0].geometry.location.lng()

If you look at the API , location isn't an array (that's the main issue here), it's a property, so .location is the correct way to access it, not .location[0] . 如果你看一下APIlocation不是一个数组(这里是主要问题),它是一个属性,所以.location是访问它的正确方法,而不是.location[0]

The official documentation is a bit hard to find for specific types, but here are updated docs on that location (a LatLng type) : 官方文档有点难以找到特定类型, 但这里是该location更新文档( LatLng类型)

The toString() is what you're getting in an alert (lat, long) , there's also .lat() for latitude and .lng() for longitude, like this: toString()是你在警报中获得的(lat, long)(lat, long)也是.lat() ,经度是.lng() ,如下所示:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获取经度和纬度的谷歌在Javascript地图 - how to get longitude and latitude of google maps in javascript 如何使用Google Maps javascript获取搜索结果标记的纬度,经度 - How to get latitude, longitude of search result marker with google maps javascript 如何从函数获取纬度经度并将其发送到新的google.maps.LatLng(latitude,longitude)javascript - how to get latitude longitude from function and send it to new google.maps.LatLng( latitude ,longitude ) javascript 当市场受到拖累时,我该如何提醒Google地图的经纬度? - How do I alert the lat and long of the Google Maps, when the market is dragged? 如何仅使用JavaScript放大Google Maps? - How do I zoom into Google Maps with just javascript? 如何正确格式化Google Maps的JavaScript数组/对象? - How do I properly format this JavaScript Array/Object for Google Maps? 如何使用JavaScript在Google地图上输出geojon点? - How do I output the geojon points on google maps using javascript? 如何控制Google Maps JavaScript上标记的哪一层? - How do I control what layer a marker is on google maps Javascript? 如何使用javascript模拟鼠标单击(在Google Maps中)? - How do I use javascript to simulate a mouse click (in Google Maps)? 如何将用户输入标记添加到我网站上的 Google 地图小部件,并从中获取纬度和经度数据? - How do I add a user-input marker to Google Maps widget on my website, and get the latitude and longitude data from it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM