Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
JoseVte committed Nov 21, 2019
0 parents commit 1cba38f
Show file tree
Hide file tree
Showing 17 changed files with 504 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/.idea
/vendor
/node_modules
package-lock.json
composer.phar
composer.lock
phpunit.xml
.phpunit.result.cache
.DS_Store
Thumbs.db
yarn.lock
9 changes: 9 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
MIT License

Copyright (c) 2019 Jose Vicente Orts

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
74 changes: 74 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
## Nova Map Address Field

A Nova field to place a marker on map to get coordinates then it reverse geocoding the coordinates to get a street address

## Installation

You can install the package in to a Laravel app that uses Nova via composer:

```bash
composer require josrom/nova-map-address
```

## Configuration
Publish the package config file:
```bash
php artisan vendor:publish --provider="Josrom\MapAddress\FieldServiceProvider"
```

This is the contents of the file which will be published at [config/map-address.php](config/map-address.php).

Add the following keys to your `.env` and `.env.example`:

```
MAP_ADDRESS_API_KEY=
Optional: Set map and address language
MAP_ADDRESS_LANGUAGE=es
```

_If you need a Google Maps API key, you can create an app and enable Places API and create credentials to get your API key https://console.developers.google.com._

## Usage:
Add the below to Nova/{Model}.php resource:

```php

use Josrom\MapAddress\MapAddress;

[
MapAddress::make('address'),

// You can set the initial map location. By default (Spain)
MapAddress::make('address')
->initLocation(38.261842, -0.6868031),

// You can set the location from the model
MapAddress::make('address')
->setLocation($this->latitude, $this->longitude),

// You can select the name of lat/lng fields. By default is lat/lng
MapAddress::make('address')
->setLatitudeField('latitude')
->setLongitudeField('longitude'),

// You can select what is the first result set in address field
MapAddress::make('address')
->setGoogleResultType('street_address'),

// You can also set the map zoom level. By default (4)
MapAddress::make('address')
->initLocation(38.261842, -0.6868031)
->zoom(12),
]
```

![Package screenshot](/doc/map.png)

## Support:

* Jose Vicente Orts: [email protected]

## License

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
34 changes: 34 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "josrom/nova-map-address",
"description": "A Laravel Nova field to place a marker on map to get coordinates then it reverse geocoding the coordinates to get a street address",
"keywords": [
"laravel",
"nova",
"geocoding",
"address",
"geo",
"map"
],
"license": "MIT",
"require": {
"php": ">=7.1.0",
"illuminate/support": "^5.7|^6.0"
},
"autoload": {
"psr-4": {
"Josrom\\MapAddress\\": "src/"
}
},
"extra": {
"laravel": {
"providers": [
"Josrom\\MapAddress\\FieldServiceProvider"
]
}
},
"config": {
"sort-packages": true
},
"minimum-stability": "dev",
"prefer-stable": true
}
13 changes: 13 additions & 0 deletions config/map-address.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

return [
/**
* API Key of Google maps
*/
'api_key' => env('MAP_ADDRESS_API_KEY'),

/**
* Language of google maps
*/
'language' => env('MAP_ADDRESS_LANGUAGE'),
];
Empty file added dist/css/field.css
Empty file.
1 change: 1 addition & 0 deletions dist/js/field.js

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions mix-manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"/dist/js/field.js": "/dist/js/field.js",
"/dist/css/field.css": "/dist/css/field.css"
}
20 changes: 20 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"private": true,
"scripts": {
"dev": "npm run development",
"development": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
"watch": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --watch --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
"watch-poll": "npm run watch -- --watch-poll",
"hot": "cross-env NODE_ENV=development node_modules/webpack-dev-server/bin/webpack-dev-server.js --inline --hot --config=node_modules/laravel-mix/setup/webpack.config.js",
"prod": "npm run production",
"production": "cross-env NODE_ENV=production node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js"
},
"devDependencies": {
"cross-env": "^5.0.0",
"laravel-mix": "^1.0",
"laravel-nova": "^1.0"
},
"dependencies": {
"vue": "^2.5.0"
}
}
9 changes: 9 additions & 0 deletions resources/js/components/DetailField.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<template>
<panel-item :field="field" />
</template>

<script>
export default {
props: ['resource', 'resourceName', 'resourceId', 'field'],
}
</script>
150 changes: 150 additions & 0 deletions resources/js/components/FormField.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
<template>
<default-field :field="field">
<template slot="field">
<div class="google-map w-full" :id="mapName"></div><br>
<input :id="field.name" type="text"
class="w-full form-control form-input form-input-bordered"
:class="errorClasses"
:placeholder="field.name"
v-model="value"
/>
<div class="flex flex-wrap w-full">
<div class="flex w-1/2">
<div class="w-1/5 py-3 pl-2">
<label class="inline-block text-80 pt-2 leading-tight" :for="field.latitude">Lat</label>
</div>
<div class="py-3 w-4/5">
<input :id="field.latitude" type="text"
class="w-full form-control form-input form-input-bordered"
:class="errorClasses"
:placeholder="field.latitude"
v-model="field.lat"
/>
</div>
</div>
<div class="flex w-1/2">
<div class="w-1/5 py-3 pl-2">
<label class="inline-block text-80 pt-2 leading-tight" :for="field.longitude">Lng</label>
</div>
<div class="py-3 w-4/5">
<input :id="field.longitude" type="text"
class="w-full form-control form-input form-input-bordered"
:class="errorClasses"
:placeholder="field.longitude"
v-model="field.lng"
/>
</div>
</div>
</div>
<p v-if="hasError" class="my-2 text-danger">
{{ firstError }}
</p>

</template>
</default-field>
</template>
<style scoped>
.google-map {
width: 720px;
height: 300px;
margin: 0 auto;
background: gray;
border:solid 1px #ccc;
}
</style>
<script>
import { FormField, HandlesValidationErrors } from 'laravel-nova'
export default {
name: 'google-map',
mixins: [FormField, HandlesValidationErrors],
props: ['resourceName', 'resourceId', 'field'],
data: function () {
return {
mapName: this.name + "-map",
}
},
mounted: function () {
let map;
const element = document.getElementById(this.mapName);
let lat = this.field.initial_lat || 38.261842;
let lng = this.field.initial_lng || -0.6868031;
if (this.field.lat && this.field.lng){
lat = this.field.lat;
lng = this.field.lng;
}
if (this.value){
if (previousMarker) {
previousMarker.setMap(null);
}
}
const options = {
zoom: this.field.zoom || 4,
center: new google.maps.LatLng(lat, lng)
};
map = new google.maps.Map(element, options);
let previousMarker;
let address = this;
previousMarker = new google.maps.Marker({
position: new google.maps.LatLng(lat, lng),
map: map
});
google.maps.event.addListener(map, 'click', function(event) {
if (previousMarker) {
previousMarker.setMap(null);
}
previousMarker = new google.maps.Marker({
position: event.latLng,
map: map
});
let geocoder = new google.maps.Geocoder;
geocoder.geocode({'location': event.latLng}, function (results, status) {
if (status === google.maps.GeocoderStatus.OK) {
if (results[0]) {
let result = results[0];
if (address.field.hasOwnProperty('google_result_type')) {
for (let i = 0; i < results.length; i++) {
if (results[i].types.length > 0 && results[i].types.includes(address.field.google_result_type)) {
result = results[i];
break;
}
}
}
address.value = result.formatted_address;
address.field.lat = event.latLng.lat().toFixed(6);
address.field.lng = event.latLng.lng().toFixed(6);
} else {
window.alert('No results found');
}
} else {
window.alert('Geocoder failed due to: ' + status);
}
});
});
},
methods: {
/*
* Set the initial, internal value for the field.
*/
setInitialValue() {
this.value = this.field.value || ''
},
/**
* Fill the given FormData object with the field's internal value.
*/
fill(formData) {
formData.append(this.field.attribute, this.value || '');
formData.append(this.field.latitude, this.field.lat || '');
formData.append(this.field.longitude, this.field.lng || '');
},
/**
* Update the field's internal value.
*/
handleChange(value) {
this.value = value
}
},
}
</script>
9 changes: 9 additions & 0 deletions resources/js/components/IndexField.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<template>
<span>{{ field.value }}</span>
</template>

<script>
export default {
props: ['resourceName', 'field'],
}
</script>
5 changes: 5 additions & 0 deletions resources/js/field.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Nova.booting((Vue, router) => {
Vue.component('index-map_address', require('./components/IndexField'));
Vue.component('detail-map_address', require('./components/DetailField'));
Vue.component('form-map_address', require('./components/FormField'));
})
1 change: 1 addition & 0 deletions resources/sass/field.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// Nova Tool CSS
Loading

0 comments on commit 1cba38f

Please sign in to comment.