Javascript 按位置创建谷歌地图
作为新手使用谷歌JavaScript API。。我不得不问 我试图找到一些关于它的信息,但我没有。。。我知道我们可以通过long/lat和多种类型创建地图,但我需要通过以下位置创建地图: 我想举个例子:巴塞罗那,帕塞格·德格雷西亚,32岁 使用并指向该位置作为中心创建地图是否可行?因为我没有邮政编码。。。或者我真的需要它来参考APIJavascript 按位置创建谷歌地图,javascript,google-maps,Javascript,Google Maps,作为新手使用谷歌JavaScript API。。我不得不问 我试图找到一些关于它的信息,但我没有。。。我知道我们可以通过long/lat和多种类型创建地图,但我需要通过以下位置创建地图: 我想举个例子:巴塞罗那,帕塞格·德格雷西亚,32岁 使用并指向该位置作为中心创建地图是否可行?因为我没有邮政编码。。。或者我真的需要它来参考API 真的谢谢 使用地址参数调用此url 获取该json { "results" : [ { "address_components" : [ {
真的谢谢 使用地址参数调用此url 获取该json
{
"results" : [
{
"address_components" : [
{
"long_name" : "32",
"short_name" : "32",
"types" : [ "street_number" ]
},
{
"long_name" : "Passeig de Gràcia",
"short_name" : "Passeig de Gràcia",
"types" : [ "route" ]
},
{
"long_name" : "Barcelona",
"short_name" : "Barcelona",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Barcelona",
"short_name" : "Barcelona",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Catalunya",
"short_name" : "CT",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "España",
"short_name" : "ES",
"types" : [ "country", "political" ]
},
{
"long_name" : "08007",
"short_name" : "08007",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "Passeig de Gràcia, 32, 08007 Barcelona, Barcelona, España",
"geometry" : {
"location" : {
"lat" : 41.3907821,
"lng" : 2.1672485
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 41.39213108029149,
"lng" : 2.168597480291502
},
"southwest" : {
"lat" : 41.38943311970849,
"lng" : 2.165899519708498
}
}
},
"place_id" : "ChIJlyIRgPKipBIRd60v8v6Vc_Y",
"types" : [ "street_address" ]
}
],,
“状态”:“确定”
}
调用此函数并使用新值设置lat和lng,可以在此位置创建一个以该位置为中心的地图组件
function initMap() {
// Create a map object and specify the DOM element for display.
var map = new google.maps.Map(document.getElementById('map'), {
center: {lat: -34.397, lng: 150.644},
scrollwheel: false,
zoom: 8
});
}
在html文件中
<div id="map"></div>
2解决方案:
1:创建一个临时(不可见)地图,搜索“巴塞罗那,Passeig de gracia,32”,并使用返回的lat/lng创建和初始化一个新地图
<div id="tmpMap" style="display: none; width: 0; height: 0;"></div>
<div id="realMap"></div>
<script>
var map = new google.maps.Map(document.getElementById('tmpMap'), {
center: {lat: 0, lng: 0},
zoom: 15
});
var geocoder = new google.maps.Geocoder();
geocoder.geocode({address: 'Barcelona, Passeig de gracia, 32'}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
latlng = results[0].geometry.location;
var map = new google.maps.Map(document.getElementById('realMap'), {
center: results[0].geometry.location,
zoom: 15
});
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
</script>
var map=new google.maps.map(document.getElementById('tmpMap'){
中心:{lat:0,lng:0},
缩放:15
});
var geocoder=new google.maps.geocoder();
geocoder.geocode({地址:'Barcelona,Passeig de gracia,32'},函数(结果,状态){
if(status==google.maps.GeocoderStatus.OK){
latlng=results[0]。geometry.location;
var map=new google.maps.map(document.getElementById('realMap'){
中心:结果[0].geometry.location,
缩放:15
});
}否则{
警报('地理编码因以下原因未成功:'+状态);
}
});
2:创建一张没有初始位置的地图,搜索“巴塞罗那,32岁的帕塞格·德格雷西亚”,并在地图上显示
<div id="map" style="display: none;"></div>
<script>
var map = new google.maps.Map(document.getElementById('map'), {
center: {lat: 0, lng: 0},
zoom: 15
});
var geocoder = new google.maps.Geocoder();
geocoder.geocode({address: 'Barcelona, Passeig de gracia, 32'}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
</script>
var map=new google.maps.map(document.getElementById('map'){
中心:{lat:0,lng:0},
缩放:15
});
var geocoder=new google.maps.geocoder();
geocoder.geocode({地址:'Barcelona,Passeig de gracia,32'},函数(结果,状态){
if(status==google.maps.GeocoderStatus.OK){
map.setCenter(结果[0].geometry.location);
}否则{
警报('地理编码因以下原因未成功:'+状态);
}
});