JavaScript: Array, Loop, Data File

Slides:



Advertisements
Similar presentations
JavaScript Part 6. Calling JavaScript functions on an event JavaScript doesn’t have a main function like other programming languages but we can imitate.
Advertisements

Google Maps API. Today’s Objectives Get Google Maps working: desktop + mobile Get clustering data complete, data onto a map.
CS 299 – Web Programming and Design Overview of JavaScript and DOM Instructor: Dr. Fang (Daisy) Tang.
CST JavaScript Validating Form Data with JavaScript.
Chapter 5 Java Script And Forms JavaScript, Third Edition.
JavaScript Defined DOM (Document Object Model) General Syntax Body vs. Head Variables Math & Logic Selection Functions & Events Loops Animation Getting.
Programming games Examples: Audio example. Google Maps api. Classwork: show more complex video. Classwork/Homework: Find place (lat and long) for basic.
Google Maps API. Static Maps send an HTTP GET request receive an image (PNG, GIF, JPEG) no javascript needed encode params in URL example:
Bridges To Computing General Information: This document was created for use in the "Bridges to Computing" project of Brooklyn College. You are invited.
Programming Games Google Map API examples. CSS. Classwork/homework: Catch up. Upload work. Show your [more] complex Google Maps API example. Plan your.
TUTORIAL 10: PROGRAMMING WITH JAVASCRIPT Session 2: What is JavaScript?
JavaScript. JavaScript is the programming language of HTML and the Web. Easy to learn One of the 3 languages of all developers MUST learn: 1. HTML to.
Static Locations, Dynamic Content.
CSD 340 (Blum)1 Starting JavaScript Homage to the Homage to the Square.
JavaScript Defined DOM (Document Object Model) General Syntax Body vs. Head Variables Math & Logic Selection Functions & Events Loops Animation Getting.
Creating Web Documents: JavaScript Ftp / file management: review Introduction to JavaScript Sources Homework: start review for midterm, work on Project.
Introduction to JavaScript CSc 2320 Fall 2014 Disclaimer: All words, pictures are adopted from “Simple JavaScript”by Kevin Yank and Cameron Adams and also.
Introduction to JavaScript MIS 3502, Spring 2016 Jeremy Shafer Department of MIS Fox School of Business Temple University 2/2/2016.
Phonegap Bridge –Geolocation and Google maps CIS 136 Building Mobile Apps 1.
HTML LAYOUTS. CONTENTS Layouts Example Layout Using Element Example Using Table Example Output Summary Exercise.
1 Using the Google Maps JavaScript API. 2 The Google Maps APIs Permit web applications to use functionality of google maps. Documentation:
Point Maps Peterson’s Chapter 11 & 12. Points and Point Maps Points – Datum and coordinate systems – geocoding Point Maps – Show where points are (just.
Creating Databases Example using Google Maps API + file creation. Dollar sign problem. Classwork/Homework: [catch up]. Work on final project.
1 Bus Tracker A Google Maps Application. 22 Objectives You will be able to Add an icon to a map created with the Google Maps API. Use Ajax techniques.
HTML for JavaScript Web Applications
JavaScript, Sixth Edition
Chapter 5 Validating Form Data with JavaScript
Build in Objects In JavaScript, almost "everything" is an object.
ISC440: Web Programming 2 Web APIs Google Maps API
>> Introduction to JavaScript
Getting Started with CSS
Programming Web Pages with JavaScript
Web Systems & Technologies
Google APIs and Facebook API
Web Services application that operates over a network
>> Introduction to CSS
Intro to JavaScript CS 1150 Spring 2017.
Project 9 Creating Pop-up Windows, Adding Scrolling Messages, and Validating Forms.
Barb Ericson Georgia Institute of Technology May 2006
JavaScript Syntax and Semantics
Geolocation using Google maps
JavaScript.
Website Design 3
Introduction to JavaScript
Google Maps: A Short How-to
JavaScript an introduction.
A second look at JavaScript
Your 1st Programming Assignment
Geolocation using Google maps
Introduction to Programming the WWW I
T. Jumana Abu Shmais – AOU - Riyadh
Working with Text and Cascading Style Sheets
JavaScript Programming Labs
Web Programming Language
The Internet 11/22/11 Conditionals and Loops
For this assignment, copy and past the XHTML to a notepad file with the .html extension. Then add the code I ask for to complete the problems.
Javascript and JQuery SRM DSC.
Loops and Arrays in JavaScript
Introduction to JavaScript
HTML5 and Local Storage.
Tutorial 10: Programming with javascript
Introduction to Programming and JavaScript
Catchup. Work on project.
Javascript data structures
CIS 136 Building Mobile Apps
Introduction to JavaScript
Web Programming and Design
Geolocation using Google maps
Web Programming and Design
Web Programming and Design
Presentation transcript:

JavaScript: Array, Loop, Data File

<!DOCTYPE html> <html>   <head>     <style>        #map {         height: 400px;         width: 100%;        }     </style>   </head>   <body>     <h3>My Google Maps Demo</h3>     <div id="map"></div>     <script>       function initMap() {         var uluru = {lat: -25.363, lng: 131.044};         var map = new google.maps.Map(document.getElementById('map'), {           zoom: 4,           center: uluru         });         var marker = new google.maps.Marker({           position: uluru,           map: map         });       }     </script>     <script async defer     src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap">     </script>   </body> </html>

function initMap() { var uluru = {lat: -25. 363, lng: 131 function initMap() { var uluru = {lat: -25.363, lng: 131.044}; var map = new google.maps.Map(document.getElementById('map'), { zoom: 4, center: uluru }); var marker = new google.maps.Marker({ position: point1, map: map }); var marker = new google.maps.Marker({ position: point2, map: map }); . . }

A Better Idea: Array + Loop

Array loop from the 1st item to the last item { create a marker; } A data structure to hold a set of data with the same attributes Benefit of Using Array Repetition becomes easier. loop from the 1st item to the last item { create a marker; }

Array in JavaScript Simple array: elements are a simple data type. var aListofNumber = [1,2,3,4]; var aListofString = ["a", "b", "c","d"];

Array Reference Use an indexing number ArrayName[indexNumber] var aListofNumber = [1,2,3,4]; var aListofString = ["a", "b", "c","d"]; aListofNumber[0] aListofString[3]

Array in JavaScript Object array: elements are objects, which have multiple data attributes (same or different types). var aListofPeople = [{name: "John", age: 28}, {name: "Jane", age: 24}, {name: "Joe", age: 20}]; Objects are grouped by {}. aListofPeople[1].name aListofPeople[2].age

In the Case of Your Assignment crimes = [ {date: "9/1/2016", lat: 40.81, lng:-77.86, location: "Wagner Bld"}, {date: "9/1/2016",lat: 40.79, lng: -77.86, location: "Reber Bld“}, . ];

Array Properties Most useful one: length arrayName.length var aListofNumber = [1,2,3,4]; aListofNumber.length = 4;

Array Methods Some important methods push(): adding a new element pop(): removing the last element indexOf(): finding the position of a given element forEach(): calling a function for each element

Processing Data in An Array Loop The For loop for (statement 1; statement 2; statement 3) {     code block to be executed } for (var i=0; i<crimes.length; i++) { var marker = new google.maps.Marker({           position: ???,           map: map         }); } http://www.w3schools.com/js/js_loop_for.asp

Peer Help Assisting peers and learning from peers are very helpful. If you finish your exercise, feel free to help others. Put your name on the Peer-Helper sheet after the class. Used to boot your class participation grade.

Exercise 1 Convert the entries in the text file into an array of objects for all crimes records. Each record should have at least the following attributes Date, Offenses, Location, lat, lng Hint: Adding necessary attribute names and characters in Excel. Copying the data into JavaScript. To check your data in JavaScript for (var i=0; i<crimes.length; i++) { console.log(crimes[i]); });

Exercise 2 Based on the codes for Exercise 1, display all markers.

Exercise 3 (Optional) Use the forEach() method to process the marker data An example can be found here: http://www.w3schools.com/jsref/jsref_forEach.asp Hint crimes.forEach(function(crime) { … })

Data File Converting the raw data into array is cumbersome. Can we read the file directly? Yes! There are many ways to do that. D3.js offers a simpler approach. To read a csv file or a JSON file d3.csv ("filename", function(data) {}); CSV: all data is string d3.json ("filename", function(data) {}); JSON: smarter, different data types. E.g., latitude and longitude data The data file should be on a server (could be the same server with the js file).

d3. csv("crimes. txt", function(data) { console d3.csv("crimes.txt", function(data) { console.log(data); }); For JSON file, you can convert the csv file to JSON with some online tools, such as http://www.csvjson.com/csv2json d3.json("crimes.json", function(data) { Pay attention to the difference in latitude and longitude data. Need this line before calling d3.csv or d3.json : <script src="//d3js.org/d3.v3.min.js"> </script> http://zhang.ist.psu.edu/teaching/402/sampleCodes/jsCSVExample.html

Exercise 4 (Optional) Read the data file and do the marker presentation.

Bonus Points Use an infowindow for each click on a marker. Listen to the click event on a marker Call a function to show an inforwindow with appropriate information.

Update of Grading Rubric Comments for JavaScript codes 1 point var uluru = {lat: 40.796466,lng: -77.8649379}; var map = new google.maps.Map(document.getElementById('map'), { zoom: 12, center: uluru }); //the center of the map //a new map with a given center and zooming level