-
Notifications
You must be signed in to change notification settings - Fork 292
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ES6: Migrated GeoLocation #951
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* Wire | ||
* Copyright (C) 2016 Wire Swiss GmbH | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see http://www.gnu.org/licenses/. | ||
* | ||
*/ | ||
|
||
'use strict'; | ||
|
||
window.z = window.z || {}; | ||
|
||
z.location = (() => { | ||
const GOOGLE_GEOCODING_BASE_URL = 'https://maps.googleapis.com/maps/api/geocode/json'; | ||
const API_KEY = 'AIzaSyCKxxKw5JBZ5zEFtoirtgnw8omvH7gWzfo'; | ||
let _parse_results = results => { | ||
let res = {}; | ||
let result = results[0]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let [result] = results; |
||
res['address'] = result.formatted_address; | ||
res['lat'] = result.geometry.location.lat; | ||
res['lng'] = result.geometry.location.lng; | ||
let ref = result.address_components; | ||
for (let i = 0, len = ref.length; i < len; i++) { | ||
let component = ref[i]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for (let component of result.address_components) { |
||
let name = component.long_name || component.short_name; | ||
for (let j = 0, len1 = component.types.length; j < len1; j++) { | ||
let type = component.types[j]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for (let type of component.types) { |
||
res[type] = name; | ||
if (type === 'country') { | ||
res['country_code'] = component.short_name || ''; | ||
} | ||
} | ||
} | ||
res['place'] = res.locality || res.natural_feature || res.administrative_area_level_3 || res.administrative_area_level_2 || res.administrative_area_level_1; | ||
delete (res.political != null); | ||
}; | ||
|
||
/* | ||
Reverse loop up for geo location | ||
@param latitude [Number] latitude | ||
@param longitude [Number] longitude | ||
*/ | ||
let get_location = (latitude, longitude) => new Promise((resolve, reject) => { | ||
if ((latitude == null) || (longitude == null)) { | ||
reject(new Error('You need to specify latitude and longitude in order to retrieve the location')); | ||
} | ||
return $.ajax({ | ||
url: `${GOOGLE_GEOCODING_BASE_URL}?latlng=${latitude},${longitude}&key=${API_KEY}`, | ||
}).done(response => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. })
.done((response) => { |
||
if (response.status === 'OK') { | ||
return resolve(_parse_results(response.results)); | ||
} | ||
return resolve(); | ||
}).fail((jqXHR, textStatus, errorThrown) => reject(new Error(errorThrown))); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. })
.fail(... |
||
}); | ||
|
||
/* | ||
Return link to google maps | ||
|
||
@param lat [Number] latitude | ||
@param lng [Number] longitude | ||
@param name [String] location name | ||
@param zoom [String] map zoom level | ||
*/ | ||
let get_maps_url = (lat, lng, name, zoom) => { | ||
let base_url; | ||
base_url = 'https://google.com/maps/'; | ||
if (name != null) { | ||
base_url += `place/${name}/`; | ||
} | ||
base_url += `@${lat},${lng}`; | ||
if (zoom != null) { | ||
base_url += `,${zoom}z`; | ||
} | ||
return base_url; | ||
}; | ||
return { | ||
get_location, | ||
get_maps_url, | ||
}; | ||
})(); |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Wire | ||
* Copyright (C) 2016 Wire Swiss GmbH | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see http://www.gnu.org/licenses/. | ||
* | ||
*/ | ||
|
||
// grunt test_init && grunt test_run:location/GeoLocation | ||
|
||
'use strict'; | ||
|
||
describe('z.location', () => describe('get_maps_url', () => { | ||
it('should return proper url when lat and long is given', () => { | ||
expect(z.location.get_maps_url(52, 13)).toBe('https://google.com/maps/@52,13') | ||
}); | ||
|
||
it('should return proper url when lat, long and zoom is given', () => { | ||
expect(z.location.get_maps_url(52, 13, null, 14)).toBe('https://google.com/maps/@52,13,14z') | ||
}); | ||
|
||
it('should return proper url when lat, long and name is given', () => { | ||
expect(z.location.get_maps_url(52, 13, 'Berlin')).toBe('https://google.com/maps/place/Berlin/@52,13') | ||
}); | ||
|
||
it('should return proper url when lat, long, name and zoom is given', () => { | ||
expect(z.location.get_maps_url(52, 13, 'Berlin', 14)).toBe('https://google.com/maps/place/Berlin/@52,13,14z') | ||
}); | ||
})); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.