百度地图API获取给定地址所在经纬度

本文介绍了如何利用百度地图API的Geocoding API来获取指定地址的经纬度坐标。通过调用geocoder的getPoint()方法,可以异步获取到地理位置信息。示例代码展示了在特定容器中展示地图并获取起始位置经纬度的过程。

百度地图API:百度地图API


根据地址获取地址所在经纬度:

Geocoding API

geocoder 中提供了getPoint() 方法获取


部分实现代码:

		if(startAddress==""){
			startAddress=$.location.current_location.address;
		}
		var map = new BMap.Map(container);
		var geocoder = new BMap.Geocoder();
		//默认为南京
		var longitude = 118.807395;
		var latitude = 32.065315;
		//获取起始地址经纬度
		geocoder.getPoint(startAddress,
				function(point){
					if(point)
					{
						longitude = point.lng;
						latitude = point.lat;
					}
				},$.location.current_location.address);
		
		//map.centerAndZoom(new BMap.Point(118.807395,32.065315), 14);
		map.centerAndZoom(new BMap.Point(longitude,latitude), 14);

container为显示地图的一个容器,startAdress为起始位置

注:回调函数和主函数是异步执行的,如果在回调函数内对主函数中的一些变量进行操作,主函数的相关代码应放到回调函数中。


最终效果实现:




评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值