markdown 获取主要城市的GPS坐标

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown 获取主要城市的GPS坐标相关的知识,希望对你有一定的参考价值。

# GPS coordinates of major cities in the world

... to place markers on a map.

This code runs in the browser debugging console.

I parse this page to extract geographical coordibates for major cities:  
https://en.wikipedia.org/w/index.php?title=List_of_cities_by_longitude&printable=yes

Just open this page in your browser, launch your debugging console and get ready to copy-paste.

First, define this function, it is a dependency:

```
function toArray(nl) // Convert NodeList to Array
{
	var arr = [];
	for(var i=-1,l=nl.length;++i!==l;arr[i]=nl[i]);		
	return(arr);
}
```

## Stage 1: parse HTML

```
var C1 = toArray(document.querySelectorAll("table.wikitable tbody tr")).map((tr)=>(
    toArray(tr.children).map((td)=>(
        td.innerText.trim()
    )
)
));
```

The array assigend to C1 is of type:

```
[
   [
      "16°30′S",
      "180°00′W",
      "Rabi Island",
      "N/A",
      "Fiji"
   ],
   [
      "51°53′N",
      "176°39′W",
      "Adak",
      "Alaska",
      "United States"
   ],
   // etc.
]
```


## Stage 2: translate to GPS coordinates
Source: http://stackoverflow.com/a/1140335

```
function ConvertDMSToDD(degrees, minutes, seconds, direction) {
    var dd = parseInt(degrees) + parseInt(minutes)/60 + parseInt(seconds)/(60*60);

    if (direction == "S" || direction == "W") {
        dd = dd * -1;
    } // Don't do anything for N or E
    return dd;
}

var C2 = C1.map(function(city){
	var latLong = [city[0],city[1]];
	coords = latLong.map(function(l){
		var p = l.split(/[^\d\w]+/);
		return ConvertDMSToDD(p[0],p[1],0,p[2]);
	});
	return ({
		"lat/long" : latLong,
		"name" : city[2],
		"state" : city[3],
		"country" :city[4],
		"coords" : coords
	})
});
```

The result is in C2 and it's an array of objects.

### Google Chrome console: Copy result to clipboard:
```
copy(JSON.stringify(C2,null,2))
```

以上是关于markdown 获取主要城市的GPS坐标的主要内容,如果未能解决你的问题,请参考以下文章

怎样根据GPS获得的经纬度来获取城市名?

查找 GPS 坐标路径上的所有城市

如何获取最近城市的名称

如何将 GPS 坐标转换为位置

GPS 坐标系

如何在不使用 GPS 的情况下获取 Android 设备的当前城市名称?