栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

通过Google Maps API使用地址而不是经度和纬度

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

通过Google Maps API使用地址而不是经度和纬度

使用Google Maps Javascript API v3
Geoprer将地址转换为可以在地图上显示的坐标。

<html><head><meta name="viewport" content="initial-scale=1.0, user-scalable=no"/><meta http-equiv="content-type" content="text/html; charset=UTF-8"/><title>Google Maps Javascript API v3 Example: Geocoding Simple</title><script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script><script type="text/javascript">  var geoprer;  var map;  var address ="San Diego, CA";  function initialize() {    geoprer = new google.maps.Geoprer();    var latlng = new google.maps.LatLng(-34.397, 150.644);    var myOptions = {      zoom: 8,      center: latlng,    mapTypeControl: true,    mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU},    navigationControl: true,      mapTypeId: google.maps.MapTypeId.ROADMAP    };    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);    if (geoprer) {      geoprer.geopre( { 'address': address}, function(results, status) {        if (status == google.maps.GeoprerStatus.OK) {          if (status != google.maps.GeoprerStatus.ZERO_RESULTS) {          map.setCenter(results[0].geometry.location); var infowindow = new google.maps.InfoWindow(     { content: '<b>'+address+'</b>',       size: new google.maps.Size(150,50)     }); var marker = new google.maps.Marker({     position: results[0].geometry.location,     map: map,      title:address });  google.maps.event.addListener(marker, 'click', function() {     infowindow.open(map,marker); });          } else { alert("No results found");          }        } else {          alert("Geopre was not successful for the following reason: " + status);        }      });    }  }</script></head><body  onload="initialize()"> <div id="map_canvas" ></body></html>

工作代码段:

var geoprer;var map;var address = "San Diego, CA";function initialize() {  geoprer = new google.maps.Geoprer();  var latlng = new google.maps.LatLng(-34.397, 150.644);  var myOptions = {    zoom: 8,    center: latlng,    mapTypeControl: true,    mapTypeControlOptions: {      style: google.maps.MapTypeControlStyle.DROPDOWN_MENU    },    navigationControl: true,    mapTypeId: google.maps.MapTypeId.ROADMAP  };  map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);  if (geoprer) {    geoprer.geopre({      'address': address    }, function(results, status) {      if (status == google.maps.GeoprerStatus.OK) {        if (status != google.maps.GeoprerStatus.ZERO_RESULTS) {          map.setCenter(results[0].geometry.location);          var infowindow = new google.maps.InfoWindow({ content: '<b>' + address + '</b>', size: new google.maps.Size(150, 50)          });          var marker = new google.maps.Marker({ position: results[0].geometry.location, map: map, title: address          });          google.maps.event.addListener(marker, 'click', function() { infowindow.open(map, marker);          });        } else {          alert("No results found");        }      } else {        alert("Geopre was not successful for the following reason: " + status);      }    });  }}google.maps.event.addDomListener(window, 'load', initialize);html,body,#map_canvas {  height: 100%;  width: 100%;}<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script><div id="map_canvas" ></div>


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/405063.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号