- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / common / extensions / docs / examples / extensions / mappy / popup.js
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 var maps_key = "ABQIAAAATfHumDbW3OmRByfquHd3SRTRERdeAiwZ9EeJWta3L_JZVS0bOBRQeZgr4K0xyVKzUdnnuFl8X9PX0w";
6
7 function gclient_geocode(address) {
8   var url = 'https://maps.googleapis.com/maps/api/geocode/json?address=' +
9             encodeURIComponent(address) + '&sensor=false';
10   var request = new XMLHttpRequest();
11
12   request.open('GET', url, true);
13   console.log(url);
14   request.onreadystatechange = function (e) {
15     console.log(request, e);
16     if (request.readyState == 4) {
17       if (request.status == 200) {
18         var json = JSON.parse(request.responseText);
19         var latlng = json.results[0].geometry.location;
20         latlng = latlng.lat + ',' + latlng.lng;
21
22         var src = "https://maps.google.com/staticmap?center=" + latlng +
23                   "&markers=" + latlng + "&zoom=14" +
24                   "&size=512x512&sensor=false&key=" + maps_key;
25         var map = document.getElementById("map");
26
27         map.src = src;
28         map.addEventListener('click', function () {
29           window.close();
30         });
31       } else {
32         console.log('Unable to resolve address into lat/lng');
33       }
34     }
35   };
36   request.send(null);
37 }
38
39 function map() {
40   var address = chrome.extension.getBackgroundPage().selectedAddress;
41   if (address)
42     gclient_geocode(address);
43 }
44
45 window.onload = map;