आप ऐसा कर सकते हैं का उपयोग कर प्रतिक्रिया देशी जियोलोकेशन:
https://facebook.github.io/react-native/docs/geolocation.html
वहां पहले से ही एक अच्छा उदाहरण आप ऐसा कर सकते हैं कि कैसे पर है:
componentDidMount: function() {
navigator.geolocation.getCurrentPosition(
(position) => {
var initialPosition = JSON.stringify(position);
this.setState({initialPosition});
},
(error) => alert(error.message),
{enableHighAccuracy: true, timeout: 20000, maximumAge: 1000}
);
this.watchID = navigator.geolocation.watchPosition((position) => {
var lastPosition = JSON.stringify(position);
this.setState({lastPosition});
});
}
आप अब प्रारंभिक स्थिति (अक्षांश और देशांतर) है, अपने हार्ड कोडित मूल्य के बजाय इसका उपयोग करें। जब भी स्थान बदलता है तो घड़ीपोत सफलता कॉलबैक का आह्वान करता है।
मैं वर्तमान में अपने ऐप में स्थानीय रेस्तरां खोजने के लिए इसका उपयोग कर रहा हूं।
अपडेट किया गया उत्तर:
मैं इस चलाने के लिए Genymotion एमुलेटर उपयोग कर रहा हूँ। यदि आप इस कोड को जेनमोशन पर भी चलाने जा रहे हैं, तो कृपया सुनिश्चित करें कि आपने Google Play समर्थन इंस्टॉल किया है, अन्यथा प्रतिक्रिया-मूल-मानचित्र काम नहीं करेंगे। यदि आप भौतिक उपकरण का उपयोग कर रहे हैं तो आपको यह कदम नहीं करना है।
https://github.com/codepath/android_guides/wiki/Genymotion-2.0-Emulators-with-Google-Play-support
जियोलोकेशन के बारे में, सुनिश्चित करें कि आप AndroidManifest.xml में ACCESS_FINE_LOCATION अनुमति जोड़ लिया है कि सुनिश्चित करें:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
नहीं तो आप एक त्रुटि प्राप्त होगी: "ऐसा लगता एप्लिकेशन की तरह 'नहीं करता है टी को स्थान तक पहुंचने की अनुमति नहीं है। "
कृपया यह भी सुनिश्चित करें कि आपका जीपीएस/वाईफ़ाई/डेटा सक्षम है, अन्यथा आपका डिवाइस आपके वर्तमान स्थान को पुनर्प्राप्त नहीं कर सकता है।
var React = require('react');
var ReactNative = require('react-native');
var {
StyleSheet,
PropTypes,
View,
Text,
Dimensions,
TouchableOpacity,
} = ReactNative;
var MapView = require('react-native-maps');
var { width, height } = Dimensions.get('window');
const ASPECT_RATIO = width/height;
// (Initial Static Location) Mumbai
const LATITUDE = 19.0760;
const LONGITUDE = 72.8777;
const LATITUDE_DELTA = 0.01;
const LONGITUDE_DELTA = LATITUDE_DELTA * ASPECT_RATIO;
var CustomMap = React.createClass({
getInitialState() {
return {
region: {
latitude: LATITUDE,
longitude: LONGITUDE,
latitudeDelta: LATITUDE_DELTA,
longitudeDelta: LONGITUDE_DELTA
},
};
},
componentDidMount: function() {
navigator.geolocation.getCurrentPosition(
(position) => {
this.setState({
region: {
latitude: position.coords.latitude,
longitude: position.coords.longitude,
latitudeDelta: LATITUDE_DELTA,
longitudeDelta: LONGITUDE_DELTA
}
});
},
(error) => alert(error.message),
{enableHighAccuracy: true, timeout: 20000, maximumAge: 1000}
);
this.watchID = navigator.geolocation.watchPosition((position) => {
const newRegion = {
latitude: position.coords.latitude,
longitude: position.coords.longitude,
latitudeDelta: LATITUDE_DELTA,
longitudeDelta: LONGITUDE_DELTA
}
this.onRegionChange(newRegion);
});
},
componentWillUnmount: function() {
navigator.geolocation.clearWatch(this.watchID);
},
onRegionChange(region) {
this.setState({ region });
},
render() {
return (
<View style={styles.container}>
<MapView
ref="map"
mapType="terrain"
style={styles.map}
region={this.state.region}
onRegionChange={this.onRegionChange}
>
</MapView>
<View style={styles.bubble}>
<Text style={{ textAlign: 'center'}}>
{`${this.state.region.latitude.toPrecision(7)}, ${this.state.region.longitude.toPrecision(7)}`}
</Text>
</View>
</View>
);
},
});
var styles = StyleSheet.create({
container: {
position: 'absolute',
top: 0,
left: 0,
right: 0,
bottom: 0,
justifyContent: 'flex-end',
alignItems: 'center',
},
map: {
position: 'absolute',
top: 0,
left: 0,
right: 0,
bottom: 0,
},
bubble: {
backgroundColor: 'rgba(255,255,255,0.7)',
paddingHorizontal: 18,
paddingVertical: 12,
borderRadius: 20,
},
});
module.exports = CustomMap;
ऊपर कोड चल रहा है, आप पाएँगे कि आपका मूल स्थिर स्थान (मुंबई) जब getCurrentPosition कहा जाता है componentDidMount के माध्यम से डिवाइस से अपने वर्तमान स्थान के साथ ओवरराइट है।
आप, स्थिति में परिवर्तन ट्रैक onRegionChange के साथ संयोजन में watchPosition का उपयोग करें, अन्यथा अगर आप सिर्फ प्रारंभिक स्थिति watchPosition हटाने कदम प्राप्त करने की आवश्यकता की जरूरत है।
हे, मैंने अपना कोड अपडेट किया है, आप इसे प्रश्न के रूप में देख सकते हैं लेकिन सही स्थान अभी भी – atif
प्रतिपादन नहीं कर रहा है क्या आप जीनमोशन का उपयोग कर रहे हैं?क्या आपने सुनिश्चित किया है कि आपका जीपीएस सक्षम है? क्या आपने यह सुनिश्चित करने के लिए कंसोल लॉगिंग स्थिति की कोशिश की है कि CurrentPosition एक वैध स्थिति देता है? मैंने यह भी देखा कि आप अपने getCurrentPosition कॉल में त्रुटियों को संभालने नहीं कर रहे हैं। "(त्रुटि) => अलर्ट (error.message) जोड़ने का प्रयास करें," यह देखने के लिए कि क्या आपको कोई त्रुटि मिल रही है या नहीं। – Antoni4
मैं परीक्षण करने के लिए भौतिक उपकरण का उपयोग कर रहा हूँ। मैंने त्रुटि भाग को हटाकर इसे जोड़ने की कोशिश की है लेकिन कोई सफलता नहीं है। क्या मेरा कोड सही है? – atif