【问题标题】:Get a user's geolocation in Meteor app在 Meteor 应用中获取用户的地理位置
【发布时间】:2016-08-22 07:19:40
【问题描述】:

现在我正在开发一个严重依赖用户地理位置的应用程序。此时我只需要通过这个用户的经纬度得到一个用户的地址。为此,我使用了两个包:mdg:geolocation(获取纬度和经度)和meteor-google-reverse-geocode(将纬度和经度转换为地址)。 现在我有以下代码:

import React, { Component, PropTypes } from 'react';
import { Meteor } from 'meteor/meteor';
import { Accounts } from 'meteor/accounts-base';
import { Geolocation } from 'meteor/mdg:geolocation';
import { reverseGeocode } from 'meteor/jaymc:google-reverse-geocode';

export default class Profile extends Component {
    setLocation() {
        var latLng = Geolocation.latLng();
        var lat = latLng.lat;
        var lng = latLng.lng;
        reverseGeocode.getSecureLocation(lat, lng, function(location) {
            Meteor.users.update(Meteor.userId(), {
                $set: {"profile.location": reverseGeocode.getAddrStr()}
            });
        });
    }
    render() {
        return (
            <div className="container">
                <div className="row">
                    <div className="col s4">
                        <User />
                    </div>
                    <button onClick={this.setLocation.bind(this)}>Set location</button>
                </div>
    </div>
        )
    }
}

我有一个User 组件,它在其他参数中显示用户的位置,但首先它应该从注册和登录中获取这个location 参数(稍后我将实现会话),所以我创建了一个按钮做的伎俩,但只有当我点击它两次。第一次点击它没有得到任何信息,而是在控制台中显示一个错误,但是当我第二次点击它时,它以某种方式按我想要的方式工作,即获取正确的地址,然后在我的 User 组件中显示它。好的,它可以抓取数据但只有当我点击它两次这是不可接受的,因为我想在注册或登录后抓取这些数据,即第一次点击,而不是第二次点击,但我仍然不知道如何去做吧。任何帮助或想法将不胜感激。感谢您花时间解决所描述的问题!

UPD

经过一番研究,我发现Geolocation 函数不是响应式的,加载数据可能需要一些时间,因此我使用ReactiveVar 使其具有响应性,并使用Tracker 在计算完成后手动停止计算。现在我的代码如下所示:

setLocation() {
    var latLng = new ReactiveVar();
    Tracker.autorun(function(computation) {
        latLng.set(Geolocation.latLng());
        if (latLng.get()) {
            computation.stop();
            console.log(latLng);
            var lat = latLng.lat;
            var lng = latLng.lng;
            reverseGeocode.getSecureLocation(lat, lng, function(location) {
                Meteor.users.update(Meteor.userId(), {
                    $set: {"profile.location": reverseGeocode.getAddrStr()}
                });
            });
        }
    })
}

它通过第一次点击计算 lanlng 属性,但在 google-reverse-geocode.js 文件中出现另一个错误 Uncaught TypeError: Cannot read property 'formatted_address' of undefined

【问题讨论】:

    标签: meteor geolocation


    【解决方案1】:

    好的,我解决了这个问题。我查看了ReactiveVar 输出并意识到我的错误是什么。看,输出包含对象curValue,其中存储了lanlng 属性,因此要提取这些属性,我只需像latLng.curValue.lat 一样显式调用它们。这是我在第一篇文章中描述的工作方式的固定函数:

    setLocation() {
        var latLng = new ReactiveVar();
        Tracker.autorun(function(computation) {
            latLng.set(Geolocation.latLng());
            if (latLng.get()) {
                computation.stop();
                console.log(latLng);
                var lat = latLng.curValue.lat;
                var lng = latLng.curValue.lng;
                reverseGeocode.getSecureLocation(lat, lng, function(location) {
                    Meteor.users.update(Meteor.userId(), {
                        $set: {"profile.location": reverseGeocode.getAddrStr()}
                    });
                });
            }
        })
    }
    

    希望对某人有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-18
      • 1970-01-01
      • 2011-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-28
      相关资源
      最近更新 更多