依据经纬度解析商圈scala实现

调用高德的API,需要先去高德申请一个key.

import com.alibaba.fastjson.{JSON, JSONArray, JSONObject}
import org.apache.commons.lang.StringUtils
import org.apache.http.client.methods.{CloseableHttpResponse, HttpGet}
import org.apache.http.impl.client.{CloseableHttpClient, HttpClients}
import org.apache.http.util.EntityUtils

import java.util
import scala.collection.mutable.ListBuffer
import scala.tools.scalap.scalax.util.StringUtil

object getBusiness {
  def main(args: Array[String]): Unit = {
    val str: String = getBusinessFromMap(116.397128, 39.916527)
    println(str) //东单,北海,沙滩
  }

  def getBusinessFromMap(long: Double, lat: Double): String = {
    //调用高德地铁api利用经纬度获取地理信息
    val url: String = "https://restapi.amap.com/v3/geocode/regeo?output=json&location=" + long + "," + lat + "&key=94feff67a3b99aef15bd801b78508f"
    //Creates CloseableHttpClient instance with default configuration.
    val client: CloseableHttpClient = HttpClients.createDefault()
    val get = new HttpGet(url)
    val response: CloseableHttpResponse = client.execute(get)
    val str: String = EntityUtils.toString(response.getEntity, "UTF-8")
    println(str)
    //解析api返回的json数据
    val nObject: JSONObject = JSON.parseObject(str)
    if (nObject.getString("status") != "1") {
      return null
    }
    val regeocode: JSONObject = nObject.getJSONObject("regeocode")
    if (regeocode.isEmpty) return null
    val addressComponent: JSONObject = regeocode.getJSONObject("addressComponent")
    if (addressComponent.isEmpty) return null
    val business: JSONArray = addressComponent.getJSONArray("businessAreas")
    //[{"name":"东单","location":"116.416804,39.913479","id":"110101"},{"name":"北海","location":"116.387646,39.928173","id":"110102"},{"name":"沙滩","location":"116.402653,39.925923","id":"110101"}]
    println(business)
    if (business.isEmpty) return null
    //将商圈名字封装到list
    val list = new ListBuffer[String]()

    for (x <- business.toArray()) {
      //每个元素转为json对象
      val jarr: JSONObject = x.asInstanceOf[JSONObject]
      //提取商圈名称
      list.append(jarr.getString("name"))
    }
    list.mkString(",")

  }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值