Javascript 在谷歌地图API中使用地址而不是经度和纬度
我听说可以提交一个地址而不是经纬度,这对我的系统来说更可行。用户在创建个人资料时必须输入他们的地址,之后他们的个人资料将显示他们房子的谷歌地图。我目前的Google Maps API与经度和纬度完美配合:Javascript 在谷歌地图API中使用地址而不是经度和纬度,javascript,google-maps,google-maps-api-3,latitude-longitude,street-address,Javascript,Google Maps,Google Maps Api 3,Latitude Longitude,Street Address,我听说可以提交一个地址而不是经纬度,这对我的系统来说更可行。用户在创建个人资料时必须输入他们的地址,之后他们的个人资料将显示他们房子的谷歌地图。我目前的Google Maps API与经度和纬度完美配合: <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> <link rel="stylesheet" type="text/css" href="css.css
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<link rel="stylesheet" type="text/css" href="css.css" />
<title>Login Page</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<style type="text/css">
html { height: 100% }
body { height: 100%; margin: 0; padding: 0 }
#map-canvas { height: 100% }
</style>
<script type="text/javascript"
src="https://maps.googleapis.com/maps/api/js?key=MYKEY&sensor=false">
</script>
<script type="text/javascript">
function initialize() {
var mapOptions = {
center: new google.maps.LatLng(52.640143,1.28685),
zoom: 15,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map"),
mapOptions);
var marker = new google.maps.Marker({
position: new google.maps.LatLng(52.640143,1.28685),
map: map,
title: "Mark On Map"
});
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
登录页面
html{高度:100%}
正文{高度:100%;边距:0;填充:0}
#地图画布{高度:100%}
函数初始化(){
变量映射选项={
中心:新google.maps.LatLng(52.640143,1.28685),
缩放:15,
mapTypeId:google.maps.mapTypeId.ROADMAP
};
var map=new google.maps.map(document.getElementById(“map”),
地图选项);
var marker=new google.maps.marker({
位置:新google.maps.LatLng(52.640143,1.28685),
地图:地图,
标题:“地图上的标记”
});
}
google.maps.event.addDomListener(窗口“加载”,初始化);
请有人帮我修改我的代码,这样Google Maps API就可以在它的位置请求地址,因为我想直接从mySQL数据库读取用户的地址。地理编码是转换地址的过程(如“1600竞技场公园路,加利福尼亚州山景城”)转换为地理坐标(如纬度37.423021和经度-122.083739),可用于放置标记或定位地图 这就是您想要的:包含示例代码
请参见将地图初始化为“加利福尼亚州圣地亚哥” 使用Google Maps Javascript API v3将地址转换为可以在地图上显示的坐标
<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 geocoder;
var map;
var address ="San Diego, CA";
function initialize() {
geocoder = new google.maps.Geocoder();
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 (geocoder) {
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (status != google.maps.GeocoderStatus.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("Geocode was not successful for the following reason: " + status);
}
});
}
}
</script>
</head>
<body style="margin:0px; padding:0px;" onload="initialize()">
<div id="map_canvas" style="width:100%; height:100%">
</body>
</html>
您可以通过地址解析地理位置。 使用jquery创建一个数组,如下所示:
//遵循此结构
var地址数组=[
'地址街号,邮政区/城市'
]
//循环所有地址,并为每个地址调用一个标记
对于(var x=0;x
另外请注意,如果你没有商业账户,谷歌会限制你的搜索结果,如果你使用了太多地址,你会得到一个错误。var-geocoder;
var geocoder;
var map;
function initialize() {
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(-34.397, 150.644);
var mapOptions = {
zoom: 8,
center: latlng
}
map = new google.maps.Map(document.getElementById('map'), mapOptions);
}
function codeAddress() {
var address = document.getElementById('address').value;
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == 'OK') {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
}
<body onload="initialize()">
<div id="map" style="width: 320px; height: 480px;"></div>
<div>
<input id="address" type="textbox" value="Sydney, NSW">
<input type="button" value="Encode" onclick="codeAddress()">
</div>
</body>
var映射;
函数初始化(){
geocoder=新的google.maps.geocoder();
var latlng=新的google.maps.latlng(-34.397150.644);
变量映射选项={
缩放:8,
中心:拉丁
}
map=new google.maps.map(document.getElementById('map'),mapOptions);
}
函数代码地址(){
var address=document.getElementById('address')。值;
geocoder.geocode({'address':address},函数(结果,状态){
如果(状态=‘正常’){
map.setCenter(结果[0].geometry.location);
var marker=new google.maps.marker({
地图:地图,
位置:结果[0]。几何体。位置
});
}否则{
警报('地理编码因以下原因未成功:'+状态);
}
});
}
或者参考文档我想分享我以前使用过的这个代码片段,它通过地理编码添加多个地址,并将这些地址添加为标记
var地址数组=[
'地址街号,邮政区/城市',
//遵循这个结构
]
var map=new google.maps.map(document.getElementById('map'){
中心:{
拉脱维亚:12.7826,
液化天然气:105.0282
},
缩放:6,
手势处理:“合作”
});
var geocoder=new google.maps.geocoder();
对于(i=0;i