根据多个坐标经纬度获取到中心点的经纬度,scala语言

文章目录

前言

Scala 语言


  • 通过多个经纬度坐标点, 计算出中心点, 这里使用的是 Scala 语言,其他的语言需要自行转换。
  • 求出来的并不是原有的点,而是原有点的中心位置的点。

scala 代码

scala 复制代码
package com.dw.process.mid

import java.lang.Double.parseDouble
import scala.annotation.tailrec
import scala.collection.mutable
import scala.math._

object GeoCalculatorTest {

  def getPointsCenter(points: Array[String]): (Double, Double) = {
    val pointNum = points.length
    var X = 0.0
    var Y = 0.0
    var Z = 0.0

    @tailrec
    def accumulate(i: Int): Unit = {
      if (i < pointNum) {
        val point = points(i).split(',')
        if (point.length == 2) {
          val lat = parseDouble(point(0)) * math.Pi / 180
          val lng = parseDouble(point(1)) * math.Pi / 180
          val x = cos(lat) * cos(lng)
          val y = cos(lat) * sin(lng)
          val z = sin(lat)

          X += x
          Y += y
          Z += z
        }
        accumulate(i + 1)
      }
    }

    accumulate(0)

    X /= pointNum
    Y /= pointNum
    Z /= pointNum

    val tmpLng = atan2(Y, X)
    val tmpLat = atan2(Z, sqrt(X * X + Y * Y))

    (tmpLat * 180 / math.Pi, tmpLng * 180 / math.Pi)
  }

  def main(args: Array[String]): Unit = {
    val str = "30.866603259732102,104.39074045163579;30.8619616865538,104.38696390134282;30.842287763333733,104.38807970029302;30.843761605258894,104.43202501279302;30.851572589486928,104.43545824033208;30.874265047548715,104.41202646237798"
    val arr = str.split(';')
    val tmpCenter = getPointsCenter(arr)
    println(s"Center latitude: ${tmpCenter._1}, Center longitude: ${tmpCenter._2}")


    // 如果使用腾讯地图或其他地图API,可以像下面这样创建坐标点
    // val defaultPoint = new qq.maps.LatLng(tmpCenter._1, tmpCenter._2)
  }
}
  • 运行得出的中心点的经纬度
log 复制代码
Center latitude: 30.85674358236576, Center longitude: 104.40754961360216
  • 核心算法已经给出,请自行根据项目需求修改。

  • 多个经纬度组成的多边形

    得出的中心点


总结

如果此篇文章有帮助到您, 希望打大佬们能关注点赞收藏评论支持一波,非常感谢大家!

如果有不对的地方请指正!!!

参考1

相关推荐
酷爱码2 小时前
如何通过python连接hive,并对里面的表进行增删改查操作
开发语言·hive·python
画个大饼2 小时前
Go语言实战:快速搭建完整的用户认证系统
开发语言·后端·golang
喵先生!3 小时前
C++中的vector和list的区别与适用场景
开发语言·c++
Thomas_YXQ3 小时前
Unity3D Lua集成技术指南
java·开发语言·驱动开发·junit·全文检索·lua·unity3d
xMathematics4 小时前
计算机图形学实践:结合Qt和OpenGL实现绘制彩色三角形
开发语言·c++·qt·计算机图形学·cmake·opengl
yuanManGan6 小时前
C++入门小馆: 深入了解STLlist
开发语言·c++
北极的企鹅886 小时前
XML内容解析成实体类
xml·java·开发语言
BillKu6 小时前
Vue3后代组件多祖先通讯设计方案
开发语言·javascript·ecmascript
Python自动化办公社区6 小时前
Python 3.14:探索新版本的魅力与革新
开发语言·python
逐光沧海6 小时前
STL常用算法——C++
开发语言·c++