服务读取本地文件型地图瓦片
import com.sailen.tpm.modules.signal.config.TpmConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Optional;
@RestController
@RequestMapping("/map")
public class MapPublicController {
@GetMapping("/{layer}/{level}/{col}/{row}")
public void xyz(@PathVariable String layer, @PathVariable String level, @PathVariable String col,
@PathVariable String row, HttpServletResponse response) {
String dataPath = "your data path";
StringBuffer buffer = new StringBuffer(dataPath);
buffer.append("/").append(layer)
.append("/").append(level)
.append("/").append(col)
.append("/").append(row);
try {
String tilePath = buffer.toString();
InputStream inputStream = new FileInputStream(tilePath);
if (Optional.of(inputStream).isPresent()) {
BufferedImage bi = ImageIO.read(inputStream);
ImageIO.write(bi, "PNG", response.getOutputStream());
bi.flush();
inputStream.close();
}
} catch (IOException e) {
System.out.println(String.format("%s/%d/%d/%s请求瓦片出错!", layer, level, col, row));
}
}
}
前台请求,leaflet为例
var map = L.map('map', {
crs: L.CRS.EPSG3857,
center: [34.7, 113.6],
zoom: 12
})
L.tileLayer('http://localhost:8080/appname/map/youlayer/{z}/{x}/{y}.png', {
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(this.map)
其中appname依据项目context-path自行更改,youlayer根据瓦片文件夹自行设定。