我正在尝试使用 react-native-maps 模块制作一个应用程序。 我可以通过 navigator.geolocation.watchPosition 获取设备位置,我还使用内置于 showUserLocation 属性中的 map 。 我怎样才能实现加载时的 map 会转到用户位置。看不到整个世界,也看不到硬编码的初始位置。
编辑: 这是我的 react-native-map 元素。该区域设置为硬编码初始 位置,我想将其更改为始终在用户位置加载。
<View style={styles.container}>
<MapView
style={styles.map}
region={this.state.initialPosition}
showsMyLocationButton={true}
loadingEnabled={true}
onRegionChange={(region)=> this.setState({ initialPosition:region })}
onLongPress={this.mapOnPress}
showsUserLocation={true}
>
我在应用程序中使用的位置来自这里,但加载很晚,通常超过 20 秒,所以我想跳过这 20 秒并立即加载用户位置。
this.watchID= navigator.geolocation.watchPosition((position)=>{
var lastRegion ={
latitude: lat,
longitude: lon,
longitudeDelta: LON_D,
latitudeDelta: LAT_D
}
this.setState({initialPosition:lastRegion})},(error)=>alert(error),{enableHighAccuracy:true,timeout:20,maximumAge:10})
有什么解决办法吗?感谢您的帮助:)
最佳答案
你可以试试这个,超时单位是毫秒。在RN中测试:44,
class Map extends Component {
constructor(props) {
super(props);
this.state = {
permissionState: false,
latitude: null,
longitude: null,
error: null,
};
}
componentDidMount() {
Platform.OS === 'android' && Platform.Version >= 23 ? this.requestMapPermission() : this.requestMap()
}
async requestMapPermission() {
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION)
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
console.log('Granted');
this.watchId = navigator.geolocation.getCurrentPosition(
(position) => {
console.log('Position is watched');
this.setState({
permissionState: true,
latitude: position.coords.latitude,
longitude: position.coords.longitude,
error: null,
});
},
(error) => this.setState({error: error.message}),
{enableHighAccuracy: false, timeout: 20000, maximumAge: 1000},
);
} else {
console.log('not Granted');
this.setState({
permissionState: false,
});
}
} catch (err) {
console.warn(err)
}
}
requestMap() {
this.watchId = navigator.geolocation.watchPosition(
(position) => {
this.setState({
permissionState: true,
latitude: position.coords.latitude,
longitude: position.coords.longitude,
error: null,
});
},
(error) => this.setState({error: error.message, permissionState: false,}),
{enableHighAccuracy: false, timeout: 20000, maximumAge: 1000},
);
}
componentWillUnmount() {
navigator.geolocation.clearWatch(this.watchID);
}
render() {
var {height, width} = Dimensions.get('window');
return (
<View style={{height: 150, justifyContent: 'center'}}>
{
this.state.permissionState === true ?
<MapView
minZoomLevel={16}
style={{height: 150, marginBottom: 5, marginTop: 5}}
region={{
latitude: this.state.latitude,
longitude: this.state.longitude,
latitudeDelta: 0.0922,
longitudeDelta: 0.0421
}}>
<MapView.Marker
coordinate={{
latitude: (this.state.latitude + 0.00000),
longitude: (this.state.longitude + 0.00000),
}}>
<View>
<Icon name="place" size={40} color="#038FC0"/>
</View>
</MapView.Marker>
</MapView> :
<View style={{
borderWidth: 1,
borderColor: '#6f6f6f',
height: 150,
justifyContent: 'center',
alignItems: 'center',
}}>
<Text>No Permission for location</Text>
</View>
}
</View>
);
}
}
const styles = StyleSheet.create({
map: {
...StyleSheet.absoluteFillObject,
}
});
export default Map;
关于android - React Native map 加载位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47563986/
鉴于我有以下迁移:Sequel.migrationdoupdoalter_table:usersdoadd_column:is_admin,:default=>falseend#SequelrunsaDESCRIBEtablestatement,whenthemodelisloaded.#Atthispoint,itdoesnotknowthatusershaveais_adminflag.#Soitfails.@user=User.find(:email=>"admin@fancy-startup.example")@user.is_admin=true@user.save!ende
我收到这个错误:RuntimeError(自动加载常量Apps时检测到循环依赖当我使用多线程时。下面是我的代码。为什么会这样?我尝试多线程的原因是因为我正在编写一个HTML抓取应用程序。对Nokogiri::HTML(open())的调用是一个同步阻塞调用,需要1秒才能返回,我有100,000多个页面要访问,所以我试图运行多个线程来解决这个问题。有更好的方法吗?classToolsController0)app.website=array.join(',')putsapp.websiteelseapp.website="NONE"endapp.saveapps=Apps.order("
我一直致力于让我们的Rails2.3.8应用程序在JRuby下正确运行。一切正常,直到我启用config.threadsafe!以实现JRuby提供的并发性。这导致lib/中的模块和类不再自动加载。使用config.threadsafe!启用:$rubyscript/runner-eproduction'pSim::Sim200Provisioner'/Users/amchale/.rvm/gems/jruby-1.5.1@web-services/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:105:in`co
我们目前正在为ROR3.2开发自定义cms引擎。在这个过程中,我们希望成为我们的rails应用程序中的一等公民的几个类类型起源,这意味着它们应该驻留在应用程序的app文件夹下,它是插件。目前我们有以下类型:数据源数据类型查看我在app文件夹下创建了多个目录来保存这些:应用/数据源应用/数据类型应用/View更多类型将随之而来,我有点担心应用程序文件夹被这么多目录污染。因此,我想将它们移动到一个子目录/模块中,该子目录/模块包含cms定义的所有类型。所有类都应位于MyCms命名空间内,目录布局应如下所示:应用程序/my_cms/data_source应用程序/my_cms/data_ty
最近因为项目需要,需要将Android手机系统自带的某个系统软件反编译并更改里面某个资源,并重新打包,签名生成新的自定义的apk,下面我来介绍一下我的实现过程。APK修改,分为以下几步:反编译解包,修改,重打包,修改签名等步骤。安卓apk修改准备工作1.系统配置好JavaJDK环境变量2.需要root权限的手机(针对系统自带apk,其他软件免root)3.Auto-Sign签名工具4.apktool工具安卓apk修改开始反编译本文拿Android系统里面的Settings.apk做demo,具体如何将apk获取出来在此就不过多介绍了,直接进入主题:按键win+R输入cmd,打开命令窗口,并将路
我需要一个非常简单的字符串验证器来显示第一个符号与所需格式不对应的位置。我想使用正则表达式,但在这种情况下,我必须找到与表达式相对应的字符串停止的位置,但我找不到可以做到这一点的方法。(这一定是一种相当简单的方法……也许没有?)例如,如果我有正则表达式:/^Q+E+R+$/带字符串:"QQQQEEE2ER"期望的结果应该是7 最佳答案 一个想法:你可以做的是标记你的模式并用可选的嵌套捕获组编写它:^(Q+(E+(R+($)?)?)?)?然后你只需要计算你获得的捕获组的数量就可以知道正则表达式引擎在模式中停止的位置,你可以确定匹配结束
我将Cucumber与Ruby结合使用。通过Selenium-Webdriver在Chrome中运行测试时,我想将下载位置更改为测试文件夹而不是用户下载文件夹。我当前的chrome驱动程序是这样设置的:Capybara.default_driver=:seleniumCapybara.register_driver:seleniumdo|app|Capybara::Selenium::Driver.new(app,:browser=>:chrome,desired_capabilities:{'chromeOptions'=>{'args'=>%w{window-size=1920,1
我想在heroku.com上查看我的应用程序日志的内容,所以我关注了thisexcellentadvice并拥有我所有的日志内容。但是我现在很想知道我的日志文件实际在哪里,因为“log/production.log”似乎是空的:C:\>herokuconsoleRubyconsoleforajpbrevx.heroku.com>>files=Dir.glob("*")=>["public","tmp","spec","Rakefile","doc","config.ru","app","config","lib","README","Gemfile.lock","vendor","sc
这应该是一个简单的问题,但我找不到任何相关信息。给定一个Ruby中的正则表达式,对于每个匹配项,我需要检索匹配的模式$1、$2,但我还需要匹配位置。我知道=~运算符为我提供了第一个匹配项的位置,而string.scan(/regex/)为我提供了所有匹配模式。如果可能,我需要在同一步骤中获得两个结果。 最佳答案 MatchDatastring.scan(regex)do$1#Patternatfirstposition$2#Patternatsecondposition$~.offset(1)#Startingandendingpo
如何只加载map边界内的标记gmaps4rails?当然,在平移和/或缩放后加载新的。与此直接相关的是,如何获取map的当前边界和缩放级别? 最佳答案 我是这样做的,我只在用户完成平移或缩放后替换标记,如果您需要不同的行为,请使用不同的事件监听器:在你看来(index.html.erb):{"zoom"=>15,"auto_adjust"=>false,"detect_location"=>true,"center_on_user"=>true}},false,true)%>在View的底部添加:functiongmaps4rail