找回密码
 注册
Simdroid-非首页
查看: 33|回复: 0

urlwrite读取googlemap地图,汉字不完全显示的问题

[复制链接]
发表于 2012-7-16 10:17:52 | 显示全部楼层 |阅读模式 来自 天津
  1. function epicenter_display
  2. %author: Sqliu
  3. clc;
  4. clear;
  5. %Elat = [39.5608];Elon = [117.5531];
  6. %lat=[39 39 40 40];lon=[117 117 118 118];lat=[lat Elat];lon=[lon Elon];
  7. %plot(lon,lat,'.r','MarkerSize',1);plot_google_map;
  8. %hold on
  9. %plot(Elon,Elat,'ro','Markersize',20,'Linewidth',4);
  10. %plot(Elon,Elat,'ro','Markersize',10,'Linewidth',4);
  11. %plot(Elon,Elat,'r+','Markersize',15,'Linewidth',4);
  12. %set(gca,'DataAspectRatio',[1.3 1 1]);
  13. %box on

  14. prompstr={'地震震中经度','地震震中纬度','发震时间','图片边界LatSouth','LatNorth','LonWest','LonEast'};
  15. defaultvalue={'117.5857','39.6479','2012-6-18 03:05:17','39','39.8','117','117.8'};
  16. prompData=inputdlg(prompstr,'Input Earthquake Location',1,defaultvalue);
  17. Elon=str2num(prompData{1});
  18. Elat=str2num(prompData{2});
  19. ttile=['发震时刻:' prompData{3}];

  20. %========图形参考边界=========
  21. %lat=[39 40];
  22. %lon=[117 118];
  23. lat=[str2num(prompData{4}) str2num(prompData{5})];
  24. lon=[str2num(prompData{6}) str2num(prompData{7})];
  25. %=============================
  26. screensize=get(0,'screensize');
  27. figure('position',[screensize(1) screensize(2)+0.05*screensize(4) 0.59*screensize(3) 0.8*screensize(4)]...
  28. ,'Name','地震震中位置','NumberTitle','off');
  29. %figure('color','w','position',[100 50 700 700]);
  30. iptsetpref('ImshowAxesVisible','on')
  31. hxx=plot(lon,lat,'.r','MarkerSize',1);
  32. plot_google_map;
  33. delete(hxx);

  34. hold on
  35. plot(Elon,Elat,'bo','Markersize',60,'Linewidth',3);
  36. plot(Elon,Elat,'go','Markersize',40,'Linewidth',3);
  37. plot(Elon,Elat,'ro','Markersize',20,'Linewidth',2);
  38. plot(Elon,Elat,'r+','Markersize',45,'Linewidth',2);
  39. set(gca,'DataAspectRatio',[1.3 1 1]);
  40. %box on
  41. set(gca,'TickDir','out');
  42. set(gca,'FontWeight','bold','Fontsize',15);
  43. set(gca,'Ycolor','b');
  44. set(gca,'Xcolor','b');
  45. hxxx3=title(ttile);
  46. set(hxxx3,'Fontsize',15,'FontWeight','bold','color','r');
  47. set(gcf,'color',[173/255 229/255 202/255]);
  48. iptsetpref('ImshowAxesVisible','on')

  49. %==============================================
  50. %==========以下为子程序==================
  51. function varargout = plot_google_map(varargin)
  52. % function h = plot_google_map(varargin)
  53. % Plots a google map on the current axes using the Google Static Maps API
  54. %
  55. % USAGE:
  56. % h = plot_google_map(Property, Value,...)
  57. % Plots the map on the given axes. Used also if no output is specified
  58. %
  59. % Or:
  60. % [lonVec latVec imag] = plot_google_map(Property, Value,...)
  61. % Returns the map without plotting it
  62. %
  63. % PROPERTIES:
  64. % Height (640) - Height of the image in pixels (max 640)
  65. % Width (640) - Width of the image in pixels (max 640)
  66. % Scale (2) - (1/2) Resolution scale factor . using Scale=2 will
  67. % double the resulotion of the downloaded image (up
  68. % to 1280x1280) and will result in finer rendering,
  69. % but processing time will be longer.
  70. % MapType - ('roadmap') Type of map to return. Any of [roadmap,
  71. % satellite, terrain, hybrid) See the Google Maps API for
  72. % more information.
  73. % Alpha (1) - (0-1) Transparency level of the map (0 is fully
  74. % transparent). While the map is always
  75. % moved to the bottom of the plot (i.e. will
  76. % not hide previously drawn items), this can
  77. % be useful in order to increase readability
  78. % if many colors are ploted (using SCATTER
  79. % for example).
  80. % Marker - The marker argument is a text string with fields
  81. % conforming to the Google Maps API. The
  82. % following are valid examples:
  83. % '43.0738740,-70.713993' (dflt midsize orange marker)
  84. % '43.0738740,-70.713993,blue' (midsize blue marker)
  85. % '43.0738740,-70.713993,yellowa' (midsize yellow
  86. % marker with label "A")
  87. % '43.0738740,-70.713993,tinyredb' (tiny red marker
  88. % with label "B")
  89. % Refresh (1) - (0/1) defines whether to automatically refresh the
  90. % map upon zoom action on the figure.
  91. % AutoAxis (1) - (0/1) defines whether to automatically adjust the axis
  92. % of the plot to avoid the map being stretched.
  93. % This will adjust the span to be correct
  94. % only if the figure window is square. If
  95. % the figure window is rectangular, it will
  96. % still appear somewhat stretched.
  97. % APIKey - (string) set your own API key which you obtained from Google:
  98. % http://developers.google.com/maps/documentation/staticmaps/#api_key
  99. % This will enable up to 25,000 map
  100. % requests per day, compared to 400
  101. % requests without a key.
  102. % To set the key, use:
  103. % plot_google_map('APIKey','SomeLongStringObtaindFromGoogle')
  104. % To disable the use of a key, use:
  105. % plot_google_map('APIKey','')
  106. %
  107. % OUTPUT:
  108. % h - Handle to the plotted map
  109. %
  110. % lonVect - Vector of Longidute coordinates (WGS84) of the image
  111. % latVect - Vector of Latidute coordinates (WGS84) of the image
  112. % imag - Image matrix (height,width,3) of the map
  113. %
  114. % EXAMPLE - plot a map showing some capitals in Europe:
  115. % lat = [48.8708 51.5188 41.9260 40.4312 52.523 37.982];
  116. % lon = [2.4131 -0.1300 12.4951 -3.6788 13.415 23.715];
  117. % plot(lon,lat,'.r','MarkerSize',20)
  118. % plot_google_map
  119. %================================================================
  120. %Elat = [39.5608];Elon = [117.5531];
  121. %lat=[39 39 40 40];lon=[117 117 118 118];lat=[lat Elat];lon=[lon Elon];
  122. %plot(lon,lat,'.r','MarkerSize',20);plot_google_map;
  123. %=================================================================
  124. % References:
  125. % http://www.mathworks.com/matlabcentral/fileexchange/24113
  126. % http://www.maptiler.org/google-maps-coordinates-tile-bounds-projection/
  127. % http://developers.google.com/maps/documentation/staticmaps/
  128. %
  129. % Acknowledgement to Val Schmidt for his submission of get_google_map.m
  130. %
  131. % Author:
  132. % Zohar Bar-Yehuda
  133. % Version 1.2 - 16/06/2012
  134. % - Support use of the "scale=2" parameter by default for finer rendering (set scale=1 if too slow).
  135. % - Auto-adjust axis extent so the map isn't stretched.
  136. % - Set and use an API key which enables a much higher usage volume per day.
  137. % Version 1.1 - 25/08/2011

  138. % store parameters in global variable (used for auto-refresh)
  139. global inputParams
  140. persistent apiKey
  141. if isnumeric(apiKey)
  142. % first run, check if API key file exists
  143. if exist('api_key.mat','file')
  144. load api_key
  145. else
  146. apiKey = '';
  147. end
  148. end
  149. axHandle = gca;
  150. inputParams.(['ax' num2str(axHandle*1e6,'%.0f')]) = varargin;

  151. % Handle input arguments

  152. height = 640;
  153. width = 640;
  154. scale =2;
  155. maptype = 'roadmap';
  156. alphaData = 1;
  157. autoRferesh = 1;
  158. autoAxis = 1;
  159. hold on

  160. markeridx = 1;
  161. markerlist = {};
  162. if nargin >= 2
  163. for idx = 1:2:length(varargin)
  164. switch lower(varargin{idx})
  165. case 'height'
  166. height = varargin{idx+1};
  167. case 'width'
  168. width = varargin{idx+1};
  169. case 'maptype'
  170. maptype = varargin{idx+1};
  171. case 'alpha'
  172. alphaData = varargin{idx+1};
  173. case 'refresh'
  174. autoRferesh = varargin{idx+1};
  175. case 'marker'
  176. markerlist{markeridx} = varargin{idx+1};
  177. markeridx = markeridx + 1;
  178. case 'autoaxis'
  179. autoAxis = varargin{idx+1};
  180. case 'apikey'
  181. apiKey = varargin{idx+1}; % set new key
  182. % save key to file
  183. funcFile = which('plot_google_map.m');
  184. pth = fileparts(funcFile);
  185. keyFile = fullfile(pth,'api_key.mat');
  186. save(keyFile,'apiKey')
  187. otherwise
  188. error(['Unrecognized variable: ' varargin{idx}])
  189. end
  190. end
  191. end
  192. if height > 640
  193. height = 640;
  194. end
  195. if width > 640
  196. width = 640;
  197. end

  198. curAxis = axis;
  199. if isequal(curAxis,[0 1 0 1]) % probably an empty figure
  200. % display world map
  201. curAxis = [-180 180 -85 85];
  202. axis(curAxis)
  203. end

  204. if autoAxis
  205. % adjust current axis limit to avoid strectched maps
  206. [xExtent,yExtent] = latLonToMeters(curAxis(3:4), curAxis(1:2) );
  207. xExtent = diff(xExtent); % just the size of the span
  208. yExtent = diff(yExtent);
  209. if xExtent > yExtent
  210. % enlarge the X extent
  211. centerY = mean(curAxis(3:4));
  212. spanY = (curAxis(4)-curAxis(3))/2;
  213. curAxis(3) = centerY-spanY*xExtent/yExtent;
  214. curAxis(4) = centerY+spanY*xExtent/yExtent;
  215. elseif yExtent > xExtent
  216. % enlarge the Y extent
  217. centerX = mean(curAxis(1:2));
  218. spanX = (curAxis(2)-curAxis(1))/2;
  219. curAxis(1) = centerX-spanX*yExtent/xExtent;
  220. curAxis(2) = centerX+spanX*yExtent/xExtent;
  221. end
  222. axis(curAxis) % update axis as quickly as possible, before downloading new image
  223. drawnow
  224. end

  225. % Delete previous map from plot (if exists)
  226. if nargout <= 1 % only if in plotting mode
  227. curChildren = get(axHandle,'children');
  228. delete(findobj(curChildren,'tag','gmap'))
  229. end

  230. % Enforce Latitude constraints of EPSG:900913
  231. if curAxis(3) < -85
  232. curAxis(3) = -85;
  233. end
  234. if curAxis(4) > 85
  235. curAxis(4) = 85;
  236. end

  237. % Calculate zoom level for current axis limits
  238. [xExtent,yExtent] = latLonToMeters(curAxis(3:4), curAxis(1:2) );
  239. minResX = diff(xExtent) / width;
  240. minResY = diff(yExtent) / height;
  241. minRes = max([minResX minResY]);
  242. tileSize = 256;
  243. initialResolution = 2 * pi * 6378137 / tileSize; % 156543.03392804062 for tileSize 256 pixels
  244. zoomlevel = floor(log2(initialResolution/minRes));

  245. % Enforce valid zoom levels
  246. if zoomlevel < 0
  247. zoomlevel = 0;
  248. end
  249. if zoomlevel > 19
  250. zoomlevel = 19;
  251. end

  252. % Calculate center coordinate in WGS1984
  253. lat = (curAxis(3)+curAxis(4))/2;
  254. lon = (curAxis(1)+curAxis(2))/2;

  255. % CONSTRUCT QUERY URL
  256. preamble = 'http://maps.googleapis.com/maps/api/staticmap';
  257. location = ['?center=' num2str(lat,10) ',' num2str(lon,10)];
  258. zoomStr = ['&zoom=' num2str(zoomlevel)];
  259. sizeStr = ['&scale=' num2str(scale) '&size=' num2str(width) 'x' num2str(height)];
  260. maptypeStr = ['&maptype=' maptype ];
  261. if ~isempty(apiKey)
  262. keyStr = ['&key=' apiKey];
  263. else
  264. keyStr = '';
  265. end
  266. markers = '&markers=';
  267. for idx = 1:length(markerlist)
  268. if idx < length(markerlist)
  269. markers = [markers markerlist{idx} '%7C'];
  270. else
  271. markers = [markers markerlist{idx}];
  272. end
  273. end
  274. if ismember(maptype,{'satellite','hybrid'})
  275. filename = 'tmp.jpg';
  276. format = '&format=jpg';
  277. convertNeeded = 0;
  278. else
  279. filename = 'tmp.png';
  280. format = '&format=png';
  281. convertNeeded = 1;
  282. end
  283. sensor = '&sensor=false';
  284. url = [preamble location zoomStr sizeStr maptypeStr format markers sensor keyStr];

  285. % Get the image
  286. try
  287. urlwrite(url,filename);
  288. catch % error downloading map
  289. warning('Unable to download map form Google Servers.\nPossible reasons: no network connection, or quota exceeded (1000 map requests per day).')
  290. return
  291. end
  292. [M Mcolor] = imread(filename);
  293. M = cast(M,'double');
  294. delete(filename); % delete temp file
  295. width = size(M,2);
  296. height = size(M,1);

  297. % Calculate a meshgrid of pixel coordinates in EPSG:900913
  298. centerPixelY = round(height/2);
  299. centerPixelX = round(width/2);
  300. [centerX,centerY] = latLonToMeters(lat, lon ); % center coordinates in EPSG:900913
  301. curResolution = initialResolution / 2^zoomlevel/scale; % meters/pixel (EPSG:900913)
  302. xVec = centerX + ((1:width)-centerPixelX) * curResolution; % x vector
  303. yVec = centerY + ((height:-1:1)-centerPixelY) * curResolution; % y vector
  304. [xMesh,yMesh] = meshgrid(xVec,yVec); % construct meshgrid

  305. % convert meshgrid to WGS1984
  306. [lonMesh,latMesh] = metersToLatLon(xMesh,yMesh);

  307. % We now want to convert the image from a colormap image with an uneven
  308. % mesh grid, into an RGB truecolor image with a uniform grid.
  309. % This would enable displaying it with IMAGE, instead of PCOLOR.
  310. % Advantages are:
  311. % 1) faster rendering
  312. % 2) makes it possible to display together with other colormap annotations (PCOLOR, SCATTER etc.)

  313. % Convert image from colormap type to RGB truecolor (if PNG is used)
  314. if convertNeeded
  315. imag = zeros(height,width,3);
  316. for idx = 1:3
  317. imag(:,:,idx) = reshape(Mcolor(M(:)+1+(idx-1)*size(Mcolor,1)),height,width);
  318. end
  319. else
  320. imag = M/255;
  321. end

  322. % Next, project the data into a uniform WGS1984 grid
  323. sizeFactor = 1; % factoring of new image
  324. uniHeight = round(height*sizeFactor);
  325. uniWidth = round(width*sizeFactor);
  326. latVect = linspace(latMesh(1,1),latMesh(end,1),uniHeight);
  327. lonVect = linspace(lonMesh(1,1),lonMesh(1,end),uniWidth);
  328. [uniLonMesh,uniLatMesh] = meshgrid(lonVect,latVect);
  329. uniImag = zeros(uniHeight,uniWidth,3);

  330. % old version (projection using INTERP2)
  331. % for idx = 1:3
  332. % % 'nearest' method is the fastest. difference from other methods is neglible
  333. % uniImag(:,:,idx) = interp2(lonMesh,latMesh,imag(:,:,idx),uniLonMesh,uniLatMesh,'nearest');
  334. % end
  335. uniImag = myTurboInterp2(lonMesh,latMesh,imag,uniLonMesh,uniLatMesh);

  336. if nargout <= 1 % plot map
  337. % display image
  338. h = image(lonVect,latVect,uniImag);
  339. set(gca,'YDir','Normal')
  340. set(h,'tag','gmap')
  341. set(h,'AlphaData',alphaData)

  342. % older version (display without conversion to uniform grid)
  343. % h =pcolor(lonMesh,latMesh,(M));
  344. % colormap(Mcolor)
  345. % caxis([0 255])
  346. % warning off % to avoid strange rendering warnings
  347. % shading flat

  348. uistack(h,'bottom') % move map to bottom (so it doesn't hide previously drawn annotations)
  349. axis(curAxis) % restore original zoom
  350. if nargout == 1
  351. varargout{1} = h;
  352. end
  353. %zoom(0.5);
  354. % if auto-refresh mode - override zoom callback to allow autumatic
  355. % refresh of map upon zoom actions.
  356. zoomHandle=zoom; %(gcf,'getzoom');
  357. if autoRferesh
  358. set(zoomHandle,'ActionPostCallback',@update_google_map);
  359. else % disable zoom override
  360. set(zoomHandle,'ActionPostCallback',[]);
  361. end
  362. else % don't plot, only return map
  363. varargout{1} = lonVect;
  364. varargout{2} = latVect;
  365. varargout{3} = uniImag;
  366. end




  367. %====================================
  368. %hold on
  369. %plot(Elon,Elat,'ro','Markersize',20,'Linewidth',4);
  370. %plot(Elon,Elat,'ro','Markersize',10,'Linewidth',4);
  371. %plot(Elon,Elat,'r+','Markersize',15,'Linewidth',4);
  372. %set(gca,'DataAspectRatio',[1.3 1 1]);
  373. %box on

  374. %====================================

  375. % Coordinate transformation functions

  376. function [lon,lat] = metersToLatLon(x,y)
  377. % Converts XY point from Spherical Mercator EPSG:900913 to lat/lon in WGS84 Datum
  378. originShift = 2 * pi * 6378137 / 2.0; % 20037508.342789244
  379. lon = (x ./ originShift) * 180;
  380. lat = (y ./ originShift) * 180;
  381. lat = 180 / pi * (2 * atan( exp( lat * pi / 180)) - pi / 2);

  382. function [x,y] = latLonToMeters(lat, lon )
  383. % Converts given lat/lon in WGS84 Datum to XY in Spherical Mercator EPSG:900913"
  384. originShift = 2 * pi * 6378137 / 2.0; % 20037508.342789244
  385. x = lon * originShift / 180;
  386. y = log(tan((90 + lat) * pi / 360 )) / (pi / 180);
  387. y = y * originShift / 180;


  388. function ZI = myTurboInterp2(X,Y,Z,XI,YI)
  389. % An extremely fast nearest neighbour 2D interpolation, assuming both input
  390. % and output grids consist only of squares, meaning:
  391. % - uniform X for each column
  392. % - uniform Y for each row
  393. XI = XI(1,:);
  394. X = X(1,:);
  395. YI = YI(:,1);
  396. Y = Y(:,1);

  397. xiPos = nan*ones(size(XI));
  398. xLen = length(X);
  399. yiPos = nan*ones(size(YI));
  400. yLen = length(Y);
  401. % find x conversion
  402. xPos = 1;
  403. for idx = 1:length(xiPos)
  404. if XI(idx) >= X(1) && XI(idx) <= X(end)
  405. while xPos < xLen && X(xPos+1)<XI(idx)
  406. xPos = xPos + 1;
  407. end
  408. diffs = abs(X(xPos:xPos+1)-XI(idx));
  409. if diffs(1) < diffs(2)
  410. xiPos(idx) = xPos;
  411. else
  412. xiPos(idx) = xPos + 1;
  413. end
  414. end
  415. end
  416. % find y conversion
  417. yPos = 1;
  418. for idx = 1:length(yiPos)
  419. if YI(idx) <= Y(1) && YI(idx) >= Y(end)
  420. while yPos < yLen && Y(yPos+1)>YI(idx)
  421. yPos = yPos + 1;
  422. end
  423. diffs = abs(Y(yPos:yPos+1)-YI(idx));
  424. if diffs(1) < diffs(2)
  425. yiPos(idx) = yPos;
  426. else
  427. yiPos(idx) = yPos + 1;
  428. end
  429. end
  430. end
  431. ZI = Z(yiPos,xiPos,:);


  432. function update_google_map(obj,evd)
  433. % callback function for auto-refresh
  434. drawnow;
  435. global inputParams
  436. params = inputParams.(['ax' num2str(gca*1e6,'%.0f')]);
  437. plot_google_map(params{:});






复制代码

urlwrite读取googlemap地图,汉字不完全显示的问题

在Matlab2004版,发现zoom的功能不足(缺callback函数),所以新装了matlab2011版。在Matlab2004版下,googlemap的地图数字可以都按汉字显示,而matlab2011版则只有部分底图元素按汉字显示。将matlab的参数配置上都设置成了汉字风格,但没解决问题,不知道是什么原因?
另,网上的matlab2011a似乎不太完整?安装后没有work路径,matlab.exe居然在bin文件夹里面。
不知哪位碰到类似的问题?谢谢

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|小黑屋|联系我们|仿真互动网 ( 京ICP备15048925号-7 )

GMT+8, 2024-7-3 14:27 , Processed in 0.031095 second(s), 11 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表