简介
百度地图API是可以给开发人员调用的开放API,我们可以用Javascrip或者Javat语言进行调用;
下面利用spring boot结合百度地图API实现定位操作
可以参考官方文档:https://lbsyun.baidu.com/index.php?title=webapi/ip-api
一、环境搭建
前提是你已经在百度地图开放平台注册了自己的应用
1.1、创建spring boot项目
1.1.1、目录结构
1.1.2、pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.zzuli</groupId>
<artifactId>baidumap</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>baidumap</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 网络请求-->
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
1.2、编写代码
1.2.1、配置网络请求的OkHttp
OkHttpConfig
package com.zzuli.baidumap.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.OkHttp3ClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
/**
* @author niuben
*/
@Configuration
public class OkHttpConfig {
@Bean
public RestTemplate restTemplate(){
return new RestTemplate(new OkHttp3ClientHttpRequestFactory());
}
}
1.2.2、接口编写
MapController
package com.zzuli.baidumap.controller;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Map;
/**
* @author niuben
*/
@RestController
public class MapController {
//有百度开放平对生成
private String ak = "自己的ak";
@Resource
private RestTemplate restTemplate;
@GetMapping("/get_addr")
public Object getAddrByUserIP(HttpServletRequest request) throws URISyntaxException {
String ip = request.getRemoteHost();
System.out.println(ip);
//如果是内网访问,可以自己手动填写IP地址
//ip = "xxx.xxx.xxx.xxx";
//百度地图API调用
String url = "https://api.map.baidu.com/location/ip?ak="+ak+"&ip="+ip+"&coor=bd09ll";
//发送请求
ResponseEntity<Map> forEntity = restTemplate.getForEntity(new URI(url), Map.class);
Map result = forEntity.getBody();
return result;
}
}