Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么';google maps api是否可以与动态lat/long一起工作?_Javascript_Google Maps_Google Maps Api 3 - Fatal编程技术网

Javascript 为什么';google maps api是否可以与动态lat/long一起工作?

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

我需要根据CMS中用户设置的地址在地图上放置一个自定义标记。通过使用该地址,我希望获得该区域的lat/long,并通过使用谷歌地图api,将其放置在正确的位置。这是我的密码:

  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吗?非常感谢:)