简体   繁体   English

需要帮助弃用谷歌地图API代码转换为V3

[英]Need help converting deprecated Google Maps API code to V3

I am working on a Foursquare/Google Maps API integration and I am using a script I found on GitHub as a starting point. 我正在进行Foursquare / Google Maps API集成,并且我使用在GitHub上找到的脚本作为起点。 The issue is that this script was written 3 years ago and uses a very early version of jQuery Mobile to render the map. 问题在于该脚本是3年前编写的,并使用jQuery Mobile的早期版本来渲染地图。 I want to get rid of the jQuery Mobile library and use updated Google API V3 code to render the map. 我想摆脱jQuery Mobile库,并使用更新的Google API V3代码来呈现地图。

This is the script using the old library: 这是使用旧库的脚本:

jQuery('#map_canvas').gmap( { 
    'center': new google.maps.LatLng(39.828175, -98.5795), 
    'zoom': 4,  
    'streetViewControl': false, 
    'mapTypeControl': false,
    'mapTypeId': google.maps.MapTypeId.ROADMAP,
    'callback': function (map) {
        if ( navigator.geolocation ) {
            navigator.geolocation.getCurrentPosition ( 
                function( position ) {
                    jQuery.userloc = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
                    addFoodTruckMarker(jQuery.userloc, 'userloc.png', '<p>You are here.</p>');
                    map.panTo(jQuery.userloc);
                    loadFoodTrucks({lat: position.coords.latitude, lng: position.coords.longitude, userloc: jQuery.userloc, zoomtotrucks: true});
                },
                function( error ) {
                    jQuery.mobile.pageLoading( true );
                    alert("Sorry, couldn't find your location.");
                },
                {
                    enableHighAccuracy: true, 
                    maximumAge: (1000 * 60 * 10), 
                    timeout: (1000 * 20)
                }
            );
        }
    }
});

And here is the code that sets the markers: 这是设置标记的代码:

addFoodTruckMarker = function (location, icon, content) {
    jQuery('#map_canvas').gmap('addMarker', { 
        'position': location,
        'icon': icon
    },
    function(map, marker){
        jQuery('#map_canvas').gmap('addInfoWindow', { 
            'position': jQuery.userloc, 
            'content': content
            }, 
            function(iw) {
                jQuery(marker).click(function() {
                    iw.open(map, marker);
                    map.panTo(marker.getPosition());
                });                                                                                                                                                                                                                               
            }
        );
    });
}

Here is my updated script so far. 到目前为止,这是我更新的脚本。 This is working in terms of loading the map, but it's not putting the user location or the Foursquare venue location on the map. 这是在加载地图方面起作用的,但是并没有在地图上放置用户位置或Foursquare场地位置。 I'm assuming it has something to do with the callback(which I just copied into the example code from the Google API V3 docs for the time being): 我假设它与回调有关(目前我刚刚将其复制到Google API V3文档的示例代码中):

function loadMap() {
    var mapOptions = {
      center: new google.maps.LatLng(39.828175, -98.5795),
      zoom: 4,
      streetViewControl: false,
      mapTypeControl: false,
      mapTypeId: google.maps.MapTypeId.ROADMAP,
      callback: function (map) {
        if ( navigator.geolocation ) {
            navigator.geolocation.getCurrentPosition ( 
                function( position ) {
                    jQuery.userloc = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
                    addFoodTruckMarker(jQuery.userloc, 'userloc.png', '<p>You are here.</p>');
                    map.panTo(jQuery.userloc);
                    loadFoodTrucks({lat: position.coords.latitude, lng: position.coords.longitude, userloc: jQuery.userloc, zoomtotrucks: true});
                },
                function( error ) {
                    //jQuery.mobile.pageLoading( true );
                    alert("Sorry, couldn't find your location.");
                },
                {
                    enableHighAccuracy: true, 
                    maximumAge: (1000 * 60 * 10), 
                    timeout: (1000 * 20)
                }
            );
        }
      }
    };
    var map = new google.maps.Map(document.getElementById("map_canvas"),
        mapOptions);
}
google.maps.event.addDomListener(window, 'load', loadMap);

Can anyone point me in the right direction? 谁能指出我正确的方向? Thanks! 谢谢!

There is no callback-option for a map(the function never will be executed). 映射没有回调选项(该函数永远不会执行)。

You may implement it on your own by adding this to the end of loadMap() : 您可以通过将其添加到loadMap()的末尾来自己实现:

//execute callback when the map has been finished initializing
google.maps.event.addListenerOnce(map,'bounds_changed',function(){this.callback();})

But there is no need to wait for anything, you may execute the code inside callback directly (at the end of loadMap). 但是不需要等待任何东西,您可以直接在callback内部执行代码(在loadMap的末尾)。 Of course you'll also need to update addFoodTruckMarker , because it still requires jquery-ui-map 当然,您还需要更新addFoodTruckMarker ,因为它仍然需要jquery-ui-map

Note: libraries may be updated, there is a newer version of jquery-ui-map, the only change that is related to your code is the method addInfoWindow that has been removed, you'll need to update addFoodTruckMarker : 注意:库可能已更新,jquery-ui-map有较新的版本,与您的代码相关的唯一更改是已删除的方法addInfoWindow ,您需要更新addFoodTruckMarker

addFoodTruckMarker = function (location, icon, content) {
    jQuery('#map_canvas').gmap('addMarker', { 
        'position': location,
        'icon': icon
    }).click(function() {
        $('#map_canvas').gmap('openInfoWindow', { 'content': content }, this);
    }); 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM