Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add functions and fix bugs #1

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
# WGS84-Convert-GCJ02
将WGS84坐标的GPX文件转换成GCJ02,也可以单独转换坐标。<br>
GPX文件可以按起讫时间截取部分,但仅支持掐头去尾。<br>
所有计算都在本地完成,不会上传数据,保证隐私。<br>
如果GPX数据量较大,贴入文本区时会需要较长时间,请耐心等待。<br>
- 单个GPS坐标转换为火星坐标
- GPX格式文件坐标转换
- [Github托管转换页面](https://yaozhenghangma.github.io/WGS84-Convert-GCJ02/gpsxConvert.html)

http://htmlpreview.github.com/?https://raw.githubusercontent.com/lucifersun/WGS84-Convert-GCJ02/master/gpsxConvert.html

<img src="Snipaste_2018-05-26_21-05-26.JPG" height="600px"/>
<img src="Snipaste_2023-04-24_17-20-01.png" height="600px"/>
Binary file added Snipaste_2023-04-24_17-20-01.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
118 changes: 82 additions & 36 deletions gpsxConvert.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,24 @@
<h2>GPS坐标(WGS-84) 换算 火星坐标(GCJ-02)</h2>

<br>
<p>GPS坐标(WGS-84) Lat: <input type="text" id="WGSLat" /> &emsp; Lon: <input type="text" id="WGSLon" /> &emsp; <button type="button" onclick="tomars ()">转换==></button> &emsp;
<p>GPS坐标(WGS-84) Lat: <input type="text" id="WGSLat" /> &emsp; Lon: <input type="text" id="WGSLon" /> &emsp; <button type="button" onclick="tomars ()">转换==></button> &emsp;
火星坐标(GCJ-02) Lat: <input type="text" id="GCJLat" /> &emsp; Lon: <input type="text" id="GCJLon" /></p>

<p><button type="button" onclick="gpxsto ()">转换GPX内容</button> &emsp; <input type="checkbox" onclick="gettime()" id="Clip"/> 截取部分 &emsp; 开始时间: <input type="datetime-local" id="begin_user_time" />
<p><button type="button" onclick="gpxsto ()">转换GPX内容</button> &emsp; <input type="checkbox" onclick="gettime()" id="Clip"/> 截取部分 &emsp; 开始时间: <input type="datetime-local" id="begin_user_time" />
&emsp; 结束时间: <input type="datetime-local" id="end_user_time" /> &emsp; 时区: <input type="text" size="1" id="GMT" value="8"/> &emsp; <button type="button" onclick="gettime ()">刷新时间</button></p>
<p>注:截取功能仅支持截取中间段内容(截头去尾)。GPX使用UTC时间,时区设置仅作用于起讫时间的显示,方便调整,不会影响GPX内的时间数据。firefox不支持datetime-local对象。</p>
<p><textarea rows="30" cols="70" id="WGSgpx"></textarea> &emsp; &emsp; <textarea rows="30" cols="70" id="GCJgpx"></textarea></p>
<table>
<tr>
<td>WGS-GPX文件 &emsp; <input id="WGSfile" type="file" accept=".gpx" onchange="readFiles(this.files)"></td>
<td>&emsp; &emsp;</td>
<td>GCJ-GPX文件 &emsp; <button type="button" id="down" onclick="saveFile()">保存文件</button></td>
</tr>
<tr>
<td><textarea rows="30" cols="70" id="WGSgpx"></textarea></td>
<td>&emsp; &emsp;</td>
<td><textarea rows="30" cols="70" id="GCJgpx"></textarea></td>
</tr>
</table>

<p><span id="demo"></span></p>
<p><span id="demo1"></span></p>
Expand All @@ -24,6 +35,36 @@ <h2>GPS坐标(WGS-84) 换算 火星坐标(GCJ-02)</h2>
a = 6378245;
ee = 6.69342162296594E-03;

function readFiles(files) {
var file = files[0];

var reader = new FileReader();
reader.onload = function (e) {
document.getElementById("WGSgpx").value=e.target.result;
};
reader.readAsText(file);
}

function saveFile(filename, content) {
var filename = "GCJ-GPX.gpx";
var content = document.getElementById("GCJgpx").value;
var blob = new Blob([content], {type: 'text/plain'});
var url = window.URL.createObjectURL(blob);
var a = document.createElement('a');

a.style = "display: none";
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();

setTimeout(function () {
document.body.removeChild(a);
window.URL.revokeObjectURL(url);
}, 5);
}


function tomars(){
var LatLon = new Array();
LatLon[0]=Number(document.getElementById("WGSLat").value);
Expand Down Expand Up @@ -54,43 +95,48 @@ <h2>GPS坐标(WGS-84) 换算 火星坐标(GCJ-02)</h2>
gcjgpxstr = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\" ?>" + "\n" + "<gpx>" + "\n" + " <trk>" + "\n";
parser=new DOMParser();
xmlDoc=parser.parseFromString(wgsgpxstr,"text/xml");
gcjgpxstr=gcjgpxstr + " <name>" + xmlDoc.getElementsByTagName("name")[0].childNodes[0].nodeValue + "</name>" + "\n";
gcjgpxstr=gcjgpxstr + " <trkseg>" + "\n";
var trksegall=xmlDoc.getElementsByTagName("trkseg");
var trkptall=xmlDoc.getElementsByTagName("trkpt");
document.getElementById("demo").innerHTML="总共" + trkptall.length + "个节点"
var beginTime,endTime,nowTime;
beginTime=Date.parse(document.getElementById("begin_user_time").value + ":00Z") - document.getElementById("GMT").value*3600*1000;
endTime=Date.parse(document.getElementById("end_user_time").value + ":59Z") - document.getElementById("GMT").value*3600*1000;
var clipValue
clipValue=document.getElementById("Clip").checked;
var ii;
ii=1;
for (var i=0;i<trkptall.length;i++){
nowTime=Date.parse(trkptall[i].getElementsByTagName("time")[0].childNodes[0].nodeValue);
if (nowTime < beginTime && clipValue){
continue;
}
if (nowTime>endTime && clipValue){
break;
}
LatLon[0]= Number(trkptall[i].getAttribute("lat"));
LatLon[1]= Number(trkptall[i].getAttribute("lon"));
LatLon=wgs2gcj(LatLon[0],LatLon[1]);
gcjgpxstr=gcjgpxstr + " <trkpt lat=\"" + LatLon[0] + "\" lon=\"" + LatLon[1] + "\">" + "\n";
gcjgpxstr=gcjgpxstr + " <ele>" + trkptall[i].getElementsByTagName("ele")[0].childNodes[0].nodeValue + "</ele>" + "\n";
if(trkptall[i].getElementsByTagName("speed")[0]!=undefined){
gcjgpxstr=gcjgpxstr + " <speed>" + trkptall[i].getElementsByTagName("speed")[0].childNodes[0].nodeValue + "</speed>" + "\n";
}
if(trkptall[i].getElementsByTagName("course")[0]!=undefined){
gcjgpxstr=gcjgpxstr + " <course>" + trkptall[i].getElementsByTagName("course")[0].childNodes[0].nodeValue + "</course>" + "\n";
document.getElementById("demo").innerHTML="共计:" + trksegall.length + "段记录," + trkptall.length + "个节点。"

var ii=1;
for (var i_trkseg=0; i_trkseg<trksegall.length; i_trkseg++) {
gcjgpxstr=gcjgpxstr + " <trkseg>" + "\n";
var trkpt_i = trksegall[i_trkseg].getElementsByTagName("trkpt");
var beginTime,endTime,nowTime;
beginTime=Date.parse(document.getElementById("begin_user_time").value + ":00Z") - document.getElementById("GMT").value*3600*1000;
endTime=Date.parse(document.getElementById("end_user_time").value + ":59Z") - document.getElementById("GMT").value*3600*1000;
var clipValue
clipValue=document.getElementById("Clip").checked;
for (var i=0;i<trkpt_i.length;i++){
nowTime=Date.parse(trkpt_i[i].getElementsByTagName("time")[0].childNodes[0].nodeValue);
if (nowTime < beginTime && clipValue){
continue;
}
if (nowTime>endTime && clipValue){
break;
}
LatLon[0]= Number(trkpt_i[i].getAttribute("lat"));
LatLon[1]= Number(trkpt_i[i].getAttribute("lon"));
LatLon=wgs2gcj(LatLon[0],LatLon[1]);
gcjgpxstr=gcjgpxstr + " <trkpt lat=\"" + LatLon[0] + "\" lon=\"" + LatLon[1] + "\">" + "\n";
gcjgpxstr=gcjgpxstr + " <ele>" + trkpt_i[i].getElementsByTagName("ele")[0].childNodes[0].nodeValue + "</ele>" + "\n";
if(trkpt_i[i].getElementsByTagName("speed")[0]!=undefined){
gcjgpxstr=gcjgpxstr + " <speed>" + trkpt_i[i].getElementsByTagName("speed")[0].childNodes[0].nodeValue + "</speed>" + "\n";
}
if(trkpt_i[i].getElementsByTagName("course")[0]!=undefined){
gcjgpxstr=gcjgpxstr + " <course>" + trkpt_i[i].getElementsByTagName("course")[0].childNodes[0].nodeValue + "</course>" + "\n";
}
gcjgpxstr=gcjgpxstr + " <time>" + trkpt_i[i].getElementsByTagName("time")[0].childNodes[0].nodeValue + "</time>" + "\n";
gcjgpxstr=gcjgpxstr + " </trkpt>" + "\n"
document.getElementById("demo1").innerHTML="已处理" + ii + "个节点"
ii++;
}
gcjgpxstr=gcjgpxstr + " <time>" + trkptall[i].getElementsByTagName("time")[0].childNodes[0].nodeValue + "</time>" + "\n";
gcjgpxstr=gcjgpxstr + " </trkpt>" + "\n"
document.getElementById("demo1").innerHTML="已处理" + ii + "个节点"
ii++;
gcjgpxstr=gcjgpxstr + " </trkseg>" + "\n";
}
gcjgpxstr=gcjgpxstr + " </trkseg>" + "\n" + " </trk>" + "\n" + "</gpx>";
gcjgpxstr=gcjgpxstr + " </trk>" + "\n" + "</gpx>";
document.getElementById("GCJgpx").value=gcjgpxstr;
gettime();
}

function wgs2gcj(wgLat, wgLon){
Expand Down