简体   繁体   English

在Rails 3中使用Mapstraction升级Google Maps API

[英]Upgrading google maps api with mapstraction in Rails 3

I'm trying to upgrade an app into Rails 3. The app requires a map that can be edited and it has been using google maps and mapstraction. 我正在尝试将应用程序升级到Rails3。该应用程序需要可编辑的地图,并且一直在使用google maps和mapstraction。 When I upgraded to rails 3 the map stopped showing up and I figured that's because it needed to be updated from google maps api v2 to v3. 当我升级到Rails 3时,地图停止显示,我想这是因为它需要从google maps api v2更新到v3。

I've been trying to use this tutorial to upgrade it but because the app is using mapstraction I've been having difficulty getting it to work. 我一直在尝试使用本教程对其进行升级,但是由于该应用程序正在使用mapstraction,因此我一直难以使其正常工作。

Initially, this was in the html: 最初,这是在html中:

<script src="http://maps.google.com/maps?file=api&v=2&key=<%= GOOGLE_MAPS_KEY %>" type="text/javascript"></script>
<script src="/javascripts/mapstraction/mxn.js" type="text/javascript"></script>
<script src="/javascripts/mapstraction/mxn.core.js" type="text/javascript"></script>
<script src="/javascripts/mapstraction/mxn.google.core.js" type="text/javascript"></script>

And the maps.js was as follows: 而maps.js如下:

var Map = function() {
    this.init()
}

$.extend(Map.prototype, {
    m : false,
    init: function() {
        this.m = new mxn.Mapstraction('mapdiv', 'googlev3')
        this.m.addControls({pan: true, zoom: 'large', map_type: false})
        // this.m.enableScrollWheelZoom()
        this.m.setMapType(3)
        return this.m
    },
    addPoint: function(lat, lng, html) {
        var myPoint = new mxn.LatLonPoint(lat, lng);
        var marker = new mxn.Marker(myPoint);
        marker.setInfoBubble(html);
        this.m.addMarker(marker);
    },
    setCenterAndZoom: function(center, zoom) {
        this.m.setCenterAndZoom(center, zoom)
    },
    boundingBox: function(lat1, lon1, lat2, lon2) {
        var zoom = this.m.getZoomLevelForBoundingBox(new mxn.BoundingBox(lat1, lon1, lat2, lon2))
        var centerlng = (lon1 + lon2) / 2
        var centerlat = (lat1 + lat2) / 2
        var myPoint = new mxn.LatLonPoint(centerlat, centerlng);
        this.m.setCenterAndZoom(myPoint, zoom)
    },
    addPointToLine: function(lat, lng, line, html, length) {
        var point = new mxn.LatLonPoint(lat, lng)
        line.points.push(point);
        var i = line.points.length
        var num = 100
        if (i == 1) {
            m.addPoint(lat, lng, html)
        }
    },
    addUser: function(lat, lng, avatar, html) {
        var marker = new mxn.Marker(new mxn.LatLonPoint(lat, lng));
        marker.setInfoBubble(html);
        marker.setIcon(avatar);
        marker.setIconSize([30,30])
        m.m.addMarker(marker);
    }
});

I tried changing the html to this (based on the upgrade tutorial and the tutorial from mapstraction): 我尝试将html更改为此(基于升级教程和mapstraction的教程):

<script src="//maps.googleapis.com/maps/api/js?sensor=false" type="text/javascript"></script>
<script src="http://mapstraction.com/mxn/build/latest/mxn.js?(googlev3)" type="text/javascript"></script>

But when that didn't make a difference I tried to remove mapstraction and change the maps.js to the following: 但是,如果没有什么不同,我尝试删除mapstraction并将maps.js更改为以下内容:

var Map = function() {
    this.init()
}

$.extend(Map.prototype, {
    m : false,
    init: function() {
        this.m = new google.maps.Map(document.getElementById('mapdiv'), {zoom: 13, mapTypeId: google.maps.MapTypeId.HYBRID});
        // this.m.addControls({pan: true, zoom: 'large', map_type: false})
        // this.m.enableScrollWheelZoom()
        // this.m.setMapType(3)
        return this.m
    },
    addPoint: function(lat, lng, html) {
        var myPoint = new google.maps.LatLng(lat, lng);
        var marker = new google.maps.Marker(myPoint);
        marker.setInfoBubble(html);
        this.m.addMarker(marker);
    },
    setCenterAndZoom: function(center, zoom) {
        this.m.setCenterAndZoom(center, zoom)
    },
    boundingBox: function(lat1, lon1, lat2, lon2) {
        var zoom = this.m.getZoomLevelForBoundingBox(new google.maps.LatLonAltBox(lat1, lon1, lat2, lon2))
        var centerlng = (lon1 + lon2) / 2
        var centerlat = (lat1 + lat2) / 2
        var myPoint = new google.maps.LatLng(centerlat, centerlng);
        this.m.setCenterAndZoom(myPoint, zoom)
    },
    addPointToLine: function(lat, lng, line, html, length) {
        var point = new google.maps.LatLng(lat, lng)
        line.points.push(point);
        var i = line.points.length
        var num = 100
        if (i == 1) {
            m.addPoint(lat, lng, html)
        }
    },
    addUser: function(lat, lng, avatar, html) {
        var marker = new google.maps.Marker(new google.maps.LatLng(lat, lng));
        marker.setInfoBubble(html);
        marker.setIcon(avatar);
        marker.setIconSize([30,30])
        m.m.addMarker(marker);
    }
});

But still nothing...does anyone have any idea how I can get the map to display? 但还是一无所获...有人知道我如何显示地图吗?

Thanks 谢谢

  1. as it seems your code uses jQuery( $.extend ), did you include jQuery? 似乎您的代码使用jQuery( $.extend ),您是否包含jQuery?
  2. all the method you've defined are methods of Map but you try to call these methods for Map.m (which is a google.map.Map -instance and doesn't have such methods) 您定义的所有方法都是Map方法,但是您尝试为Map.m调用这些方法(这是google.map.Map -instance并且没有此类方法)
  3. I don't see where you create the Map -instance(I mean your "class", not the google.maps.Map ) at all 我完全看不到在哪里创建Map -instance(我的意思是您的“类”,而不是google.maps.Map
  4. a google.maps.Map requires a center -property which is not present(at least not when you create the google.maps.Map -instance in Map.init() ) google.maps.Map需要一个不存在的center属性(至少在Map.init()创建google.maps.Map -instance时不存在)

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

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