一、java根据地理位置获取经纬度。
百度地图 SDK开发密钥的申请地址为:https://lbs.baidu.com/apiconsole/key#/home
public class EntCoordSyncJob {
static String AK = "KxBPoZK9XIu0CKmnf3e5PwYNB09q8OD2"; // 百度地图密钥
public static void main(String[] args) {
String dom = "山东省济南市高新区万达";
Trapeze trapeze = getCoordinate(dom);
System.out.println("'" + dom + "'的经纬度为:" + trapeze);
}
// 调用百度地图API根据地址,获取坐标
public static Trapeze getCoordinate(String address) {
if (address != null && !"".equals(address)) {
address = address.replaceAll("\\s*", "").replace("#", "栋");
String url = "https://api.map.baidu.com/geocoding/v3/?address=" + address + "&output=json&ak=" + AK ;
String json = loadJSON(url);
if (json != null && !"".equals(json)) {
JSONObject obj = JSONObject.fromObject(json);
if ("0".equals(obj.getString("status"))) {
double lng = obj.getJSONObject("result").getJSONObject("location").getDouble("lng"); // 经度
double lat = obj.getJSONObject("result").getJSONObject("location").getDouble("lat"); // 纬度
DecimalFormat df = new DecimalFormat("#.######");
//return df.format(lng) + "," + df.format(lat);//返回经纬度数据
Trapeze trapeze = new Trapeze();
trapeze.setLat(new BigDecimal(lat));
trapeze.setLng(new BigDecimal(lng));
return trapeze;//返回经纬度实体类
}
}
}
return null;
}
public static String loadJSON(String url) {
StringBuilder json = new StringBuilder();
try {
URL oracle = new URL(url);
URLConnection yc = oracle.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream(), "UTF-8"));
String inputLine = null;
while ((inputLine = in.readLine()) != null) {
json.append(inputLine);
}
in.close();
} catch (MalformedURLException e) {} catch (IOException e) {}
return json.toString();
}
}
二、vue前端根据IP地址获取地理位置信息。
高德地图(不能在手机页面使用,因为是通过IP获取的地理位置)
1.引入vue-amap
安装:npm install -S vue-amap
在main.js文件中引用vue-amap
import VueAMap from 'vue-amap';
Vue.use(VueAMap)
VueAMap.initAMapApiLoader({
key:'ad392baa1f1c07423183df47e2cb925f',//替换你的key
plugin: ['AMap.Autocomplete','AMap.PlaceSearch','AMap.Scale','AMap.OverView','AMap.ToolBar','AMap.MapType','AMap.PolyEditor','AMap.CircleEditor','AMap.Geolocation'],
v:'1.4.4'
})
2.创建js文件。location.js文件
/**
* 高德地图定位
* @type {{}}
*/
export const location = {
initMap(id){
let mapObj = new AMap.Map(id, {})
let geolocation;
mapObj.plugin(['AMap.Geolocation'], function () {
geolocation = new AMap.Geolocation({
enableHighAccuracy: true, // 是否使用高精度定位,默认:true
timeout: 10000, // 超过10秒后停止定位,默认:无穷大
maximumAge: 0, // 定位结果缓存0毫秒,默认:0
convert: true, // 自动偏移坐标,偏移后的坐标为高德坐标,默认:true
showButton: true, // 显示定位按钮,默认:true
buttonPosition: 'LB', // 定位按钮停靠位置,默认:'LB',左下角
buttonOffset: new AMap.Pixel(10, 20), // 定位按钮与设置的停靠位置的偏移量,默认:Pixel(10, 20)
showMarker: true, // 定位成功后在定位到的位置显示点标记,默认:true
showCircle: true, // 定位成功后用圆圈表示定位精度范围,默认:true
panToLocation: true, // 定位成功后将定位到的位置作为地图中心点,默认:true
zoomToAccuracy: true // 定位成功后调整地图视野范围使定位位置及精度范围视野内可见,默认:false
})
mapObj.addControl(geolocation)
geolocation.getCurrentPosition()
})
return geolocation;
}
}
3.vue页面代码
import { location } from "../../utils/localtion";//引入js文件
export default {
components: {},
data () {
return {
lat: null,
lng: null,
}
},
created() {
},
methods: {
/**获取地图定位*/
getLocation() {
let _that = this;
let geolocation = location.initMap("map-container"); //定位
AMap.event.addListener(geolocation, "complete", result => {
_that.lat = result.position.lat;
_that.lng = result.position.lng;
_that.province = result.addressComponent.province;
_that.city = result.addressComponent.city;
_that.district = result.addressComponent.district;
_that.address = result.formattedAddress;
console.log(result);
});
},
},
mounted() {
this.getLocation(); // 调用获取地理位置
}
};
腾讯地图
created() {
//获取当前位置的经纬度
var data = {
key: "BHWBZ-AZOCP-QBAD6-VOENV-GVJNF-3QFEC" //这个key就是你申请的key
};
var url = "https://apis.map.qq.com/ws/location/v1/ip"; //这个就是地理位置信息的接口
data.output = "jsonp";
this.$jsonp(url, data)
.then(res => {
console.log(res);
console.log(res,123);
})
.catch(error => {
console.log(error,456);
});
},