mars-coord
TypeScript icon, indicating that this package has built-in type declarations

0.0.3 • Public • Published

Mars Coord

English README npm version License: MIT

这是一个用于转换火星坐标系的工具库。它使用 TypeScript 编写。

支持的坐标系统有 'WGS84','GCJ02','BD09',以及 'CGCS2000'。

同时提供了AMAP(高德地图),QQ(腾讯地图),和BAIDU(百度地图)这几个别名,分别对应 'GCJ02','GCJ02',和 'BD09'。

安装

npm install mars-coord

使用方法

import { Coordinate, convertCoordinate } from 'mars-coord';

// 创建一个坐标
const myCoordinate: Coordinate = [110.030774, 36.815854]; // 例如,这是一个 'WGS84' 坐标

// 将坐标转换为其他坐标系
const result = convertCoordinate(myCoordinate, 'WGS84');

console.log('WGS84:', result.WGS84);
console.log('GCJ02:', result.GCJ02);
console.log('AMAP:', result.AMAP); // 高德地图使用的坐标系
console.log('QQ:', result.QQ); // 腾讯地图使用的坐标系
console.log('BD09:', result.BD09);
console.log('BAIDU:', result.BAIDU); // 百度地图使用的坐标系
console.log('CGCS2000:', result.CGCS2000);

在前端中使用

import { convertCoordinate } from 'mars-coord';

// 定义一个函数,获取当前位置,并返回一个Promise
function getCurrentLocation() {
  return new Promise((resolve, reject) => {
    if (navigator.geolocation) {
      navigator.geolocation.getCurrentPosition(resolve, reject);
    } else {
      reject(new Error('此浏览器不支持地理位置。'));
    }
  });
}

// 定义一个异步函数,获取并转换坐标
async function getConvertedCoordinates() {
  try {
    // 等待获取当前位置
    const position = await getCurrentLocation();
    const myCoordinate = [position.coords.longitude, position.coords.latitude];

    // 使用mars-coord将坐标转换为其他坐标系
    const result = convertCoordinate(myCoordinate, 'WGS84');

    console.log('WGS84:', result.WGS84); // 输出WGS84坐标
    console.log('GCJ02:', result.GCJ02); // 输出GCJ02坐标
    console.log('AMAP:', result.AMAP); // 输出高德地图使用的坐标
    console.log('QQ:', result.QQ); // 输出QQ地图使用的坐标
    console.log('BD09:', result.BD09); // 输出BD09坐标
    console.log('BAIDU:', result.BAIDU); // 输出百度地图使用的坐标
    console.log('CGCS2000:', result.CGCS2000); // 输出CGCS2000坐标
  } catch (error) {
    // 处理任何错误
    console.error('发生错误:', error);
  }
}

// 调用函数执行代码
getConvertedCoordinates();

坐标拾取器

License

This project is licensed under the terms of the MIT license.

Package Sidebar

Install

npm i mars-coord

Weekly Downloads

1

Version

0.0.3

License

ISC

Unpacked Size

27.3 kB

Total Files

8

Last publish

Collaborators

  • shinchven