Google Maps API返回“未定义”

T先生

我正在向Google Maps Geocoding API查询字符串。我在控制台中看到一个返回,因此我知道我已正确连接,但是if语句中的内容返回“ Undefined”。关于为什么会这样的任何想法?

$(document).ready(function(){

      // Search Submit  
      $("#search").submit(function(event){
            event.preventDefault("", function(){
                  //
                  });

            name = $("#search-name").val();
            console.log("Search term at submit is: "+name);

            // Send to Google Geocoding API

            var geocoder = new google.maps.Geocoder();

            geocoder.geocode({ 
                        address : name
                  }, function(results, status){

                        if (status == google.maps.GeocoderStatus.OK) {

                              console.log("results after query: "+ results.formatted_address + "\n" + results.length + " QTY Posts");
                  }

            );
      });
});
埃米尔·马克斯(Emir Marques)

结果是数组不是对象。对于对象,请使用position或使用foreach读取所有返回值。

尝试将results.formatted_address更改为results [0] .formatted_address。

var map;
var addressField;
var geocoder;

$(document).ready(function () {
    // Define map options
    var mapOptions = {
        center: new google.maps.LatLng(57.698254, 12.037024),
        zoom: 16,
        mapTypeId: google.maps.MapTypeId.HYBRID,
        panControl: true,
        zoomControl: true,
        mapTypeControl: true,
        scaleControl: true,
        streetViewControl: true,
        overviewMapControl: true
    };

    // Define map
    map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);

    // Define Gecoder
    geocoder = new google.maps.Geocoder();

    // Init searchbox
    initSearchBox();
});

function initSearchBox() {
    // Add searchbox
    var searchControlDiv = document.createElement('div');
    var searchControl = new SearchControl(searchControlDiv, map);

    searchControlDiv.index = 1;
    map.controls[google.maps.ControlPosition.TOP_CENTER].push(searchControlDiv);
}

function SearchControl(controlDiv, map) {
    // Set CSS styles for the DIV containing the control
    // Setting padding to 5 px will offset the control
    // from the edge of the map.
    controlDiv.style.padding = '5px';

    // Set CSS for the control border.
    var controlUI = document.createElement('div');
    controlUI.style.backgroundColor = 'white';
    controlUI.style.borderStyle = 'solid';
    controlUI.style.borderWidth = '2px';
    controlUI.style.cursor = 'pointer';
    controlUI.style.textAlign = 'center';
    controlUI.title = 'Sök ex: gatunamn, stad';
    controlDiv.appendChild(controlUI);

    // Create the search box
    var controlSearchBox = document.createElement('input');
    controlSearchBox.id = 'search_address';
    controlSearchBox.size = '80';
    controlSearchBox.type = 'text';

    // Initiat autocomplete
    $(function () {
        $(controlSearchBox).autocomplete({
            source: function (request, response) {

                if (geocoder == null) {
                    geocoder = new google.maps.Geocoder();
                }

                geocoder.geocode({
                    'address': request.term
                }, function (results, status) {
                    if (status == google.maps.GeocoderStatus.OK) {
                        var searchLoc = results[0].geometry.location;
                        var lat = results[0].geometry.location.lat();
                        var lng = results[0].geometry.location.lng();
                        var latlng = new google.maps.LatLng(lat, lng);
                        var bounds = results[0].geometry.bounds;
						
                        console.log(results)
                        
                        geocoder.geocode({
                            'latLng': latlng
                        }, function (results1, status1) {
                            if (status1 == google.maps.GeocoderStatus.OK) {
                                if (results1[1]) {
                                    response($.map(results1, function (loc) {
                                        return {
                                            label: loc.formatted_address,
                                            value: loc.formatted_address,
                                            bounds: loc.geometry.bounds
                                        }
                                    }));
                                }
                            }
                        });
                    }
                });
            },
            select: function (event, ui) {
                var pos = ui.item.position;
                var lct = ui.item.locType;
                var bounds = ui.item.bounds;

                if (bounds) {
                    map.fitBounds(bounds);
                }
            }
        });
    });

    // Set CSS for the control interior.
    var controlText = document.createElement('div');
    controlText.style.fontFamily = 'Arial,sans-serif';
    controlText.style.fontSize = '12px';
    controlText.style.paddingLeft = '4px';
    controlText.style.paddingRight = '4px';
    controlText.appendChild(controlSearchBox);
    controlUI.appendChild(controlText);
}
html {
    height: 100%
}
body {
    height: 100%;
    margin: 0;
    padding: 0
}
#map_canvas {
    height: 100%
}
<link rel="stylesheet" type="text/css" href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
<div id="map_canvas" style="width:100%; height:100%;"></div>

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Google Maps-从未定义的函数返回getPosition

来自分类Dev

Google Maps API 3-类型错误:a未定义

来自分类Dev

Google Maps API gm_bindings_未定义

来自分类Dev

Google Maps API gm_bindings_未定义

来自分类Dev

Google Maps api标记未定义错误

来自分类Dev

google.maps.places未定义

来自分类Dev

Google Maps Geocoder未定义

来自分类Dev

未定义Google Maps功能

来自分类Dev

Google Maps Geocoder未定义

来自分类Dev

为什么Google Maps API中未定义我的Maps对象

来自分类Dev

Google Maps Api v3标记。Google未定义

来自分类Dev

很难让Google Maps在Ionic中显示-“未定义Google”

来自分类Dev

Google Maps API v3 Heatmaps错误:“无法读取未定义的属性'HeatmapLayer'”

来自分类Dev

Google Maps API JS V3:infowindow.getPosition()==未定义?

来自分类Dev

Google Maps JS API(v3)InfoWindow脚本错误-JSON未定义

来自分类Dev

Google Maps API(使用gmapsjs)时,setCenter抛出“未定义不是函数”吗?

来自分类Dev

Google Maps api V3,添加ajax后,这在getCenter()上是未定义的错误

来自分类Dev

Google Maps API 3中未定义标记的javascript数组

来自分类Dev

为什么在使用 Google Maps API 时全局设置的 JS 变量显示为未定义?

来自分类Dev

ReferenceError:使用angular-google-maps时未定义_

来自分类Dev

Google Maps集成中的“未定义rd”错误

来自分类Dev

带有Dart的Google Maps:保留功能未定义

来自分类Dev

Google Maps集成中的“未定义rd”错误

来自分类Dev

Google Maps API V3地方信息库无法读取未定义的属性“位置”

来自分类Dev

使用angular2-google-maps和google places搜索栏未定义google对象

来自分类Dev

Google Maps API RefererNotAllowedError

来自分类Dev

Google Maps API DeletedApiProjectMapError

来自分类Dev

Google Maps Api许可

来自分类Dev

Google Maps Api

Related 相关文章

  1. 1

    Google Maps-从未定义的函数返回getPosition

  2. 2

    Google Maps API 3-类型错误:a未定义

  3. 3

    Google Maps API gm_bindings_未定义

  4. 4

    Google Maps API gm_bindings_未定义

  5. 5

    Google Maps api标记未定义错误

  6. 6

    google.maps.places未定义

  7. 7

    Google Maps Geocoder未定义

  8. 8

    未定义Google Maps功能

  9. 9

    Google Maps Geocoder未定义

  10. 10

    为什么Google Maps API中未定义我的Maps对象

  11. 11

    Google Maps Api v3标记。Google未定义

  12. 12

    很难让Google Maps在Ionic中显示-“未定义Google”

  13. 13

    Google Maps API v3 Heatmaps错误:“无法读取未定义的属性'HeatmapLayer'”

  14. 14

    Google Maps API JS V3:infowindow.getPosition()==未定义?

  15. 15

    Google Maps JS API(v3)InfoWindow脚本错误-JSON未定义

  16. 16

    Google Maps API(使用gmapsjs)时,setCenter抛出“未定义不是函数”吗?

  17. 17

    Google Maps api V3,添加ajax后,这在getCenter()上是未定义的错误

  18. 18

    Google Maps API 3中未定义标记的javascript数组

  19. 19

    为什么在使用 Google Maps API 时全局设置的 JS 变量显示为未定义?

  20. 20

    ReferenceError:使用angular-google-maps时未定义_

  21. 21

    Google Maps集成中的“未定义rd”错误

  22. 22

    带有Dart的Google Maps:保留功能未定义

  23. 23

    Google Maps集成中的“未定义rd”错误

  24. 24

    Google Maps API V3地方信息库无法读取未定义的属性“位置”

  25. 25

    使用angular2-google-maps和google places搜索栏未定义google对象

  26. 26

    Google Maps API RefererNotAllowedError

  27. 27

    Google Maps API DeletedApiProjectMapError

  28. 28

    Google Maps Api许可

  29. 29

    Google Maps Api

热门标签

归档