根据多个坐标经纬度获取到中心点的经纬度,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

相关推荐
软件黑马王子3 小时前
C#初级教程(4)——流程控制:从基础到实践
开发语言·c#
闲猫3 小时前
go orm GORM
开发语言·后端·golang
丁卯4044 小时前
Go语言中使用viper绑定结构体和yaml文件信息时,标签的使用
服务器·后端·golang
李白同学5 小时前
【C语言】结构体内存对齐问题
c语言·开发语言
黑子哥呢?6 小时前
安装Bash completion解决tab不能补全问题
开发语言·bash
青龙小码农6 小时前
yum报错:bash: /usr/bin/yum: /usr/bin/python: 坏的解释器:没有那个文件或目录
开发语言·python·bash·liunx
大数据追光猿6 小时前
Python应用算法之贪心算法理解和实践
大数据·开发语言·人工智能·python·深度学习·算法·贪心算法
彳卸风7 小时前
Unable to parse timestamp value: “20250220135445“, expected format is
开发语言
bing_1587 小时前
简单工厂模式 (Simple Factory Pattern) 在Spring Boot 中的应用
spring boot·后端·简单工厂模式
dorabighead7 小时前
JavaScript 高级程序设计 读书笔记(第三章)
开发语言·javascript·ecmascript