Skip to content

Commit 4274b3f

Browse files
authored
Merge pull request #10 from mostafaznv/dev
use ol-style-icon instead of static centered marker image
2 parents ee52be7 + cc60f26 commit 4274b3f

10 files changed

Lines changed: 68 additions & 68 deletions

File tree

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"multi polygon",
1111
"spatial",
1212
"openlayers",
13+
"geocoder",
1314
"nova"
1415
],
1516
"license": "MIT",

dist/css/field.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/js/field.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

resources/js/components/DetailField.vue

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<template #value>
44
<div class="map-detail-field">
55
<template v-if="mapType === 'POINT'">
6-
<point-form-field class="readonly" :field="field" :readonly="true" />
6+
<point-form-field :field="field" :readonly="true" />
77
<point-index-field class="mt-3" :field="field" :modal-mode="true" />
88
</template>
99

@@ -46,11 +46,3 @@ export default {
4646
}
4747
}
4848
</script>
49-
50-
<style lang="scss" scoped>
51-
.map-detail-field {
52-
.readonly {
53-
pointer-events: none;
54-
}
55-
}
56-
</style>

resources/js/components/form-fields/MultiPolygonFormField.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<template>
2-
<div class="map-container" :class="['marker-icon-' + markerIcon, isDirty ? '' : 'is-not-dirty']">
2+
<div class="map-container">
33
<ol-map ref="map" @click="setDirty" :load-tiles-while-animating="true" :load-tiles-while-interacting="true" :style="mapStyles">
44
<ol-view
55
@centerChanged="setDirty"

resources/js/components/form-fields/PointFormField.vue

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,30 @@
11
<template>
2-
<div class="map-container with-marker" :class="['marker-icon-' + markerIcon, isDirty ? '' : 'is-not-dirty']">
3-
<ol-map ref="map" @click="setDirty" :load-tiles-while-animating="true" :load-tiles-while-interacting="true"
4-
:style="mapStyles">
5-
<ol-view
6-
@centerChanged="onCenterChanged"
7-
:center="center"
8-
:rotation="rotation"
9-
:zoom="zoom"
10-
:projection="projection"
11-
/>
2+
<div class="map-container">
3+
<ol-map ref="map" :load-tiles-while-animating="true" :load-tiles-while-interacting="true" :style="mapStyles">
4+
<ol-view :center="center" :rotation="rotation" :zoom="zoom" :projection="projection" />
125

136
<ol-tile-layer>
147
<ol-source-osm />
158
</ol-tile-layer>
169

10+
<ol-vector-layer>
11+
<ol-source-vector :projection="projection">
12+
<ol-feature v-if="hasInitValue">
13+
<ol-geom-point :coordinates="[initValue.longitude, initValue.latitude]" />
14+
</ol-feature>
15+
16+
<template v-if="!isReadonly">
17+
<ol-interaction-modify v-if="isDirty" @modifyend="onModifyEnd" />
18+
<ol-interaction-draw v-else type="Point" @drawend="onDrawEnd" />
19+
</template>
20+
</ol-source-vector>
21+
22+
<ol-style>
23+
<ol-style-icon :src="markerIcon" :anchor="[0.5, 1]" :scale="0.1" />
24+
</ol-style>
25+
</ol-vector-layer>
26+
27+
1728
<ol-zoom-control v-if="withZoomControl" />
1829
<ol-zoomslider-control v-if="withZoomSlider" />
1930
<ol-fullscreen-control v-if="withFullScreenControl" />
@@ -33,6 +44,10 @@ export default {
3344
data() {
3445
return {
3546
isDirty: false,
47+
initValue: {
48+
longitude: null,
49+
latitude: null
50+
},
3651
fieldValue: {
3752
longitude: null,
3853
latitude: null
@@ -42,6 +57,10 @@ export default {
4257
computed: {
4358
isReadonly() {
4459
return this.readonly === true
60+
},
61+
62+
hasInitValue() {
63+
return this.initValue.latitude && this.initValue.longitude
4564
}
4665
},
4766
methods: {
@@ -59,6 +78,10 @@ export default {
5978
])
6079
6180
this.center = lonLat
81+
this.initValue = {
82+
longitude: lonLat[0],
83+
latitude: lonLat[1]
84+
}
6285
6386
this.setValue(lonLat[1], lonLat[0])
6487
}
@@ -70,8 +93,20 @@ export default {
7093
}
7194
},
7295
73-
onCenterChanged(center) {
74-
this.setValue(center[1], center[0])
96+
onDrawEnd(evt) {
97+
const coordinate = evt.feature?.getGeometry().getCoordinates()
98+
99+
if (Array.isArray(coordinate)) {
100+
this.setValue(coordinate[1], coordinate[0])
101+
}
102+
},
103+
104+
onModifyEnd(evt) {
105+
const coordinate = evt.features?.getArray()[0].getGeometry().getCoordinates()
106+
107+
if (Array.isArray(coordinate)) {
108+
this.setValue(coordinate[1], coordinate[0])
109+
}
75110
},
76111
77112
setDirty() {

resources/js/components/form-fields/PolygonFormField.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<template>
2-
<div class="map-container" :class="['marker-icon-' + markerIcon, isDirty ? '' : 'is-not-dirty']">
2+
<div class="map-container">
33
<ol-map ref="map" @click="setDirty" :load-tiles-while-animating="true" :load-tiles-while-interacting="true" :style="mapStyles">
44
<ol-view
55
@centerChanged="setDirty"

resources/scss/field.scss

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,6 @@
44
.map-container {
55
position: relative;
66

7-
&.is-not-dirty {
8-
filter: grayscale(1);
9-
}
10-
11-
&.with-marker {
12-
transition: all 300ms;
13-
14-
&:before {
15-
position: absolute;
16-
content: '';
17-
width: 46px;
18-
height: 46px;
19-
top: calc(50% - 46px);
20-
left: calc(50% - 23px);
21-
background: url('/vendor/nova-map-field/dist/images/ic-pin-1.png') center;
22-
background-size: cover !important;
23-
z-index: 10;
24-
}
25-
26-
&.marker-icon-2 {
27-
&:before {
28-
background-image: url('/vendor/nova-map-field/dist/images/ic-pin-2.png');
29-
}
30-
}
31-
32-
&.marker-icon-3 {
33-
&:before {
34-
background-image: url('/vendor/nova-map-field/dist/images/ic-pin-3.png');
35-
}
36-
}
37-
}
38-
397
.ol-overlaycontainer-stopevent {
408
z-index: 15 !important;
419
}

src/Fields/MapPointField.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ class MapPointField extends Field
1616

1717
private string $mapType = 'POINT';
1818

19+
public function markerIcon(int $icon): self
20+
{
21+
if (in_array($icon, [1, 2, 3])) {
22+
$this->markerIcon = url($this->markerIconPath . "ic-pin-{$icon}.png");
23+
}
24+
25+
return $this;
26+
}
27+
1928
protected function fillAttributeFromRequest(NovaRequest $request, $requestAttribute, $model, $attribute)
2029
{
2130
if ($request->exists($requestAttribute)) {

src/Traits/WithMapProps.php

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ trait WithMapProps
1717

1818
private int $mapHeight;
1919

20-
private int $markerIcon;
20+
/**
21+
* @readonly
22+
*/
23+
private string $markerIconPath = '/vendor/nova-map-field/dist/images/';
24+
private string $markerIcon;
2125

2226
private bool $showSearchBox;
2327
private MapSearchProvider $searchProvider;
@@ -50,7 +54,7 @@ public function __construct($name, $attribute = null, callable $resolveCallback
5054
$this->withZoomSlider = $config['controls']['zoom-slider'];
5155
$this->withFullScreenControl = $config['controls']['full-screen-control'];
5256
$this->mapHeight = $config['map-height'];
53-
$this->markerIcon = $config['icon'];
57+
$this->markerIcon = url($this->markerIconPath . "ic-pin-{$config['icon']}.png");
5458

5559
$this->showSearchBox = $searchConfig['enable'];
5660
$this->searchProvider = $searchConfig['provider'];
@@ -115,15 +119,6 @@ public function mapHeight(int $height): self
115119
return $this;
116120
}
117121

118-
public function markerIcon(int $icon): self
119-
{
120-
if (in_array($icon, [1, 2, 3])) {
121-
$this->markerIcon = $icon;
122-
}
123-
124-
return $this;
125-
}
126-
127122
public function withSearchBox(bool $status = true): self
128123
{
129124
$this->showSearchBox = $status;

0 commit comments

Comments
 (0)