The Meetup.com API is very easy to use and everybody received one API key when registering with the site. I decided to utilize the API by integrating it with Google Gears and Map APIs to come out a mashup for people can easy to find out what meetups happening in his(her) own town.
To automatically find out the locations that users are in, the Google Gears API is used to retrieve Geolocation. Users will need to install Google Gears to use the feature, otherwise, they have to manually keyin the location information.
The very first time you run the mashup it will ask your authorization -
A PHP file is running at back-end to communicate with Meetup API and the main program - mymu.html, I need to modifiy the header of the json output from Meetup in order for Dojo to accept it as a dojox.data.jsonreststore datastore to feed the spreadsheet displayed on top.
- <?php
- $state = urlencode($_GET["state"]);
- $city = urlencode($_GET["city"]);
- if (isset($_GET["page"]))
- $page = $_GET["page"];
- else
- $page = "50";
- if (isset($_GET["topic"]))
- $topic = $_GET["topic"];
- else
- $topic="";
- if (! isset($_GET["type"]))
- $type=0;
- else
- $type=$_GET["type"];
- switch($type) {
- case "0":
- break;
- case "1":
- $curl = curl_init();
- $req = "http://api.meetup.com/events.json/?country=us&state=" . $state . "&city=" . $city . "&key=YourMeetupApiKey";
- if ($topic != "") {
- $req .= "&topic=" . $topic;
- }
- if ($page != "") {
- $req .= "&page=" . $page;
- }
- curl_setopt($curl, CURLOPT_URL, $req );
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
- $result = curl_exec($curl);
- curl_close($curl);
- $result=str_replace("\"results\":", "identifier:\"id\", items:", $result);
- echo $result;
- break;
- }
- ?>
The main JavaScript function - init() will try to find the user geolocation first, then retrieve data from Meetup API and populate the information on Dojo spreadsheet and a Google Map.(init2)
- function init() {
- if (!window.google || !google.gears) {
- alert('Gears is not installed, please get it at http://gears.google.com ');
- return;
- }
- function successCallback(p) {
- if (p.gearsAddress.country == "United States") {
- dijit.byId('city').attr('value', p.gearsAddress.city);
- dijit.byId('state').attr('name', p.gearsAddress.city);
- }
- init2();
- }
- function errorCallback(err) {
- var msg = 'Error retrieving your location: ' + err.message;
- alert(msg);
- }
- try {
- var geolocation = google.gears.factory.create('beta.geolocation');
- geolocation.getCurrentPosition(successCallback,
- errorCallback,
- { enableHighAccuracy: true,
- gearsRequestAddress: true });
- } catch (e) {
- alert('Error using Geolocation API: ' + e.message);
- return;
- }
- }
- function init2() {
- gridView(); //create spreadsheet
- loadMap(dijit.byId('city').value + ", " + dijit.byId('state').value); // load the city map
- drawMap(store); // draw the markers
- }
No comments:
Post a Comment