Skip to content

Commit

Permalink
reverse geocode to display address in result
Browse files Browse the repository at this point in the history
  • Loading branch information
jbouzekri committed Jul 1, 2017
1 parent 86b09a6 commit ba88649
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 12 deletions.
3 changes: 2 additions & 1 deletion src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@

<script>
import './assets/css/style.css'
import GmaltGeoloc from './components/GmaltGeoloc'
import GmaltMap from './components/GmaltMap'
import GmaltSearch from './components/GmaltSearch'
Expand All @@ -107,7 +108,7 @@ export default {
return AltService
.get(lat, lng)
.then((json) => {
if (requestedPosition === this.position) {
if (JSON.stringify(requestedPosition) === JSON.stringify(this.position)) {
this.loading = false
this.alt = json.alt
}
Expand Down
32 changes: 31 additions & 1 deletion src/components/GmaltResult.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
</div>
<div class="card-item">
<span class="card-item-title">Address :</span>
<span class="card-item-value">
<span class="card-item-value card-item-value-address">
<strong v-if="address">{{ address }}</strong>
<span v-if="!address">No value</span>
</span>
Expand All @@ -31,9 +31,34 @@
</template>

<script>
import GeocodeService from '../services/GeocodeService'
export default {
name: 'gmalt-result',
props: ['lat', 'lng', 'alt', 'loading'],
watch: {
position () {
if (this.lng && this.lat) {
GeocodeService
.get(this.lat, this.lng)
.then((result) => {
this.setAddress(result[0], result[1])
})
}
}
},
methods: {
setAddress (requestedPosition, address) {
if (JSON.stringify(requestedPosition) === JSON.stringify(this.position)) {
this.address = address
}
}
},
computed: {
position () {
return {lat: this.lat, lng: this.lng}
}
},
data () {
return {
address: ''
Expand All @@ -60,6 +85,11 @@
float: right;
}
.card-item-value-address {
width: calc(100% - 70px);
text-align: right;
}
.card-item--result {
margin-top: 20px;
}
Expand Down
13 changes: 3 additions & 10 deletions src/services/AltService.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
import * as resolveUrl from 'resolve-url'
import HttpService from './HttpService'

const apiUrl = resolveUrl(process.env.API_URL, '/altitude')

export default {
get (lat, lng) {
return fetch(apiUrl + '?lat=' + lat + '&lng=' + lng)
.then(function (response) {
if (response.status >= 200 && response.status < 300) {
return response
} else {
let error = new Error(response.statusText)
error.response = response
throw error
}
})
return HttpService
.get(apiUrl + '?lat=' + lat + '&lng=' + lng)
.then(function (response) {
return response.json()
})
Expand Down
29 changes: 29 additions & 0 deletions src/services/GeocodeService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
export default {
geocoderObject: null,

geocoder () {
if (!this.geocoderObject) {
/* global google */
this.geocoderObject = new google.maps.Geocoder()
}

return this.geocoderObject
},

get (lat, lng) {
const requestedPosition = {lat: lat, lng: lng}
return new Promise((resolve) => {
this.geocoder().geocode({location: requestedPosition}, function (results, status) {
let address = null
if (status === 'OK') {
if (results[0]) {
address = results[0].formatted_address
}
}

resolve([requestedPosition, address])
})
})
}
}

14 changes: 14 additions & 0 deletions src/services/HttpService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export default {
get (url) {
return fetch(url)
.then(function (response) {
if (response.status >= 200 && response.status < 300) {
return response
} else {
let error = new Error(response.statusText)
error.response = response
throw error
}
})
}
}

0 comments on commit ba88649

Please sign in to comment.