Ok so I have set up a map and an autocomplete field on a testing site.

好的,我在测试网站上设置了地图和自动填充字段。

My goal is to get the user to input his/her address into the field and then when he/she clicks confirm work out the closest store to them based on the information provided.

我的目标是让用户在现场输入他/她的地址,然后当他/她点击确认根据提供的信息确定离他们最近的商店。

I have the lat/long for each store and with the help of autocomplete I now have the lat/long of the users address but when I use the computeDistanceBetween method provided in the documentation I get NaN instead of a desired result.

我有每个商店的lat / long,并且在自动完成的帮助下,我现在有lat / long的用户地址但是当我使用文档中提供的computeDistanceBetween方法时,我得到NaN而不是期望的结果。

Here is a link to the test site to get an idea - http://dev.touch-akl.com/map/

这是测试网站的链接,以获得一个想法 - http://dev.touch-akl.com/map/

And Below is what I have tried so far

以下是我到目前为止所尝试的内容

    //----------------------------------------------------------------
    //--- Search Box -------------------------------------------------

    var input = document.getElementById('address');
    var $confirm = $('#confirm');

    var options = {
       types: ['geocode'],
       componentRestrictions: {country: 'nz'}//Turkey only
    };        

    var autocomplete = new google.maps.places.Autocomplete(input,options);
    autocomplete.bindTo('bounds', map);

    $confirm.click(function(){

        var _street = $('#address').val();
        var place = autocomplete.getPlace();
        var _coordinates = place.geometry.location.toString();
        var _delivery = _coordinates.substring(1, _coordinates.length - 1);
        var _kCord = '-36.874694,174.735292';
        var _pCord = '-36.858317,174.782284'

        console.log(_coordinates);
        console.log(_delivery);

        console.log(google.maps.geometry.spherical.computeDistanceBetween(_pCord, _delivery));
        console.log(google.maps.geometry.spherical.computeDistanceBetween(_kCord, _delivery));


    });

1 个解决方案

#1


25

google.maps.geometry.spherical.computeDistanceBetween requires that you feed it two google.maps.LatLng objects whereas you are using strings, and that won't work. The following should...

google.maps.geometry.spherical.computeDistanceBetween要求您提供两个google.maps.LatLng对象,而您正在使用字符串,这将无法正常工作。以下应该......

$confirm.click(function(){
    var _street = $('#address').val();
    var place = autocomplete.getPlace();
    var _coordinates = place.geometry.location; //a google.maps.LatLng object
    var _kCord = new google.maps.LatLng(-36.874694, 174.735292);
    var _pCord = new google.maps.LatLng(-36.858317, 174.782284);

    console.log(_coordinates);

    console.log(google.maps.geometry.spherical.computeDistanceBetween(_pCord, _coordinates));
    console.log(google.maps.geometry.spherical.computeDistanceBetween(_kCord, _coordinates));
});

更多相关文章

  1. Python学习之——Tcp/ip基础/IP地址/DNS/端口简介
  2. 【python coding 1:网络检测】ping本地文件里的ip地址
  3. python正则表达式匹配时间和IP地址
  4. Python——域名解析成IP地址
  5. 套接字错误“IP地址在其上下文中无效” - Python
  6. python中查看变量内存地址的方法
  7. CentOS 7下配置IP地址
  8. 如果后台的SVN服务器IP地址更改了,如何修改客户端的连接url呢?
  9. Linux 修改ip地址

随机推荐

  1. php连接数据库的步骤
  2. 使用PHP简单实现类似“畅言”等评论系统
  3. php怎么放大字体
  4. PHP高效生成一个不重复随机数
  5. php无限极分类原理
  6. php怎么分页
  7. 关于php mysqli函数的一些总结和详细介绍
  8. PHP 实现 Snowflake 生成分布式唯一 ID
  9. 关于php mysqli函数的一些总结及实例(三)
  10. 关于php mysqli函数的一些总结和实例(四)