SlideShare une entreprise Scribd logo
1  sur  37
Google Maps + JSON = Sant

PROSJEKT: BYSYKKEL
Hva er Oslo Bysykkel?
• Oslo Bysykkel er et samarbeid mellom
  Clear Channel Norway AS og Oslo
  Kommune
• Bysykkelordningen er reklamefinansiert
• Ordningen startet i 2003. Den gang ble
  det lånt 284.561 sykler. I 2012 var tallet
  1.017.015.


                                     Osloby.no (2012) & bysykler.no (2012)
Bakgrunn for prosjektet
• Jeg kom alt for ofte til et tomt stativ
• Ville finne ut hvor og når det var mulig å få
  tak i en sykkel
• Hentet ned data fra Bysykkel og YR
• Brukte det til å finne ut når det var
  statistisk sannsynlig å finne en ledig
  sykkel
• Begynte å leke med Google Maps
Heatmap: Oslo
Sykkelstativ: Oslo
OK, men hva er verdien?
Google Maps Workshop
Google Maps
Google Maps
Google Maps
<script type="text/javascript" src=
"https://maps.googleapis.com/maps/api/js?sensor=false">
</script>

mapOptions = {
   zoom: 2,
   center: new google.maps.LatLng(0, 0),
   mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(
      document.getElementById('map_canvas'),
          mapOptions);
Markers
Markers
var marker =
   new google.maps.Marker({
   position: new google.maps.LatLng(
        60.393777,
        5.324708),
   title: "Bergen",
   map: map
   });
JSON – Bergen Dokart
Hva er JSON?
• JSON (Javascript Object Notation) er
  syntaks (kode) for å lagre og utveksle
  informasjon.
• Nettjenester returnerer gjerne både XML
  og JSON.
(W3C, 2013)
Ett JSON-objekt
• Ett JSON-objekt lagrer informasjon om én
  ting, for eksempel en person:
  – {name: “Peter”, mobile: 45454545…osv.}
En JSON-objektliste
• En JSON-objektliste har som hensikt å
  lagre mange JSON-objekter sammen:

  {personer:[
     {name: “Peter”, mobile: 45454545},
     {name: “Julie”, mobile: 45885454},
     {name: “Lisa”, mobile: 45956475}
  ]}
JSONP
• Vanlig feilmelding:
  XMLHttpRequest cannot load
  http://api.citybik.es/bysykkel.json. Origin
  http://dittdomene.com is not allowed by
  Access-Control-Allow-Origin.

• Løsning: JSONP - JSON with Padding
• En kommunikasjonsteknikk for å be om data
  fra en server på et annet domene, noe som
  er forbudt av typiske nettlesere
JSON vs JSONP
JSON:                JSONP:

{                    callback(
    "Name": "Foo",      {
    "Id": 1234,            "Name": "Foo",
                           "Id": 1234,
    "Rank": 7
                           "Rank": 7
}
                        }
                     );
jQuery - JSONP - GET
<script type="text/javascript" src=
"http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js?
ver=1.9.1"></script>

$.ajax({
    type: 'GET',
    url:
         'http://hotell.difi.no/api/jsonp/bergen/dokart',
    dataType: 'jsonp',
    success: hentToaletter,
    jsonpCallback: 'callback'
});
Dokart - JSONP - Resultat
callback(
    {"entries":[
       {"pissoir_only":"NULL",
            "adresse":"Rådhusgaten 10",
            "rullestol":"1",
            "id":"1",
            "tid_lordag":"NULL",
            "plassering":"RÅDHUSET",
            "longitude":"5.32812829999999",
            "latitude":"60.3920969"},
       {..}], "page":1,"pages":1,"posts":11});
Markers
function hentToaletter(response) {
   entries = response["entries"];
   for(var item in entries) {
        var marker = new google.maps.Marker({
            position: new google.maps.LatLng(
                 entries[item]["latitude"],
                 entries[item]["longitude"]),
            title: entries[item]["plassering"],
            map: map
        });
   }
}
MarkerImage
MarkerImage
var markerImage = {
   url:
        "http://icons.iconarchive.com/icons/rokey/
        smooth/32/toilet-paper-icon.png",
   size: new google.maps.Size(32, 32),
   origin: new google.maps.Point(0, 0),
   anchor: new google.maps.Point(16, 16),
   scaledSize: new google.maps.Size(32, 32)
};

marker.setIcon(markerImage);
MarkerCluster
MarkerCluster
<script type="text/javascript" src="
http://google-maps-utility-library-v3.googlecode.com/svn/trunk/markerc
"></script>

var markers[];
var marker = new google.maps.Marker({…});
markers.push(marker);

var mcOptions = {
             gridSize: 40,
             maxZoom: 15
          };
var mc = new MarkerClusterer(map, markers, mcOptions);
InfoWindow
InfoWindow (1/4)
var pin = new google.maps.MVCObject();

var title = document.createElement("DIV");
var streetview = document.createElement("DIV");
var content = document.createElement("DIV");
content.appendChild(title);
content.appendChild(streetview);

var infowindow = new google.maps.InfoWindow({
    content: content
});
InfoWindow (2/4)
google.maps.event.addListenerOnce(
    infowindow,
    "domready",
    function () {
        panorama = new
           google.maps.StreetViewPanorama(streetview);
        panorama.bindTo("position", pin);
});
Hente dataene fra citybik.es
$.ajax({
    type: "GET",
    url: "http://api.citybik.es/bysykkel.json",
    dataType: "jsonp",
    success: getRacks
});

function getRacks(response) {
   for (var i = 0; i < response.length; i++) {
           newMarker(response[i]);
        }
}
Citybik.es - Resultat
[{
       "name": "01-Middelthunsgate (vis-a-vis nr. 21, retning Kirkeveien)",
       "idx": 0,
       "timestamp": "2013-04-05T13:01:03.425838",
       "number": 1,
       "free": 6,
       "bikes": 12,
       "coordinates": "",
       "online": 1,
       "lat": 59927630,
       "lng": 10710070,
       "id": 0
     },
     {…}, {…}
]
InfoWindow (3/4)
function newMarker(data) {
   var marker = new google.maps.Marker({…});

    google.maps.event.addListener(
       marker,
       "click",
       function () {
           openInfoWindow(marker);
       }
    );
}
InfoWindow (4/4)
function openInfoWindow(m) {
   title.innerHTML = "Sykkelstativ";
   pin.set("position", m.getPosition());
   infowindow.open(map, m);
}
Heatmap
Heatmap (1/2)
var racks = [];

for (var i = 0; i < response.length; i++) {
    racks.push({
        location: new google.maps.LatLng(
            response[i]["lat"] / 1E6,
            response[i]["lng"] / 1E6),
        weight: response[i]["bikes"]
    })
}
Heatmap (2/2)
heatmap = new google.maps.visualization.HeatmapLayer({
     data: racks,
     radius: 30,
     gradient: [
         'rgba(0, 0, 0, 0)',
         'rgba(0, 0, 255, 1)',
         'rgba(0, 255, 0, 1)',
         'rgba(255, 0, 0, 1)'],
         opacity: 0.5
     });
heatmap.setMap(map);
Prosjekt: Bysykkel - Alt kombinert
Referanse
• Google (2013): Google Maps Javascript API V3
  Reference,
  https://developers.google.com/maps/documentatio
  n/javascript/reference
• jQuery (2013): jQuery API, http://api.jquery.com/
• MarkerCluster (2013): MarkerCluster for Google
  Maps v3 , http://google-maps-utility-library-
  v3.googlecode.com/svn/trunk/markerclusterer/doc
  s/reference.html
• W3C (2013): Javascript/JSON,
  http://www.w3schools.com/json/default.asp

Contenu connexe

En vedette

AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 

En vedette (20)

AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 

Innføring i Google Maps APIet

  • 1. Google Maps + JSON = Sant PROSJEKT: BYSYKKEL
  • 2. Hva er Oslo Bysykkel? • Oslo Bysykkel er et samarbeid mellom Clear Channel Norway AS og Oslo Kommune • Bysykkelordningen er reklamefinansiert • Ordningen startet i 2003. Den gang ble det lånt 284.561 sykler. I 2012 var tallet 1.017.015. Osloby.no (2012) & bysykler.no (2012)
  • 3. Bakgrunn for prosjektet • Jeg kom alt for ofte til et tomt stativ • Ville finne ut hvor og når det var mulig å få tak i en sykkel • Hentet ned data fra Bysykkel og YR • Brukte det til å finne ut når det var statistisk sannsynlig å finne en ledig sykkel • Begynte å leke med Google Maps
  • 6. OK, men hva er verdien?
  • 10. Google Maps <script type="text/javascript" src= "https://maps.googleapis.com/maps/api/js?sensor=false"> </script> mapOptions = { zoom: 2, center: new google.maps.LatLng(0, 0), mapTypeId: google.maps.MapTypeId.ROADMAP }; map = new google.maps.Map( document.getElementById('map_canvas'), mapOptions);
  • 12. Markers var marker = new google.maps.Marker({ position: new google.maps.LatLng( 60.393777, 5.324708), title: "Bergen", map: map });
  • 13. JSON – Bergen Dokart
  • 14. Hva er JSON? • JSON (Javascript Object Notation) er syntaks (kode) for å lagre og utveksle informasjon. • Nettjenester returnerer gjerne både XML og JSON. (W3C, 2013)
  • 15. Ett JSON-objekt • Ett JSON-objekt lagrer informasjon om én ting, for eksempel en person: – {name: “Peter”, mobile: 45454545…osv.}
  • 16. En JSON-objektliste • En JSON-objektliste har som hensikt å lagre mange JSON-objekter sammen: {personer:[ {name: “Peter”, mobile: 45454545}, {name: “Julie”, mobile: 45885454}, {name: “Lisa”, mobile: 45956475} ]}
  • 17. JSONP • Vanlig feilmelding: XMLHttpRequest cannot load http://api.citybik.es/bysykkel.json. Origin http://dittdomene.com is not allowed by Access-Control-Allow-Origin. • Løsning: JSONP - JSON with Padding • En kommunikasjonsteknikk for å be om data fra en server på et annet domene, noe som er forbudt av typiske nettlesere
  • 18. JSON vs JSONP JSON: JSONP: { callback( "Name": "Foo", { "Id": 1234, "Name": "Foo", "Id": 1234, "Rank": 7 "Rank": 7 } } );
  • 19. jQuery - JSONP - GET <script type="text/javascript" src= "http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js? ver=1.9.1"></script> $.ajax({ type: 'GET', url: 'http://hotell.difi.no/api/jsonp/bergen/dokart', dataType: 'jsonp', success: hentToaletter, jsonpCallback: 'callback' });
  • 20. Dokart - JSONP - Resultat callback( {"entries":[ {"pissoir_only":"NULL", "adresse":"Rådhusgaten 10", "rullestol":"1", "id":"1", "tid_lordag":"NULL", "plassering":"RÅDHUSET", "longitude":"5.32812829999999", "latitude":"60.3920969"}, {..}], "page":1,"pages":1,"posts":11});
  • 21. Markers function hentToaletter(response) { entries = response["entries"]; for(var item in entries) { var marker = new google.maps.Marker({ position: new google.maps.LatLng( entries[item]["latitude"], entries[item]["longitude"]), title: entries[item]["plassering"], map: map }); } }
  • 23. MarkerImage var markerImage = { url: "http://icons.iconarchive.com/icons/rokey/ smooth/32/toilet-paper-icon.png", size: new google.maps.Size(32, 32), origin: new google.maps.Point(0, 0), anchor: new google.maps.Point(16, 16), scaledSize: new google.maps.Size(32, 32) }; marker.setIcon(markerImage);
  • 25. MarkerCluster <script type="text/javascript" src=" http://google-maps-utility-library-v3.googlecode.com/svn/trunk/markerc "></script> var markers[]; var marker = new google.maps.Marker({…}); markers.push(marker); var mcOptions = { gridSize: 40, maxZoom: 15 }; var mc = new MarkerClusterer(map, markers, mcOptions);
  • 27. InfoWindow (1/4) var pin = new google.maps.MVCObject(); var title = document.createElement("DIV"); var streetview = document.createElement("DIV"); var content = document.createElement("DIV"); content.appendChild(title); content.appendChild(streetview); var infowindow = new google.maps.InfoWindow({ content: content });
  • 28. InfoWindow (2/4) google.maps.event.addListenerOnce( infowindow, "domready", function () { panorama = new google.maps.StreetViewPanorama(streetview); panorama.bindTo("position", pin); });
  • 29. Hente dataene fra citybik.es $.ajax({ type: "GET", url: "http://api.citybik.es/bysykkel.json", dataType: "jsonp", success: getRacks }); function getRacks(response) { for (var i = 0; i < response.length; i++) { newMarker(response[i]); } }
  • 30. Citybik.es - Resultat [{ "name": "01-Middelthunsgate (vis-a-vis nr. 21, retning Kirkeveien)", "idx": 0, "timestamp": "2013-04-05T13:01:03.425838", "number": 1, "free": 6, "bikes": 12, "coordinates": "", "online": 1, "lat": 59927630, "lng": 10710070, "id": 0 }, {…}, {…} ]
  • 31. InfoWindow (3/4) function newMarker(data) { var marker = new google.maps.Marker({…}); google.maps.event.addListener( marker, "click", function () { openInfoWindow(marker); } ); }
  • 32. InfoWindow (4/4) function openInfoWindow(m) { title.innerHTML = "Sykkelstativ"; pin.set("position", m.getPosition()); infowindow.open(map, m); }
  • 34. Heatmap (1/2) var racks = []; for (var i = 0; i < response.length; i++) { racks.push({ location: new google.maps.LatLng( response[i]["lat"] / 1E6, response[i]["lng"] / 1E6), weight: response[i]["bikes"] }) }
  • 35. Heatmap (2/2) heatmap = new google.maps.visualization.HeatmapLayer({ data: racks, radius: 30, gradient: [ 'rgba(0, 0, 0, 0)', 'rgba(0, 0, 255, 1)', 'rgba(0, 255, 0, 1)', 'rgba(255, 0, 0, 1)'], opacity: 0.5 }); heatmap.setMap(map);
  • 36. Prosjekt: Bysykkel - Alt kombinert
  • 37. Referanse • Google (2013): Google Maps Javascript API V3 Reference, https://developers.google.com/maps/documentatio n/javascript/reference • jQuery (2013): jQuery API, http://api.jquery.com/ • MarkerCluster (2013): MarkerCluster for Google Maps v3 , http://google-maps-utility-library- v3.googlecode.com/svn/trunk/markerclusterer/doc s/reference.html • W3C (2013): Javascript/JSON, http://www.w3schools.com/json/default.asp

Notes de l'éditeur

  1. Kilder http://www.osloby.no/nyheter/Bysykkelsesongen-starter-i-dag-7163004.html#.UV7KWZPOt8E http://www.bysykler.no/oslo/generell-informasjon/hva-er-oslo-bysykkel
  2. Kilde: https://developers.google.com/maps/
  3. http://urbye.net/kart.html &lt;script type=&quot;text/javascript&quot; src=&quot; https://maps.googleapis.com/maps/api/js?sensor=false &quot;&gt;&lt;/script&gt;
  4. http://urbye.net/kart.html
  5. http://urbye.net/dokart.html
  6. http://urbye.net/dokart.html
  7. http://urbye.net/dokart.html
  8. http://hotell.difi.no/api/jsonp/bergen/dokart?
  9. http://urbye.net/dokart.html
  10. http://urbye.net/dokart.html
  11. http://urbye.net/dokart.html
  12. http://urbye.net/bom.html
  13. JSONP-Kilde: http://hotell.difi.no/api/json/vegvesen/bomstasjoner?
  14. http://urbye.net/sykkel.html
  15. http://urbye.net/sykkel.html
  16. http://urbye.net/sykkel.html google.maps.event namespace https://developers.google.com/maps/documentation/javascript/reference?hl=en
  17. http://urbye.net/sykkel.html Events https://developers.google.com/maps/documentation/javascript/events
  18. http://urbye.net/sykkel.html
  19. http://urbye.net/heatmap.html &lt;script type=&quot;text/javascript&quot; src=&quot;https://maps.googleapis.com/maps/api/js?sensor=false&amp;libraries=visualization&quot;&gt;&lt;/script&gt;
  20. http://urbye.net/