Javascript 为什么';google maps api是否可以与动态lat/long一起工作?
我需要根据CMS中用户设置的地址在地图上放置一个自定义标记。通过使用该地址,我希望获得该区域的lat/long,并通过使用谷歌地图api,将其放置在正确的位置。这是我的密码:Javascript 为什么';google maps api是否可以与动态lat/long一起工作?,javascript,google-maps,google-maps-api-3,Javascript,Google Maps,Google Maps Api 3,我需要根据CMS中用户设置的地址在地图上放置一个自定义标记。通过使用该地址,我希望获得该区域的lat/long,并通过使用谷歌地图api,将其放置在正确的位置。这是我的密码: function getLatLong(address, callback){ var geo = new google.maps.Geocoder; geo.geocode({'address':address},function(results, status){ if (status
function getLatLong(address, callback){
var geo = new google.maps.Geocoder;
geo.geocode({'address':address},function(results, status){
if (status == google.maps.GeocoderStatus.OK) {
callback (results[0].geometry.location.k + ',' + results[0].geometry.location.D);
} else {
return;
}
});
}
$(document).ready(function(){
getLatLong("<?= $r['address'] ?>", function(addr){
console.log(addr);
function initialize() {
var mapOptions = {
zoom: 15,
scrollwheel: false,
center: new google.maps.LatLng(addr)
}
var map = new google.maps.Map(document.getElementById('footer-map'),
mapOptions);
var image = '<?= parseLink("images/map-marker.png") ?>';
var myLatLng = new google.maps.LatLng(addr);
var beachMarker = new google.maps.Marker({
position: myLatLng,
map: map,
icon: image
});
console.log('loaded');
}
google.maps.event.addDomListener(window, 'load', initialize);
})
});
<script>
function getLatLong(address, callback){
var geo = new google.maps.Geocoder;
geo.geocode({'address':address},function(results, status){
if (status == google.maps.GeocoderStatus.OK) {
console.log(results[0].geometry.location);
callback(results[0].geometry.location);} else{return;} });}
$(document).ready(function(){
getLatLong("indore", function(addr){
console.log(addr);
var mapOptions = {
zoom: 4,
center: addr
}
var map = new google.maps.Map(document.getElementById('map-canvas'),mapOptions);
var myLatLng = addr;//new google.maps.LatLng(addr);
var beachMarker = new google.maps.Marker({
position: myLatLng,
map: map
});
console.log('loaded');})
});</script></head>
函数getLatLong(地址,回调){
var geo=new google.maps.Geocoder;
geo.geocode({'address':address},函数(结果,状态){
if(status==google.maps.GeocoderStatus.OK){
回调(结果[0].geometry.location.k+','+结果[0].geometry.location.D);
}否则{
返回;
}
});
}
$(文档).ready(函数(){
getLatLong(“),函数(addr){
控制台日志(addr);
函数初始化(){
变量映射选项={
缩放:15,
滚轮:错误,
中心:新google.maps.LatLng(地址)
}
var map=new google.maps.map(document.getElementById('footer-map'),
地图选项);
var图像=“”;
var mylatng=new google.maps.LatLng(addr);
var beachMarker=new google.maps.Marker({
职位:myLatLng,
地图:地图,
图标:图像
});
console.log('loaded');
}
google.maps.event.addDomListener(窗口“加载”,初始化);
})
});
因此,它返回带有lat/long的console.log(addr)
,如果我在center:new google.maps.LatLng(addr)
中复制该值,而不是addr
,一切正常,但如果我将addr
留在那里,它就不会加载地图
<script>
function getLatLong(address, callback){
var geo = new google.maps.Geocoder;
geo.geocode({'address':address},function(results, status){
if (status == google.maps.GeocoderStatus.OK) {
console.log(results[0].geometry.location);
callback(results[0].geometry.location);} else{return;} });}
$(document).ready(function(){
getLatLong("indore", function(addr){
console.log(addr);
var mapOptions = {
zoom: 4,
center: addr
}
var map = new google.maps.Map(document.getElementById('map-canvas'),mapOptions);
var myLatLng = addr;//new google.maps.LatLng(addr);
var beachMarker = new google.maps.Marker({
position: myLatLng,
map: map
});
console.log('loaded');})
});</script></head>
我做错了什么
<script>
function getLatLong(address, callback){
var geo = new google.maps.Geocoder;
geo.geocode({'address':address},function(results, status){
if (status == google.maps.GeocoderStatus.OK) {
console.log(results[0].geometry.location);
callback(results[0].geometry.location);} else{return;} });}
$(document).ready(function(){
getLatLong("indore", function(addr){
console.log(addr);
var mapOptions = {
zoom: 4,
center: addr
}
var map = new google.maps.Map(document.getElementById('map-canvas'),mapOptions);
var myLatLng = addr;//new google.maps.LatLng(addr);
var beachMarker = new google.maps.Marker({
position: myLatLng,
map: map
});
console.log('loaded');})
});</script></head>
结果[0].geometry.location.k+','+结果[0].geometry.location.D
,因为结果[0].geometry.location
本身就是一个LatLng对象<script>
function getLatLong(address, callback){
var geo = new google.maps.Geocoder;
geo.geocode({'address':address},function(results, status){
if (status == google.maps.GeocoderStatus.OK) {
console.log(results[0].geometry.location);
callback(results[0].geometry.location);} else{return;} });}
$(document).ready(function(){
getLatLong("indore", function(addr){
console.log(addr);
var mapOptions = {
zoom: 4,
center: addr
}
var map = new google.maps.Map(document.getElementById('map-canvas'),mapOptions);
var myLatLng = addr;//new google.maps.LatLng(addr);
var beachMarker = new google.maps.Marker({
position: myLatLng,
map: map
});
console.log('loaded');})
});</script></head>
<script>
function getLatLong(address, callback){
var geo = new google.maps.Geocoder;
geo.geocode({'address':address},function(results, status){
if (status == google.maps.GeocoderStatus.OK) {
console.log(results[0].geometry.location);
callback(results[0].geometry.location);} else{return;} });}
$(document).ready(function(){
getLatLong("indore", function(addr){
console.log(addr);
var mapOptions = {
zoom: 4,
center: addr
}
var map = new google.maps.Map(document.getElementById('map-canvas'),mapOptions);
var myLatLng = addr;//new google.maps.LatLng(addr);
var beachMarker = new google.maps.Marker({
position: myLatLng,
map: map
});
console.log('loaded');})
});</script></head>
<script>
function getLatLong(address, callback){
var geo = new google.maps.Geocoder;
geo.geocode({'address':address},function(results, status){
if (status == google.maps.GeocoderStatus.OK) {
console.log(results[0].geometry.location);
callback(results[0].geometry.location);} else{return;} });}
$(document).ready(function(){
getLatLong("indore", function(addr){
console.log(addr);
var mapOptions = {
zoom: 4,
center: addr
}
var map = new google.maps.Map(document.getElementById('map-canvas'),mapOptions);
var myLatLng = addr;//new google.maps.LatLng(addr);
var beachMarker = new google.maps.Marker({
position: myLatLng,
map: map
});
console.log('loaded');})
});</script></head>
函数getLatLong(地址,回调){
var geo=new google.maps.Geocoder;
geo.geocode({'address':address},函数(结果,状态){
if(status==google.maps.GeocoderStatus.OK){
console.log(结果[0].geometry.location);
回调(结果[0].geometry.location);}else{return;}}}});}
$(文档).ready(函数(){
getLatLong(“indore”,函数(addr){
控制台日志(addr);
变量映射选项={
缩放:4,
中心:地址
}
var map=new google.maps.map(document.getElementById('map-canvas'),mapOptions);
var mylatng=addr;//新建google.maps.LatLng(addr);
var beachMarker=new google.maps.Marker({
职位:myLatLng,
地图:地图
});
console.log('loaded');})
});
”
和标记图像,请重新添加不太清楚为什么要在document.ready中保留initialize函数。理想情况下,它应该像getLatLong一样位于外部。其次,离开addr必须复制lat lng,因为单个参数ex'81.32','12.1'将像'81.32,12.1'一样复制,这就是为什么它不是working@jsjunkie你能发布正确的Syntax吗?非常感谢:)