在百度地图api,xy坐标转换经纬度在线怎么转换成百度坐标

百度地图之地址信息和坐标的转换
在实际运用中,经常需要进行地理编码和地理反编码,即将地址信息转换成坐标和将坐标转换成地址信息,此demo就是用来展示如何进行地理编码搜索(用地址检索坐标)、反地理编码搜索(用坐标检索地址)以及展示如何使用ItemizedOverlay在地图上标注结果点,代码原型来自百度Demo,代码如下:
Activity:
package com.
import android.app.A
import android.graphics.drawable.D
import android.os.B
import android.view.V
import android.view.View.OnClickL
import android.widget.B
import android.widget.EditT
import android.widget.T
import com.baidu.mapapi.map.ItemizedO
import com.baidu.mapapi.map.MapV
import com.baidu.mapapi.map.OverlayI
import com.baidu.mapapi.search.MKAddrI
import com.baidu.mapapi.search.MKBusLineR
import com.baidu.mapapi.search.MKDrivingRouteR
import com.baidu.mapapi.search.MKPoiR
import com.baidu.mapapi.search.MKS
import com.baidu.mapapi.search.MKSearchL
import com.baidu.mapapi.search.MKShareUrlR
import com.baidu.mapapi.search.MKSuggestionR
import com.baidu.mapapi.search.MKTransitRouteR
import com.baidu.mapapi.search.MKWalkingRouteR
import com.baidu.platform.comapi.basestruct.GeoP
public class GeoCoderActivity extends Activity implements OnClickListener {
private Button mBtnReverseGeoCode = // 将坐标反编码为地址
private Button mBtnGeoCode = // 将地址编码为坐标
private EditText lat =
private EditText lon =
private EditText editCity =
private EditText editGeoCodeKey =
// 地图相关
private MapView mMapView = // 地图View
// 搜索相关
private MKSearch mSearch = // 搜索模块,也可去掉地图模块独立使用
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DemoApplication app = (DemoApplication) this.getApplication();
setContentView(R.layout.geocoder);
CharSequence titleLable = &地理编码功能&;
setTitle(titleLable);
// 地图初始化
mMapView = (MapView) findViewById(R.id.geocoder_bmapView);
mMapView.getController().enableClick(true);
mMapView.getController().setZoom(12);
// UI初始化
lat = (EditText) findViewById(R.id.geocoder_et_lat);
lon = (EditText) findViewById(R.id.geocoder_et_lon);
editCity = (EditText) findViewById(R.id.geocoder_et_city);
editGeoCodeKey = (EditText) findViewById(R.id.geocoder_et_geocodekey);
mBtnReverseGeoCode = (Button) findViewById(R.id.geocoder_btn_reversegeocode);
mBtnGeoCode = (Button) findViewById(R.id.geocoder_btn_geocode);
mBtnReverseGeoCode.setOnClickListener(this);
mBtnGeoCode.setOnClickListener(this);
// 初始化搜索模块,注册事件监听
mSearch = new MKSearch();
mSearch.init(app.mBMapManager, new MKSearchListener() {
public void onGetPoiDetailSearchResult(int type, int error) {
public void onGetAddrResult(MKAddrInfo res, int error) {
if (error != 0) {
String str = String.format(&错误号:%d&, error);
Toast.makeText(GeoCoderActivity.this, str,
Toast.LENGTH_LONG).show();
// 地图移动到该点
mMapView.getController().animateTo(res.geoPt);
if (res.type == MKAddrInfo.MK_GEOCODE) {
// 地理编码:通过地址检索坐标点
String strInfo = String.format(&纬度:%f 经度:%f&,
res.geoPt.getLatitudeE6() / 1e6,
res.geoPt.getLongitudeE6() / 1e6);
Toast.makeText(GeoCoderActivity.this, strInfo,
Toast.LENGTH_LONG).show();
if (res.type == MKAddrInfo.MK_REVERSEGEOCODE) {
// 反地理编码:通过坐标点检索详细地址及周边poi
String strInfo = res.strA
Toast.makeText(GeoCoderActivity.this, strInfo,
Toast.LENGTH_LONG).show();
// 生成ItemizedOverlay图层用来标注结果点
ItemizedOverlay&OverlayItem& itemOverlay = new ItemizedOverlay&OverlayItem&(
null, mMapView);
// 生成Item
OverlayItem item = new OverlayItem(res.geoPt, &&, null);
// 得到需要标在地图上的资源
Drawable marker = getResources().getDrawable(
R.drawable.icon_markf);
// 为maker定义位置和边界
marker.setBounds(0, 0, marker.getIntrinsicWidth(),
marker.getIntrinsicHeight());
// 给item设置marker
item.setMarker(marker);
// 在图层上添加item
itemOverlay.addItem(item);
// 清除地图其他图层
mMapView.getOverlays().clear();
// 添加一个标注ItemizedOverlay图层
mMapView.getOverlays().add(itemOverlay);
// 执行刷新使生效
mMapView.refresh();
public void onGetPoiResult(MKPoiResult res, int type, int error) {
public void onGetDrivingRouteResult(MKDrivingRouteResult res,
int error) {
public void onGetTransitRouteResult(MKTransitRouteResult res,
int error) {
public void onGetWalkingRouteResult(MKWalkingRouteResult res,
int error) {
public void onGetBusDetailResult(MKBusLineResult result, int iError) {
public void onGetSuggestionResult(MKSuggestionResult res, int arg1) {
public void onGetShareUrlResult(MKShareUrlResult result, int type,
int error) {
public void onClick(View v) {
if (v == mBtnGeoCode) {
// Geo搜索
mSearch.geocode(editGeoCodeKey.getText().toString(), editCity
.getText().toString());
if (v == mBtnReverseGeoCode) {
GeoPoint ptCenter = new GeoPoint((int) (Float.valueOf(lat.getText()
.toString()) * 1e6), (int) (Float.valueOf(lon.getText()
.toString()) * 1e6));
// 反Geo搜索
mSearch.reverseGeocode(ptCenter);
protected void onPause() {
mMapView.onPause();
super.onPause();
protected void onResume() {
mMapView.onResume();
super.onResume();
protected void onDestroy() {
mMapView.destroy();
super.onDestroy();
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mMapView.onSaveInstanceState(outState);
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
mMapView.onRestoreInstanceState(savedInstanceState);
&?xml version=&1.0& encoding=&utf-8&?&
&LinearLayout xmlns:android=&http://schemas.android.com/apk/res/android&
android:layout_width=&match_parent&
android:layout_height=&match_parent&
android:orientation=&vertical& &
&LinearLayout
android:layout_width=&match_parent&
android:layout_height=&wrap_content&
android:orientation=&horizontal& &
android:id=&@+id/geocoder_et_city&
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:text=&北京& /&
android:id=&@+id/geocoder_et_geocodekey&
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:text=&海淀区上地十街10号& /&
android:id=&@+id/geocoder_btn_geocode&
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:background=&@drawable/button_style&
android:text=&Geo& /&
&/LinearLayout&
&LinearLayout
android:layout_width=&match_parent&
android:layout_height=&wrap_content&
android:orientation=&horizontal& &
android:id=&@+id/geocoder_et_lat&
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:text=&39.904965& /&
android:id=&@+id/geocoder_et_lon&
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:text=&116.327764& /&
android:id=&@+id/geocoder_btn_reversegeocode&
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:background=&@drawable/button_style&
android:text=&ReverseGeo& /&
&/LinearLayout&
&com.baidu.mapapi.map.MapView
android:id=&@+id/geocoder_bmapView&
android:layout_width=&match_parent&
android:layout_height=&match_parent&
android:clickable=&true& /&
&/LinearLayout&
配置文件同之前地图示例
附上图片效果:在百度地图api,经纬度怎么转换成百度坐标_百度知道
在百度地图api,经纬度怎么转换成百度坐标
我有更好的答案
berland Hotel which he had chosen? If they had
在百度地图api,经纬度怎么转换成百度坐标
为您推荐:
其他类似问题
换一换
回答问题,赢新手礼包
个人、企业类
违法有害信息,请在下方选择后提交
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。python调用百度地图API实现经纬度换算、热力地图全流程指南
作者: 博观厚积
基于地图的数据可视化应用愈来愈广泛,目前,有很多方法来实现地图可视化,包括excel的power map包、各种数据分析软件的地图库以及在线交互地图可视化操作工具,如Echarts、Tableau Public、polyMaps等等。另外还有一种手段就是通过软件调用百度、google或者其他地图的api,自己DIY可视化地图,但是这种办法需要操作者本身既要对相关软件的编程熟悉,又要熟悉不同地图api的具体用法。本文就是用这种手段,以一个简单的表格文件出发,在不知道相关地点经纬度的情况下,通过python调用百度地图API实现热力地图,这其中需要申请密钥、批量经纬度换算、转换成js数据、百度热力地图api相关参数的调整等等。
(1)初始数据:csv格式的数据表格
初始数据为个大中城市新建住宅价格指数同比值,直接从国家统计局网站公布的数据copy过来(下图),数据已整理好,为两列(城市city、房价指数price),并保存为csv格式。在实际中,我们常常通过爬取网站上万条地区数据并存为csv格式来分析,在这里为简化流程,初始数据来源直接copy已有数据。
(2)城市转换成经纬度第一步:注册密钥
在百度地图api上相关位置的展现是以经纬度为基础的(这里暂不介绍百度地图坐标体系与其他地图的区别),如北京,其经度(longitude)为:116.395645,纬度(latitude)为:39.929986,在这里既需要通过百度的Geocoding API来获取不同城市的经纬度坐标,又要求将csv数据文件导入python,批量获取这70个城市的坐标信息。在做这些之前,需要注册百度地图api(首先你要用百度的账号)以获取免费的密钥,才能完全使用该api。
登录网址:http://lbsyun.baidu.com/,
首页点击申请密钥按钮,经过填写个人信息、邮箱注册等,成功之后在开放平台上点击“创建应用”,填写相关信息,在这里特别说明的是,在IP白名单框里,如果不清楚自己的IP地址,最好设置为:0.0.0.0/0,虽然百度提醒它会有泄露使用的风险,但是有时候你把你自己的IP地址输进去可能也不行。提交后,在你创建应用的访问应用(AK)那一栏就是你的密钥。
(3)城市转换成经纬度第二步:构造经纬度获取函数
注册密钥后就可以在百度Web服务API下的Geocoding API接口来获取你所需要地址的经纬度坐标并转化为json结构的数据,其网址为:
http://lbsyun.baidu.com/index.php?title=webapi/guide/webservice-geocoding,
网页中有相关说明,根据示例URL,采用python3软件,写出如下函数:
import json from urllib.request import urlopen, quote import requests,csv import pandas as pd #导入这些库后边都要用到 def getlnglat(address): url = 'http://api.map.baidu.com/geocoder/v2/' output = 'json' ak = '你申请的密钥***' add = quote(address) #由于本文城市变量为中文,为防止乱码,先用quote进行编码 uri = url + '?' + 'address=' + add + '&output=' + output + '&ak=' + ak req = urlopen(uri) res = req.read().decode() #将其他编码的字符串解码成unicode temp = json.loads(res) #对json数据进行解析 return temp
(4)城市转换成经纬度第三步:批量获取城市经纬度坐标
在构造完获取坐标函数后,我们就需要用python读取csv文件的数据,并将city列单独读出来,批量获取经度、纬度坐标,并生成json的数据文件,其代码如下:
file = open(r'E:\\爬虫数据分析\调用百度地图api\point.json','w') #建立json数据文件 with open(r'E:\\爬虫数据分析\调用百度地图api\各区域房价.csv', 'r') as csvfile: #打开csv reader = csv.reader(csvfile) for line in reader: #读取csv里的数据 # 忽略第一行 if reader.line_num == 1: #由于第一行为变量名称,故忽略掉 continue # line是个list,取得所有需要的值 b = line[0].strip() #将第一列city读取出来并清除不需要字符 c= line[1].strip()#将第二列price读取出来并清除不需要字符 lng = getlnglat(b)['result']['location']['lng'] #采用构造的函数来获取经度 lat = getlnglat(b)['result']['location']['lat'] #获取纬度 str_temp = '{"lat":' + str(lat) + ',"lng":' + str(lng) + ',"count":' + str(c) +'},' #print(str_temp) #也可以通过打印出来,把数据copy到百度热力地图api的相应位置上 file.write(str_temp) #写入文档 file.close() #保存
在这里特别要注意str_temp = ‘{“lat”:’ + str(lat) + ‘,”lng”:’ + str(lng) + ‘,”count”:’ + str(c) +’},’,这一行的命令,这是参照百度地图Java API热力图制作的相应格式而生成的,生成的json数据格式为:{“lat”:39.24,”lng”:116.67,”count”:124.7},如下图所示,来自于网址:http://developer.baidu.com/map/jsdemo.htm#c1_15。
(5)生成热力地图
接下来就比较简单,我们先建立一个html文件,将http://developer.baidu.com/map/jsdemo.htm#c1_15 网址中源代码复制过来,首先将代码中的ak换成你自己的密钥;
然后将生成的point.json文件里的数据复制出来,在替换掉var points =[ ]里的内容,即可。这里要注意的是,由于百度地图Java API热力图默认的是以天安门为中心的北京区域地图,而我们的数据是全国性的,所以这里还需要对热力图中“设置中心点坐标和地图级别”的部分进行修改(见下图),具体设置可以参考百度创建地图api中:
http://api.map.baidu.com/lbsapi/creatmap/ ,自己可以去调试出合适的中心点与地图级别。
最后,由于我们的大部分price数据(也就是points里的count)都超过了100(默认最大为100),还需要对热点图代码中的点最大值进行设定(这里设为140)。
保存后,用浏览器打开,即得到了个大中城市新建住宅价格指数同比的热力地图
转载请注明来自36大数据(36dsj.com):36大数据>> python调用百度地图API实现经纬度换算、热力地图全流程指南
责任编辑:
声明:该文观点仅代表作者本人,搜狐号系信息发布平台,搜狐仅提供信息存储空间服务。
今日搜狐热点在百度地图api,经纬度怎么转换成百度坐标_百度知道
在百度地图api,经纬度怎么转换成百度坐标
我有更好的答案
&!DOCTYPE&html&&&html&&&head&&&meta&http-equiv=&Content-Type&content=&text/&charset=gb2312&/&&&script&type=&text/javascript&src=&changeMore.js&&&/script&&&title&批量转换坐标&/title&&&/head&&&body&&&input&onclick=&magic();&value=&批量转换&type=&button&/&(据说有50次/秒的限制哦)&hr&/&&&div&style=&clear:both&&&&div&style=&float:&&&&h4&谷歌地图&/h4&&&div&style=&width:520height:340border:1px&solid&gray&id=&map_canvas&&&/div&&&p&鼠标点击的谷歌坐标是:&span&id=&info&&&/span&&/p&&&script&type=&text/javascript&src=&&script&type=&text/javascript&&&functioninitialize()&{varmyOptions&={&&zoom:&14,&&center:&newgoogle.maps.LatLng(39.82,&116.46),&&mapTypeId:&google.maps.MapTypeId.ROADMAP&&};varmap&=newgoogle.maps.Map(document.getElementById('map_canvas'),myOptions);&&google.maps.event.addListener(map,&'click',&function(e)&{&&document.getElementById(&info&).innerHTML&=e.latL&&});varmarker1&=newgoogle.maps.Marker({&&position:&newgoogle.maps.LatLng(39.83,&116.9),&&map:&map&&});varmarker2&=newgoogle.maps.Marker({&&position:&newgoogle.maps.LatLng(39.71,&116.32),&&map:&map&&});varmarker3&=newgoogle.maps.Marker({&&position:&newgoogle.maps.LatLng(39.574,&116.35),&&map:&map&&});varmarker4&=newgoogle.maps.Marker({&&position:&newgoogle.maps.LatLng(39.29,&116.88),&&map:&map&&});varmarker5&=newgoogle.maps.Marker({&&position:&newgoogle.maps.LatLng(39.71,&116.72),&&map:&map&&});&&}&&google.maps.event.addDomListener(window,&'load',&initialize);&/script&&&/div&&&div&style=&float:&&&&h4&百度地图&/h4&&&div&style=&width:520height:340border:1px&solid&gray&id=&container&&&/div&&&p&鼠标点击的百度坐标是:(&span&id=&info2&&&/span&)&/p&&&script&type=&text/javascript&src=&&script&type=&text/javascript&&&varmap&=newBMap.Map(&container&);&&map.centerAndZoom(newBMap.Point(116.404,&39.915),&15);varmarkers&=[];&&map.addEventListener(&click&,function(e){&&document.getElementById(&info2&).innerHTML&=e.point.lng&+&,&+e.point.&&});//注意:百度和谷歌的经纬度坐标顺序是相反的。&&varpoints&=[newBMap.Point(116.9,39.83),newBMap.Point(116.32,39.71),newBMap.Point(116.35,39.574),newBMap.Point(116.88,39.29),newBMap.Point(116.72,39.71)&&];functioncallback(xyResult){&if(xyResult.error&!=0){}//出错就直接返回;varpoint&=newBMap.Point(xyResult.x,&xyResult.y);varmarker&=newBMap.Marker(point);&&map.addOverlay(marker);&&map.setCenter(point);//由于写了这句,可以每一个被转的点都是中心点的过程&&}functionmagic(){&&BMap.Convertor.transMore(points,2,callback);&&}&/script&&&/div&&&/div&&&/body&&&/html&&changeMore.js&&//&zhangying&&(function(){&&functionload_script(xyUrl,&callback){&&varhead&=&document.getElementsByTagName('head')[0];&& varscript&=&document.createElement('script');&&script.type&=&'text/javascript';&&script.src&=&xyU&&//借鉴了jQuery的script跨域方法&&scriptscript.onload&=&script.onreadystatechange&=&function(){&&if((!this.readyState&||&this.readyState&===&&loaded&&||&this.readyState&===&&complete&)){&&callback&&&callback();&&//Handle&memory&leak&in&IE&&scriptscript.onload&=&script.onreadystatechange&=&&&if(&head&&&script.parentNode&)&{&&head.removeChild(&script&);&&}&&}&&};&&//Use&insertBefore&instead&of&appendChild&to&circumvent&an&IE6&bug.&&head.insertBefore(&script,&head.firstChild&);&&}&&functiontransMore(points,type,callback){&&for(varindex&inpoints){&&if(index&&50){}&&varxyUrl&=&&&&to=4&x=&&+&points[index].lng&+&//这里要循环读入数组points的lng数据,直到points.length完毕。&&&&y=&&+&points[index].lat&+&&&&callbackcallback=callback&;&&//动态创建script标签&&load_script(xyUrl);&&}&&}&&windowwindow.BMap&=&window.BMap&||&{};&&BMap.Convertor&=&{};&&BMap.Convertor.transMore&=&transM&&})();
为您推荐:
其他类似问题
百度地图api的相关知识
换一换
回答问题,赢新手礼包
个人、企业类
违法有害信息,请在下方选择后提交
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。源代码编辑器

我要回帖

更多关于 xy坐标转换经纬度在线 的文章

 

随机推荐