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

TIM*_*MEX 2 html javascript css api google-maps

如您所见,那里有一条线路可以发出警报.这是" undefined".然而,如果我只是提醒results[0].geometry.location,它会发出警报(41.321, 41.556).

我只是想提醒它并将它(作为一个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;
    });
Run Code Online (Sandbox Code Playgroud)

ani*_*iri 33

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


Nic*_*ver 6

如果你看一下API,location不是一个数组(这里是主要的问题),它是一个属性,.location访问它的正确方法也是如此,而不是.location[0].

官方文档有点难以找到特定类型,但这里有更新的文档location(一种LatLng类型):

toString()是你在警报中获得的(lat, long),还有.lat()纬度和.lng()经度,如下所示:

alert(results[0].geometry.location.lat());
alert(results[0].geometry.location.lng());
Run Code Online (Sandbox Code Playgroud)