2014-07-19 13 views
7

मैं एक meteor.js nuub हूँ, लेकिन मैंने इसे समझने की कोशिश करने में काफी समय बिताया।Meteor.js प्रतिक्रियाशील एचटीएमएल 5 भौगोलिक स्थिति position.coords

मैं getCurrentPosition में पास किए गए HTML5 भौगोलिक स्थान API कॉलबैक के आधार पर अपने UI को प्रतिक्रियात्मक रूप से अपडेट करने का प्रयास कर रहा हूं। हालांकि, यूआई मेरे वर्तमान कोड के साथ अद्यतन नहीं कर रहा है।

क्या आप सुझाव और/या समाधान प्रदान कर सकते हैं?

मूल बातें:: उल्का सर्वर चल रहा है, अन्य डेटा को सफलतापूर्वक संग्रह के माध्यम से मोंगो से/की सेवा

मैं एक मुख्य पृष्ठ है (main.html):

<head> 
    <title>Geolocation</title> 
</head> 
<body> 
<div class="container"> 
    <header class="navbar"> 
    <div class="navbar-inner"> 
     <a class="brand" href="/">Geolocation</a> 
    </div> 
    </header> 
    <div id="locationDemo"> 
    {{> location}} 
    </div> 
</div> 
</body> 

कि संदर्भ यहाँ विवरण हैं एक टेम्पलेट (location.js):

<template name="location"> 
    <div> 
    Lat: {{lat}} 
    </div> 
    <div> 
    Lon: {{lon}} 
    </div> 
</template> 

इस जुड़े सहायक है (location.js):

_lat = { 
    current: 0, 
    dep: new Deps.Dependency, 
    get: function(){ 
    this.dep.depend(); 

    if(this.current === 0){ 
     getLocation(); 
    } 

    return this.current; 
    }, 
    set: function(value){ 
    this.current = value; 
    this.dep.changed(); 
    Deps.flush(); 
    return this.current; 
    } 
}; 

_lon = { 
    current: 0, 
    dep: new Deps.Dependency, 
    get: function(){ 
    this.dep.depend(); 

    if(this.current === 0){ 
     getLocation(); 
    } 

    return this.current; 
    }, 
    set: function(value){ 
    this.current = value; 
    this.dep.changed(); 
    Deps.flush(); 
    return this.current; 
    } 
}; 

function getLocation(){ 
    if (navigator.geolocation) 
    { 
    navigator.geolocation.getCurrentPosition(showPosition, showError); 
    } 
    else{ 
    console.log("Geolocation is not supported by this browser."); 
    } 
} 

function showPosition(position) 
{ 
    _lat.set(position.coords.latitude); 
    _lon.set(position.coords.longitude); 
} 

function showError(error) { 
    switch(error.code) { 
    case error.PERMISSION_DENIED: 
     console.log("User denied the request for Geolocation."); 
     break; 
    case error.POSITION_UNAVAILABLE: 
     console.log("Location information is unavailable."); 
     break; 
    case error.TIMEOUT: 
     console.log("The request to get user location timed out."); 
     break; 
    case error.UNKNOWN_ERROR: 
     console.log("An unknown error occurred."); 
     break; 
    } 
} 

Template.location.helpers({ 
    lat: _lat.get(), 
    lon: _lon.get() 
}); 
+0

पर जियोलोकेशन पैकेज उपलब्ध जोड़ने के लिए की जरूरत है कि आप इसे उम्मीद कर रहे हैं अद्यतन करने के लिए जब 'navigator.geolocation.getCurrentPosition' बदलता है? यदि ऐसा है, तो आपको शायद 'Deps.autorun' की आवश्यकता होगी, लेकिन फिर भी मुझे यकीन नहीं है कि भौगोलिक स्थान का उपयोग प्रतिक्रियाशील डेटा स्रोत के रूप में किया जा सकता है। –

+0

अरे क्रिस्टियन, हाँ, मैंने सोचा था कि _lat और _lon प्रतिक्रियाशील युद्धों के रूप में उपयोग किया जा सकता है। मैं Deps.autorun सोच रहा था, लेकिन मुझे यकीन नहीं है कि इसे कहां कॉल करें। –

उत्तर

4

जहाँ तक मुझे पता है कि navigator.geolocation एक प्रतिक्रियाशील डेटा स्रोत नहीं है। तो यह कुछ स्पष्ट मतदान के बिना काम नहीं करेगा। एक और चीज जिसे आपने गलत पाया है वह है कि आपके सहायक काम नहीं कर रहे हैं, इसलिए उन्हें बार-बार नहीं कहा जा सकता था।

यह काम हो सकता है (परीक्षण नहीं):

Meteor.setInterval(function() { 
    navigator.geolocation.getCurrentPosition(function(position) { 
     Session.set('lat', position.coords.latitude); 
     Session.set('lon', position.coords.longitude); 
    }); 
}, 5000); 

Template.location.helpers({ 
    lat: function() { return Session.get('lat'); }, 
    lon: function() { return Session.get('lon'); } 
}); 
+0

फिर से धन्यवाद ईसाई। मैं केवल एक बार स्थान मांग रहा था (गेटटर में 'अगर' कथन), लेकिन मुझे मतदान विचार पसंद है। हालांकि, मैं _lat.get() प्रतिक्रियाशील स्रोत बनाने के लिए देख रहा था, भौगोलिक स्थान एपीआई नहीं। जिओलोकेशन काम कर रहा है, सिर्फ डेप सामान नहीं। –

+0

2 ले लो: ईसाई, आप बिल्कुल सही थे! जो मैं खो रहा था वह टेम्पलेट का _lat और _lon ऑब्जेक्ट्स पर बाध्यकारी है - जिसे स्लाव किम (उल्का Google समूह) द्वारा भी इंगित किया गया था। यह अब पूरी तरह से काम कर रहा है। –

1

जियोलोकेशन देशी iOS एप्लिकेशन के रूप में संकलित उल्का क्षुधा में काम करने के लिए प्राप्त करने के लिए, मैं https://atmospherejs.com/mdg/geolocation

संबंधित मुद्दे