{"id":65,"date":"2011-05-12T06:00:31","date_gmt":"2011-05-12T16:00:31","guid":{"rendered":"http:\/\/mymonkeydo.com\/?p=65"},"modified":"2020-05-03T16:44:10","modified_gmt":"2020-05-04T02:44:10","slug":"converting-google-maps-v2-to-v3","status":"publish","type":"post","link":"https:\/\/mymonkeydo.com\/converting-google-maps-v2-to-v3\/","title":{"rendered":"Converting Google Maps v2 to v3"},"content":{"rendered":"

Problem:<\/h2>\n

I was given a project where the client switched from using http to https. \u00a0Everything worked fine except for their Google Maps page. \u00a0The page \u00a0had a warning in IE about unsecure mixed content. \u00a0This was because Google Maps was pulling from an http (unsecured) address. \u00a0It turns out that the Map was using version 2 of Google Maps API which does not support https. \u00a0Only version 3 does.<\/p>\n

Solution:<\/h2>\n

Don’t be fooled into thinking this is a straight forward, simple upgrade. \u00a0It is not. \u00a0This solution is just a log of what I did to get the site from v2 up to v3. \u00a0It is in no way \u00a0a complete guide. \u00a0The site used EWindow instead of the infoWindow because of its customized look. \u00a0So I guess the following could be a rough step-by-step guide to converting the Ewindow script to v3.<\/p>\n


\n<\/strong><\/p>\n

In general you’ll need to replace all “G” prefixes like “GSize” with “google.maps” like “google.maps.Size”.<\/strong><\/p>\n

Replace GSize with google.maps.Size
\nReplace GLatLng with google.maps.LatLng
\nReplace GPoint with google.maps.Point
\nReplace GOverlay with google.maps.Overlay<\/p>\n

Replace “new google.maps.Overlay” with “new google.maps.OverlayView()”<\/strong><\/p>\n

Take out or replace GBrowserIsCompatible. There is no equivalent function in v3.<\/strong><\/p>\n

Replace<\/strong><\/p>\n

map = new GMap2(document.getElementById(\"map_canvas\"))<\/pre>\n

with something like:<\/p>\n

    var myOptions = {\n      zoom: 4,\n      center: myLatlng,\n      mapTypeId: google.maps.MapTypeId.ROADMAP\n    }\n    map = new google.maps.Map(document.getElementById(\"map_canvas\"), myOptions);<\/pre>\n

Replace a custom icon marker using the icon-complex example<\/strong>:
\n http:\/\/code.google.com\/apis\/maps\/documentation\/javascript\/examples\/icon-complex.html<\/a><\/p>\n

Before the change:<\/p>\n

var tinyIcon = new GIcon();\ntinyIcon.image = \"images\/gmap\/marker.gif\";\ntinyIcon.iconSize = new google.maps.Size(22, 22);\ntinyIcon.iconAnchor = new google.maps.Point(11, 11);\ntinyIcon.infoWindowAnchor = new google.maps.Point(0, 0);\nvar myLatLng = new google.maps.LatLng(loc['lat'], loc['lng']);\nvar marker = new GMarker(myLatLng, { icon:tinyIcon });\nGEvent.addListener(marker, showWindow, mouseover);\nmap.addOverlay(marker);<\/pre>\n

After:<\/p>\n

var image = new google.maps.MarkerImage('images\/gmap\/marker.gif',\n  \/\/ This marker is 20 pixels wide by 32 pixels tall.\n  new google.maps.Size(22, 22),\n  \/\/ The origin for this image is 0,0.\n  new google.maps.Point(0,0),\n  \/\/ The anchor for this image is the base of the flagpole at 0,32.\n  new google.maps.Point(11, 11));\n\nvar myLatLng = new google.maps.LatLng(loc['lat'], loc['lng']);\nvar marker = new google.maps.Marker({\n    position: myLatLng,\n    map: map,\n    shadow: shadow,\n    icon: image\n});\ngoogle.maps.event.addDomListener(marker,'mouseover', showWindow);<\/pre>\n

Replace getPoint to getPosition<\/strong><\/p>\n

var vx = marker.getIcon().iconAnchor.x - marker.getIcon().infoWindowAnchor.x;\nvar vy = marker.getIcon().iconAnchor.y - marker.getIcon().infoWindowAnchor.y;\nthis.openOnMap(marker.getPoint(), html, new google.maps.Point(vx,vy));<\/pre>\n

For infoWindowAnchor I could not find an equivalent so I scratched the whole calculation and just put in a hard coded value.<\/p>\n

var vx = 10;\nvar vy = 10;\nthis.openOnMap(marker.getPosition(), html, new google.maps.Point(vx,vy));<\/pre>\n

Replace initialize:<\/strong><\/p>\n

EWindow.prototype.initialize = function(map) {<\/pre>\n

With onAdd:<\/p>\n

EWindow.prototype.onAdd = function() {<\/pre>\n

Replace map.addOverlay(object) by moving a similar call to within the object.<\/strong><\/p>\n

map.addOverlay(ewindow)<\/pre>\n

To this within the object:<\/p>\n

this.setMap(map)<\/pre>\n

Replace map.getPane()<\/strong><\/p>\n

map.getPane(G_MAP_FLOAT_SHADOW_PANE)<\/pre>\n

With this:<\/p>\n

this.getPanes().mapPane<\/pre>\n

Replace this.redraw<\/strong><\/p>\n

this.redraw<\/pre>\n

with this.draw<\/p>\n

this.draw<\/pre>\n

Replace Overlay.getZindex<\/strong><\/p>\n

var z = google.maps.Overlay.getZIndex(this.point.lat());<\/pre>\n

I could not find an equivalent to getZIndex so, again, it’s somewhat hard coded.<\/p>\n

\/\/ you may need to work on this \"hack\" to replace V2 getZindex\n\/\/ GOverlay.getZIndex(this.point.lat());\nvar z = 1000*(90-this.point.lat());\nthis.div_.style.zIndex = parseInt(z);<\/pre>\n

Replace this.map.fromLatLngToDivPixel<\/strong><\/p>\n

var p = this.map.fromLatLngToDivPixel(this.point);<\/pre>\n

With this:<\/p>\n

var proj = this.getProjection();\nvar p = proj.fromLatLngToDivPixel(this.point);<\/pre>\n

Replace map.panBy(Size) to map.panBy(x:number, y:number)<\/strong><\/p>\n

map.panBy(new google.maps.Size(pan_right, pan_up));<\/pre>\n

With this:<\/p>\n

map.panBy(-pan_right, -pan_up);<\/pre>\n

A very helpful site was one that made google maps compatible for both versions:
\n http:\/\/notebook.kulchenko.com\/maps\/google-maps-using-multiple-api-versions<\/a><\/p>\n

And of course the Google Maps v3 Examples and API<\/a> docs helped a great deal. \u00a0It may also help to look at version 2 <\/a>of the API so that you can try and find something equivalent.<\/p>\n

I uploaded a converted EWindow.js for whoever needs a google maps v3 version of EWindow.<\/p>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","footnotes":""},"categories":[2],"tags":[27,39],"yoast_head":"\nConverting Google Maps v2 to v3 - My Monkey Do<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/mymonkeydo.com\/converting-google-maps-v2-to-v3\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Converting Google Maps v2 to v3 - My Monkey Do\" \/>\n<meta property=\"og:url\" content=\"https:\/\/mymonkeydo.com\/converting-google-maps-v2-to-v3\/\" \/>\n<meta property=\"og:site_name\" content=\"My Monkey Do\" \/>\n<meta property=\"article:published_time\" content=\"2011-05-12T16:00:31+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-05-04T02:44:10+00:00\" \/>\n<meta name=\"author\" content=\"Webhead\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Webhead\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/mymonkeydo.com\/converting-google-maps-v2-to-v3\/\",\"url\":\"https:\/\/mymonkeydo.com\/converting-google-maps-v2-to-v3\/\",\"name\":\"Converting Google Maps v2 to v3 - My Monkey Do\",\"isPartOf\":{\"@id\":\"https:\/\/mymonkeydo.com\/#website\"},\"datePublished\":\"2011-05-12T16:00:31+00:00\",\"dateModified\":\"2020-05-04T02:44:10+00:00\",\"author\":{\"@id\":\"https:\/\/mymonkeydo.com\/#\/schema\/person\/b16fc650a8c182faaac896bab099b829\"},\"breadcrumb\":{\"@id\":\"https:\/\/mymonkeydo.com\/converting-google-maps-v2-to-v3\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/mymonkeydo.com\/converting-google-maps-v2-to-v3\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/mymonkeydo.com\/converting-google-maps-v2-to-v3\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/mymonkeydo.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Converting Google Maps v2 to v3\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/mymonkeydo.com\/#website\",\"url\":\"https:\/\/mymonkeydo.com\/\",\"name\":\"My Monkey Do\",\"description\":\"A Log of Coding Solutions\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/mymonkeydo.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/mymonkeydo.com\/#\/schema\/person\/b16fc650a8c182faaac896bab099b829\",\"name\":\"Webhead\",\"url\":\"https:\/\/mymonkeydo.com\/author\/corey\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Converting Google Maps v2 to v3 - My Monkey Do","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/mymonkeydo.com\/converting-google-maps-v2-to-v3\/","og_locale":"en_US","og_type":"article","og_title":"Converting Google Maps v2 to v3 - My Monkey Do","og_url":"https:\/\/mymonkeydo.com\/converting-google-maps-v2-to-v3\/","og_site_name":"My Monkey Do","article_published_time":"2011-05-12T16:00:31+00:00","article_modified_time":"2020-05-04T02:44:10+00:00","author":"Webhead","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Webhead","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/mymonkeydo.com\/converting-google-maps-v2-to-v3\/","url":"https:\/\/mymonkeydo.com\/converting-google-maps-v2-to-v3\/","name":"Converting Google Maps v2 to v3 - My Monkey Do","isPartOf":{"@id":"https:\/\/mymonkeydo.com\/#website"},"datePublished":"2011-05-12T16:00:31+00:00","dateModified":"2020-05-04T02:44:10+00:00","author":{"@id":"https:\/\/mymonkeydo.com\/#\/schema\/person\/b16fc650a8c182faaac896bab099b829"},"breadcrumb":{"@id":"https:\/\/mymonkeydo.com\/converting-google-maps-v2-to-v3\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/mymonkeydo.com\/converting-google-maps-v2-to-v3\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/mymonkeydo.com\/converting-google-maps-v2-to-v3\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/mymonkeydo.com\/"},{"@type":"ListItem","position":2,"name":"Converting Google Maps v2 to v3"}]},{"@type":"WebSite","@id":"https:\/\/mymonkeydo.com\/#website","url":"https:\/\/mymonkeydo.com\/","name":"My Monkey Do","description":"A Log of Coding Solutions","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/mymonkeydo.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/mymonkeydo.com\/#\/schema\/person\/b16fc650a8c182faaac896bab099b829","name":"Webhead","url":"https:\/\/mymonkeydo.com\/author\/corey\/"}]}},"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/mymonkeydo.com\/wp-json\/wp\/v2\/posts\/65"}],"collection":[{"href":"https:\/\/mymonkeydo.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mymonkeydo.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mymonkeydo.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mymonkeydo.com\/wp-json\/wp\/v2\/comments?post=65"}],"version-history":[{"count":0,"href":"https:\/\/mymonkeydo.com\/wp-json\/wp\/v2\/posts\/65\/revisions"}],"wp:attachment":[{"href":"https:\/\/mymonkeydo.com\/wp-json\/wp\/v2\/media?parent=65"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mymonkeydo.com\/wp-json\/wp\/v2\/categories?post=65"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mymonkeydo.com\/wp-json\/wp\/v2\/tags?post=65"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}