Skip to content

Instantly share code, notes, and snippets.

Created February 9, 2017 16:59
Show Gist options
  • Save anonymous/c811c7741221365e235c5b2b85857f5a to your computer and use it in GitHub Desktop.
Save anonymous/c811c7741221365e235c5b2b85857f5a to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/muwasetisa
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<script src="https://code.jquery.com/jquery-3.1.0.js"></script>
</head>
<body>
<script id="jsbin-javascript">
function geoFindMe() {
if (!navigator.geolocation){
console.log('Geolocation is not supported by your browser');
return;
}
function success(position) {
console.log(position.coords.latitude, position.coords.longitude);
getCityFromGeocord([position.coords.latitude, position.coords.longitude]);
// return [position.coords.latitude, position.coords.longitude];
}
function error() {
console.log(false);
// return [];
}
console.log('loading...');
navigator.geolocation.getCurrentPosition(success, error);
}
function isCity(obj) {
return obj.types[0] == 'administrative_area_level_1' || obj.types[0] == 'administrative_area_level_2';
}
function getCityFromGeocord(geocord) {
var str = geocord.join(',');
$.ajax({
method: "GET",
url: "https://maps.googleapis.com/maps/api/geocode/json?latlng=" + str
})
.done(function(res) {
if(res.status == 'OK') {
var ppp = res.results[0].address_components;
var qqq = ppp.filter(isCity);
console.log(qqq[0].short_name.substr(0, 2));
}else {
console.log('no match!');
}
}).fail(function() {
console.log('ajax fail!');
});
}
geoFindMe();
</script>
<script id="jsbin-source-javascript" type="text/javascript">function geoFindMe() {
if (!navigator.geolocation){
console.log('Geolocation is not supported by your browser');
return;
}
function success(position) {
console.log(position.coords.latitude, position.coords.longitude);
getCityFromGeocord([position.coords.latitude, position.coords.longitude]);
// return [position.coords.latitude, position.coords.longitude];
}
function error() {
console.log(false);
// return [];
}
console.log('loading...');
navigator.geolocation.getCurrentPosition(success, error);
}
function isCity(obj) {
return obj.types[0] == 'administrative_area_level_1' || obj.types[0] == 'administrative_area_level_2';
}
function getCityFromGeocord(geocord) {
var str = geocord.join(',');
$.ajax({
method: "GET",
url: "https://maps.googleapis.com/maps/api/geocode/json?latlng=" + str
})
.done(function(res) {
if(res.status == 'OK') {
var ppp = res.results[0].address_components;
var qqq = ppp.filter(isCity);
console.log(qqq[0].short_name.substr(0, 2));
}else {
console.log('no match!');
}
}).fail(function() {
console.log('ajax fail!');
});
}
geoFindMe();</script></body>
</html>
function geoFindMe() {
if (!navigator.geolocation){
console.log('Geolocation is not supported by your browser');
return;
}
function success(position) {
console.log(position.coords.latitude, position.coords.longitude);
getCityFromGeocord([position.coords.latitude, position.coords.longitude]);
// return [position.coords.latitude, position.coords.longitude];
}
function error() {
console.log(false);
// return [];
}
console.log('loading...');
navigator.geolocation.getCurrentPosition(success, error);
}
function isCity(obj) {
return obj.types[0] == 'administrative_area_level_1' || obj.types[0] == 'administrative_area_level_2';
}
function getCityFromGeocord(geocord) {
var str = geocord.join(',');
$.ajax({
method: "GET",
url: "https://maps.googleapis.com/maps/api/geocode/json?latlng=" + str
})
.done(function(res) {
if(res.status == 'OK') {
var ppp = res.results[0].address_components;
var qqq = ppp.filter(isCity);
console.log(qqq[0].short_name.substr(0, 2));
}else {
console.log('no match!');
}
}).fail(function() {
console.log('ajax fail!');
});
}
geoFindMe();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment