Google商家信息自动填充API多个实例 [英] Google Places Autocomplete API Multiple Instances

查看:80
本文介绍了Google商家信息自动填充API多个实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网站出了问题。在提交表单之前,我使用自动完成功能查找地址的长度。问题是,在我有2个实例的页面上,我在Chrome中遇到错误'geometry'为null或者不是对象,第一个实例不会得到latlon。

I've got a problem with my site. I use autocomplete to find the lat long of an address before submitting to a form. The problem is, on the page where I have 2 instances, I get an error in Chrome "'geometry' is null or not an object" and the first instance will not get the latlon.

if($('input#searchArea').length){
        var input = document.getElementById('searchArea');
        var options = {
            componentRestrictions: {country: 'uk'}
        }
        var autocomplete = new google.maps.places.Autocomplete(input, options);

        google.maps.event.addListener(autocomplete, 'place_changed', function(){
            var place = autocomplete.getPlace();
            var location = String(place.geometry.location); // The problemed line
            var latLong = location.substr(1, location.length-2);
            latLong = latLong.split(', ');
            if($('input#searchLatLong').length == 0) $('form#findArea').append('<input type="hidden" name="ll" id="searchLatLong" />');
            $('input#searchLatLong').val(latLong[0] + ',' + latLong[1]);
        });
    }

    if($('input#letArea').length){
        var input = document.getElementById('letArea');
        var options = {
            componentRestrictions: {country: 'uk'}
        }
        autocomplete = new google.maps.places.Autocomplete(input, options);

        google.maps.event.addListener(autocomplete, 'place_changed', function(){
            var place = autocomplete.getPlace();
            var location = String(place.geometry.location);
            var latLong = location.substr(1, location.length-2);
            latLong = latLong.split(', ');
            if($('input#letLatLong').length == 0) $('form#letPlaceArea').append('<input type="hidden" name="ll" id="letLatLong" />');
            $('input#letLatLong').val(latLong[0] + ',' + latLong[1]);
        });
    }

有没有人遇到这个?

干杯,
RJ

Cheers, RJ

编辑:发现问题。变量被命名为相同。在第二个实例上更改它们对它进行了排序。

Problem found. It was the variables being named the same. Changing them on the second instance sorted it.

干杯,
RJ

Cheers, RJ

推荐答案

发现问题。变量被命名为相同。在第二个实例上更改它们对它进行了排序。

Problem found. It was the variables being named the same. Changing them on the second instance sorted it.

这篇关于Google商家信息自动填充API多个实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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