From ba35ae10d3e5fc86880347bd11a545d96fe7e3c9 Mon Sep 17 00:00:00 2001 From: harukin-DeskMini Date: Wed, 20 Apr 2022 09:27:13 +0900 Subject: [PATCH] =?UTF-8?q?=E7=9C=8B=E6=9D=BF=E5=87=A6=E7=90=86=E7=B3=BB?= =?UTF-8?q?=E3=81=A8=E4=BD=8D=E7=BD=AE=E6=83=85=E5=A0=B1=E5=8F=96=E5=BE=97?= =?UTF-8?q?=E7=B3=BB=E3=82=92=E4=B8=80=E6=96=89=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- menu.js | 362 ++++++++++++++++++++++++-------------------------------- 1 file changed, 156 insertions(+), 206 deletions(-) diff --git a/menu.js b/menu.js index 236ad69..4405db0 100644 --- a/menu.js +++ b/menu.js @@ -24,27 +24,6 @@ import 内子線 from './assets/四国旅客鉄道内子線.json'; import 徳島線 from './assets/四国旅客鉄道徳島線.json'; import 鳴門線 from './assets/四国旅客鉄道鳴門線.json' import LED_vision from './components/発車時刻表/LED_vidion'; -const makeLineStationListValiable = (base,lineName)=>{ - let returnData =[]; - base.features.forEach(element =>{ - if(element.geometry.type=="Point"){ - returnData.push({ - lat:element.geometry.coordinates[1], - lng:element.geometry.coordinates[0], - name:element.properties.name, - lineName:lineName - }) - } - }); - return returnData; -} -const yosanLineStation = makeLineStationListValiable(予讃線,"予讃線"); -const DosanLineStation = makeLineStationListValiable(土讃線,"土讃線"); -const KotokuLineStation = makeLineStationListValiable(高徳線,"高徳線"); -const UchikoLineStation = makeLineStationListValiable(内子線,"(予讃)内子線"); -const TokushimaLineStation = makeLineStationListValiable(徳島線,"徳島線"); -const NarutoLineStation = makeLineStationListValiable(鳴門線,"鳴門線"); -const stationList = {}; @@ -84,135 +63,46 @@ export default function Menu(props) { const StationBoardAcSR = useRef(null); const navigation = useNavigation(); - - const [stationName,setStationName] = useState(undefined); - const getCurrentStation = (location) =>{ - const findStationEachLine = (selectLine,selectLineName) => { - const searchArea = 0.005; - for( let selectLineData of selectLine){ - let latBool = false; - let lngBool = false; - if(location.latitude < selectLineData.lat){ - if((selectLineData.lat - location.latitude) < searchArea)latBool = true; - } - else{ - if((location.latitude - selectLineData.lat) < searchArea)latBool = true; - } - if(location.longitude < selectLineData.lng){ - if((selectLineData.lng - location.longitude) < searchArea)lngBool = true; - } - else{ - if((location.longitude - selectLineData.lng) < searchArea)lngBool = true; - } - if(latBool && lngBool){ - for(let data of stationList.日英対応表){ - if(data.Station_JP == selectLineData.name){ - let pre = undefined; - let nex = undefined; - let now = undefined; - let index =0; - for(let da of stationList[selectLineName]){ - if((data.Station_JP == da.Station_JP) || (data.Station_JP == da.StationName)){ - try{ - let preEn = undefined; - stationList.日英対応表.forEach((j,l)=>{ - if(j.Station_JP == (stationList[selectLineName][index-1].Station_JP ? stationList[selectLineName][index-1].Station_JP : stationList[selectLineName][index-1].StationName)){ - preEn = j.Station_EN; - } - }) - pre = { - name:stationList[selectLineName][index-1].Station_JP ? stationList[selectLineName][index-1].Station_JP : stationList[selectLineName][index-1].StationName, - en:preEn, - StationNumber:stationList[selectLineName][index-1].StationNumber, - StationTimeTable:stationList[selectLineName][index-1].StationTimeTable, - StationMap:stationList[selectLineName][index-1].StationMap - } - }catch(e){ - pre = { - name:null, - en:null, - StationNumber:null - } - } - now = { - LineName:selectLineData.lineName, - name:selectLineData.name, - en:data.Station_EN, - StationNumber:da.StationNumber, - JrHpUrl:da.JrHpUrl, - StationTimeTable:da.StationTimeTable, - StationMap:da.StationMap - } - try{ - let nexEn = undefined; - stationList.日英対応表.forEach((j,l)=>{ - if(j.Station_JP == (stationList[selectLineName][index+1].Station_JP ? stationList[selectLineName][index+1].Station_JP : stationList[selectLineName][index+1].StationName)){ - nexEn = j.Station_EN; - } - }) - nex = { - name:stationList[selectLineName][index+1].Station_JP ? stationList[selectLineName][index+1].Station_JP : stationList[selectLineName][index+1].StationName, - en:nexEn, - StationNumber:stationList[selectLineName][index+1].StationNumber, - StationTimeTable:stationList[selectLineName][index+1].StationTimeTable, - StationMap:stationList[selectLineName][index+1].StationMap - } - }catch(e){ - nex = { - name:null, - en:null, - StationNumber:null, - StationTimeTable:null - } - } - } - index++; - } - if(now){ - setStationName({ - "前":pre, - "今":now, - "次":nex - }); - return true; - } - } - } - } + + //位置情報 + const [location, setLocation] = useState(null); + const [errorMsg, setErrorMsg] = useState(null); + useEffect(() => { + Location.requestForegroundPermissionsAsync().then(data=>{ + if (data.status !== 'granted') { + setErrorMsg('Permission to access location was denied'); + return ()=>{}; } - return false; - } - findStationEachLine(yosanLineStation,"予讃線") ? null : - findStationEachLine(yosanLineStation,"松宇線") ? null : - findStationEachLine(UchikoLineStation,"松宇線") ? null : - findStationEachLine(DosanLineStation,"土讃線") ? null : - findStationEachLine(DosanLineStation,"窪川線") ? null : - findStationEachLine(KotokuLineStation,"高徳線") ? null : - findStationEachLine(TokushimaLineStation,"徳島線") ? null : - findStationEachLine(NarutoLineStation,"鳴門線") ? null : console.log("not found"); - } - async function loadOfficialStationList(){ + Location.getCurrentPositionAsync({}).then(location=>setLocation(location)) + }) + setInterval(() => { + Location.getCurrentPositionAsync({}).then(location=>setLocation(location)) + }, 10000); + },[]) + + const [originalStationList , setOriginalStationList] = useState(); + useEffect(()=>{ //駅リストイニシャライズ const HeaderConfig = { headers:{ 'referer':'https://train.jr-shikoku.co.jp/sp.html' } } Promise.all([ - await fetch('https://train.jr-shikoku.co.jp/g?arg1=station&arg2=yosan', HeaderConfig).then(response => response.json()), - await fetch('https://train.jr-shikoku.co.jp/g?arg1=station&arg2=uwajima',HeaderConfig).then(response => response.json()), - await fetch('https://train.jr-shikoku.co.jp/g?arg1=station&arg2=uwajima2',HeaderConfig).then(response => response.json()), - await fetch('https://train.jr-shikoku.co.jp/g?arg1=station&arg2=dosan',HeaderConfig).then(response => response.json()), - await fetch('https://train.jr-shikoku.co.jp/g?arg1=station&arg2=dosan2',HeaderConfig).then(response => response.json()), - await fetch('https://train.jr-shikoku.co.jp/g?arg1=station&arg2=koutoku',HeaderConfig).then(response => response.json()), - await fetch('https://train.jr-shikoku.co.jp/g?arg1=station&arg2=tokushima',HeaderConfig).then(response => response.json()), - await fetch('https://train.jr-shikoku.co.jp/g?arg1=station&arg2=naruto',HeaderConfig).then(response => response.json()), - await fetch('https://train.jr-shikoku.co.jp/g?arg1=station&arg2=between',HeaderConfig).then(response => response.json()), - await fetch('https://train.jr-shikoku.co.jp/g?arg1=line&arg2=train_lang',HeaderConfig).then(response => response.json()) - ]).then((values)=>{ + fetch('https://train.jr-shikoku.co.jp/g?arg1=station&arg2=yosan', HeaderConfig).then(response => response.json()), + fetch('https://train.jr-shikoku.co.jp/g?arg1=station&arg2=uwajima',HeaderConfig).then(response => response.json()), + fetch('https://train.jr-shikoku.co.jp/g?arg1=station&arg2=uwajima2',HeaderConfig).then(response => response.json()), + fetch('https://train.jr-shikoku.co.jp/g?arg1=station&arg2=dosan',HeaderConfig).then(response => response.json()), + fetch('https://train.jr-shikoku.co.jp/g?arg1=station&arg2=dosan2',HeaderConfig).then(response => response.json()), + fetch('https://train.jr-shikoku.co.jp/g?arg1=station&arg2=koutoku',HeaderConfig).then(response => response.json()), + fetch('https://train.jr-shikoku.co.jp/g?arg1=station&arg2=tokushima',HeaderConfig).then(response => response.json()), + fetch('https://train.jr-shikoku.co.jp/g?arg1=station&arg2=naruto',HeaderConfig).then(response => response.json()), + fetch('https://train.jr-shikoku.co.jp/g?arg1=station&arg2=between',HeaderConfig).then(response => response.json()), + fetch('https://train.jr-shikoku.co.jp/g?arg1=line&arg2=train_lang',HeaderConfig).then(response => response.json()) + ]).then(values =>{ + let stationList = {}; [stationList.予讃線, stationList.松宇線, stationList.伊予灘線, stationList.土讃線, stationList.窪川線, stationList.高徳線, stationList.徳島線, stationList.鳴門線, stationList.駅間リスト,stationList.日英対応表] = values; - const concatBetweenStations = (eachRouteData) => { let additional = []; eachRouteData.forEach((routeData,routeIndex) => { @@ -226,42 +116,74 @@ export default function Menu(props) { } }) } - }catch(e){ - } + }catch(e){} }); return(eachRouteData.concat(additional).sort((a,b)=> (a.StationNumber > b.StationNumber) ? 1 : -1 )) } - stationList.予讃線 = concatBetweenStations(stationList.予讃線); - stationList.松宇線 = concatBetweenStations(stationList.松宇線); - stationList.伊予灘線 = concatBetweenStations(stationList.伊予灘線); - stationList.土讃線 = concatBetweenStations(stationList.土讃線); - stationList.窪川線 = concatBetweenStations(stationList.窪川線); - stationList.高徳線 = concatBetweenStations(stationList.高徳線); - stationList.徳島線 = concatBetweenStations(stationList.徳島線); - stationList.鳴門線 = concatBetweenStations(stationList.鳴門線); - + const addStationPosition = (setDataBase, geoJson, EnJpList) =>{ + return setDataBase.map((data)=>{ + let stationName; + if(data.hasOwnProperty("Station_JP")) stationName = data.Station_JP; + else if(data.hasOwnProperty("StationName")){ + stationName = data.StationName; + data.Station_JP = data.StationName; + data.Station_EN = EnJpList.find(d=>d.Station_JP == data.Station_JP).Station_EN + } + geoJson.features.filter(d=>d.geometry.type == "Point").forEach(element =>{ + if(element.properties.name == stationName){ + data.lat = element.geometry.coordinates[1]; + data.lng = element.geometry.coordinates[0]; + } + }); + return data; + }) + } + stationList.予讃線 = addStationPosition(concatBetweenStations(stationList.予讃線), 予讃線, stationList.日英対応表); + stationList.松宇線 = addStationPosition(concatBetweenStations(stationList.松宇線), 予讃線, stationList.日英対応表); + stationList.伊予灘線 = addStationPosition(concatBetweenStations(stationList.伊予灘線), 予讃線, stationList.日英対応表); + stationList.土讃線 = addStationPosition(concatBetweenStations(stationList.土讃線), 土讃線, stationList.日英対応表); + stationList.窪川線 = addStationPosition(concatBetweenStations(stationList.窪川線), 土讃線, stationList.日英対応表); + stationList.高徳線 = addStationPosition(concatBetweenStations(stationList.高徳線), 高徳線, stationList.日英対応表); + stationList.徳島線 = addStationPosition(concatBetweenStations(stationList.徳島線), 徳島線, stationList.日英対応表); + stationList.鳴門線 = addStationPosition(concatBetweenStations(stationList.鳴門線), 鳴門線, stationList.日英対応表); + + setOriginalStationList(stationList); }); - } - - - + },[]) - const [location, setLocation] = useState(null); - const [errorMsg, setErrorMsg] = useState(null); - useEffect(async() => { - let { status } = await Location.requestForegroundPermissionsAsync(); - if (status !== 'granted') { - setErrorMsg('Permission to access location was denied'); - return; + const [stationName,setStationName] = useState(undefined); + const [currentStation, setCurrentStation] = useState(undefined); + useEffect(()=>{ + if(!location) return ()=>{}; + if(!originalStationList) return ()=>{}; + const findStationEachLine = (selectLine) => { + const searchArea = 0.0015; + const _calcDistance = (from, to) =>{ + let lat = Math.abs(from.lat - to.lat); + let lng = Math.abs(from.lng - to.lng); + return Math.sqrt((lat*lat)+(lng*lng)) + } + let NearStation = selectLine.filter(d=>_calcDistance(d,{lat:location.coords.latitude,lng:location.coords.longitude}) < searchArea); + return NearStation; } - Location.getCurrentPositionAsync({}).then(location=>{ - console.log(location); - setLocation(location); - loadOfficialStationList().then(()=>getCurrentStation(location.coords)); - }) - }, []); + + const lineList = ["予讃線", "松宇線", "伊予灘線", "土讃線", "窪川線", "高徳線", "徳島線", "鳴門線"]; + console.log(lineList.map(d=>findStationEachLine(originalStationList[d])).find(d=>d.length > 0)) + let returnDataBase = lineList.map(d=>findStationEachLine(originalStationList[d])).find(d=>d.length > 0) + if(returnDataBase.length){ + setCurrentStation(returnDataBase); + } + else{ + setCurrentStation(undefined); + } + + },[location,originalStationList]) + + + + const LottieRef = useRef(null); @@ -287,6 +209,18 @@ export default function Menu(props) { }catch(e){} }) + const getPreNextStation = (now) =>{ + const lineList = ["予讃線", "松宇線", "伊予灘線", "土讃線", "窪川線", "高徳線", "徳島線", "鳴門線"]; + let returnData; + lineList.forEach(d=>{ + let cache = originalStationList[d].findIndex( data => data.StationNumber == now.StationNumber); + if(cache != -1){ + returnData = [originalStationList[d][cache-1],originalStationList[d][cache+1]] + } + + }) + return returnData; + } return ( {Status} @@ -305,57 +239,73 @@ export default function Menu(props) { 新型コロナウイルスに関するお知らせ 列車の運行計画・混雑状況・感染症対策への取り組み - {stationName ? stationName.今 ? + {currentStation ? !stationName.今.JrHpUrl || Linking.openURL(stationName.今.JrHpUrl)} */onPress={() => {StationBoardAcSR.current?.setModalVisible()}}> JR - - - {stationName.今.StationNumber} - - + {currentStation.map((d,index,array)=> + { + if(array.length == 1) return 20; + else if(index == 0) return 5; + else if(index == 1) return 35; + else return 20; + })()+"%",right:'10%',width:wp("10%"),height:wp("10%"),borderColor:"#2E94BB",borderWidth:parseInt("2%"),borderRadius:parseInt("100%")}} > + + {d.StationNumber} + + + )} - {stationName.今.LineName} - {stationName.今.name} - {stationName.今.en} + {/* {stationName.今.LineName} */} + {currentStation[0].Station_JP} + {currentStation[0].Station_EN} - {stationName.前 ? : null} - {stationName.前 ? - - - {stationName.前.StationNumber} - - : null} - - {stationName.前.name} - {stationName.前.en} - - {/* Linking.openURL(stationName.今.StationTimeTable)}> - 時刻表 - */} - - {stationName.次.name} - {stationName.次.en} - - {stationName.次.name != null ? - - - {stationName.次.StationNumber} - - : null} - {stationName.次.name != null ? : null} + {(()=>{ + let [preStation, nexStation] = getPreNextStation(currentStation[0]) + return( + [ + + {preStation && + [, + + + {preStation.StationNumber} + + , + + {preStation.Station_JP} + {preStation.Station_EN} + ]} + , + + + {nexStation && + [ + {nexStation.Station_JP} + {nexStation.Station_EN} + , + + + {nexStation.StationNumber} + + , + ]} + ] + ) + })()} + - : null : null} - {stationName ? stationName.今 ? - - : null : null} + : null } + {currentStation ? + + : null} {JRSTraInfoEXAcSR.current?.setModalVisible()}}>