增加地图自定义图标并显示客列表
This commit is contained in:
@@ -20,6 +20,6 @@ class CustomerLocationAdmin(admin.ModelAdmin):
|
||||
'fields': ('address_zh', 'address_en', 'description_zh', 'description_en')
|
||||
}),
|
||||
('显示', {
|
||||
'fields': ('marker_color', 'sort_order', 'is_active')
|
||||
'fields': ('marker_color', 'marker_icon', 'sort_order', 'is_active')
|
||||
}),
|
||||
)
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
# Generated by Django 5.0.14 on 2026-08-03 08:00
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('customers', '0001_initial'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='customerlocation',
|
||||
name='marker_icon',
|
||||
field=models.ImageField(blank=True, help_text='建议尺寸 32x32 或 48x48 像素的 PNG 图片,留空则使用默认蓝色圆点', null=True, upload_to='customers/markers/', verbose_name='自定义标记图标'),
|
||||
),
|
||||
]
|
||||
@@ -11,6 +11,8 @@ class CustomerLocation(models.Model):
|
||||
description_zh = models.TextField('描述说明(中文)', blank=True, default='')
|
||||
description_en = models.TextField('描述说明(英文)', blank=True, default='')
|
||||
marker_color = models.CharField('标记点颜色', max_length=20, default='#165DFF', help_text='十六进制颜色值')
|
||||
marker_icon = models.ImageField('自定义标记图标', upload_to='customers/markers/', blank=True, null=True,
|
||||
help_text='建议尺寸 32x32 或 48x48 像素的 PNG 图片,留空则使用默认蓝色圆点')
|
||||
sort_order = models.IntegerField('排序', default=0)
|
||||
is_active = models.BooleanField('启用', default=True)
|
||||
created_at = models.DateTimeField('创建时间', auto_now_add=True)
|
||||
|
||||
+15
-12
@@ -2,6 +2,7 @@ from django.shortcuts import render
|
||||
from django.http import JsonResponse
|
||||
from .models import CustomerLocation
|
||||
from django.utils.translation import get_language
|
||||
from django.conf import settings
|
||||
|
||||
|
||||
def customer_map(request):
|
||||
@@ -12,19 +13,21 @@ def customer_map(request):
|
||||
def customer_locations_api(request):
|
||||
lang = get_language()
|
||||
suffix = 'en' if lang.startswith('en') else 'zh'
|
||||
locations = CustomerLocation.objects.filter(is_active=True).values(
|
||||
'id', f'name_{suffix}', f'address_{suffix}', f'description_{suffix}',
|
||||
'latitude', 'longitude', 'marker_color'
|
||||
)
|
||||
locations = CustomerLocation.objects.filter(is_active=True).order_by('sort_order', '-id')
|
||||
data = []
|
||||
for loc in locations:
|
||||
for index, loc in enumerate(locations, start=1):
|
||||
icon_url = None
|
||||
if loc.marker_icon:
|
||||
icon_url = settings.MEDIA_URL + str(loc.marker_icon)
|
||||
data.append({
|
||||
'id': loc['id'],
|
||||
'name': loc[f'name_{suffix}'],
|
||||
'address': loc[f'address_{suffix}'],
|
||||
'description': loc[f'description_{suffix}'],
|
||||
'lat': loc['latitude'],
|
||||
'lng': loc['longitude'],
|
||||
'color': loc['marker_color'] or '#165DFF',
|
||||
'id': loc.id,
|
||||
'index': index,
|
||||
'name': getattr(loc, f'name_{suffix}'),
|
||||
'address': getattr(loc, f'address_{suffix}'),
|
||||
'description': getattr(loc, f'description_{suffix}'),
|
||||
'lat': loc.latitude,
|
||||
'lng': loc.longitude,
|
||||
'color': loc.marker_color or '#165DFF',
|
||||
'icon': icon_url,
|
||||
})
|
||||
return JsonResponse({'locations': data})
|
||||
|
||||
+178
-17
@@ -6,10 +6,118 @@
|
||||
{% block extra_css %}
|
||||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/leaflet@1.9.4/dist/leaflet.css">
|
||||
<style>
|
||||
.map-wrapper {
|
||||
display: flex;
|
||||
gap: 20px;
|
||||
min-height: 600px;
|
||||
}
|
||||
.map-container {
|
||||
flex: 1;
|
||||
border-radius: 12px;
|
||||
overflow: hidden;
|
||||
box-shadow: 0 2px 12px rgba(0,0,0,0.08);
|
||||
}
|
||||
#customer-map {
|
||||
width: 100%;
|
||||
height: 600px;
|
||||
}
|
||||
.customer-list-panel {
|
||||
width: 320px;
|
||||
flex-shrink: 0;
|
||||
background: #fff;
|
||||
border-radius: 12px;
|
||||
box-shadow: 0 2px 12px rgba(0,0,0,0.08);
|
||||
overflow: hidden;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
}
|
||||
.customer-list-header {
|
||||
padding: 16px 20px;
|
||||
border-bottom: 1px solid #eef0f4;
|
||||
background: #fafbfc;
|
||||
}
|
||||
.customer-list-header h5 {
|
||||
margin: 0;
|
||||
color: var(--title-color);
|
||||
font-size: 1rem;
|
||||
}
|
||||
.customer-list-header .count {
|
||||
color: var(--text-secondary);
|
||||
font-size: 0.8rem;
|
||||
margin-left: 8px;
|
||||
}
|
||||
.customer-list {
|
||||
flex: 1;
|
||||
overflow-y: auto;
|
||||
list-style: none;
|
||||
padding: 0;
|
||||
margin: 0;
|
||||
}
|
||||
.customer-list li {
|
||||
padding: 14px 20px;
|
||||
border-bottom: 1px solid #f3f5f8;
|
||||
cursor: pointer;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 12px;
|
||||
transition: background 0.2s;
|
||||
}
|
||||
.customer-list li:hover {
|
||||
background: #f7f9fc;
|
||||
}
|
||||
.customer-list li.active {
|
||||
background: #eef4ff;
|
||||
}
|
||||
.customer-index {
|
||||
width: 28px;
|
||||
height: 28px;
|
||||
border-radius: 50%;
|
||||
background: var(--primary-color);
|
||||
color: #fff;
|
||||
font-size: 0.8rem;
|
||||
font-weight: 600;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
.customer-name {
|
||||
flex: 1;
|
||||
color: var(--title-color);
|
||||
font-size: 0.9rem;
|
||||
line-height: 1.4;
|
||||
}
|
||||
.leaflet-popup-content-wrapper { border-radius: 8px; }
|
||||
.leaflet-popup-content { margin: 12px 16px; }
|
||||
.leaflet-popup-content h6 { margin-bottom: 4px; color: var(--title-color); }
|
||||
.leaflet-popup-content p { margin-bottom: 0; color: var(--text-secondary); font-size: 0.875rem; }
|
||||
.numbered-marker {
|
||||
background: var(--primary-color);
|
||||
color: #fff;
|
||||
border-radius: 50%;
|
||||
width: 32px;
|
||||
height: 32px;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
font-weight: 600;
|
||||
font-size: 0.8rem;
|
||||
border: 2px solid #fff;
|
||||
box-shadow: 0 2px 6px rgba(0,0,0,0.3);
|
||||
}
|
||||
|
||||
@media (max-width: 992px) {
|
||||
.map-wrapper {
|
||||
flex-direction: column;
|
||||
}
|
||||
.customer-list-panel {
|
||||
width: 100%;
|
||||
max-height: 300px;
|
||||
}
|
||||
#customer-map {
|
||||
height: 400px;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
{% endblock %}
|
||||
|
||||
@@ -20,7 +128,17 @@
|
||||
<h2>{% trans '客户分布' %}</h2>
|
||||
<p>{% trans '我们的客户遍布全国各地' %}</p>
|
||||
</div>
|
||||
<div id="customer-map"></div>
|
||||
<div class="map-wrapper">
|
||||
<div class="map-container">
|
||||
<div id="customer-map"></div>
|
||||
</div>
|
||||
<div class="customer-list-panel">
|
||||
<div class="customer-list-header">
|
||||
<h5>{% trans '客户列表' %}<span class="count" id="customer-count"></span></h5>
|
||||
</div>
|
||||
<ul class="customer-list" id="customer-list"></ul>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
{% endblock %}
|
||||
@@ -30,33 +148,76 @@
|
||||
<script>
|
||||
document.addEventListener('DOMContentLoaded', function() {
|
||||
const map = L.map('customer-map').setView([35.8617, 104.1954], 4);
|
||||
const markers = {};
|
||||
|
||||
L.tileLayer('https://webrd0{s}.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=8&x={x}&y={y}&z={z}', {
|
||||
subdomains: ['1', '2', '3', '4'],
|
||||
attribution: '© 高德地图'
|
||||
}).addTo(map);
|
||||
|
||||
function createNumberedIcon(index, color) {
|
||||
return L.divIcon({
|
||||
className: '',
|
||||
html: '<div class="numbered-marker" style="background:' + (color || '#165DFF') + ';">' + index + '</div>',
|
||||
iconSize: [32, 32],
|
||||
iconAnchor: [16, 16],
|
||||
popupAnchor: [0, -16]
|
||||
});
|
||||
}
|
||||
|
||||
fetch("{% url 'customer_locations_api' %}")
|
||||
.then(r => r.json())
|
||||
.then(data => {
|
||||
if (data.locations && data.locations.length) {
|
||||
data.locations.forEach(loc => {
|
||||
const marker = L.circleMarker([loc.lat, loc.lng], {
|
||||
radius: 8,
|
||||
fillColor: loc.color || '#165DFF',
|
||||
color: '#fff',
|
||||
weight: 2,
|
||||
opacity: 1,
|
||||
fillOpacity: 0.8
|
||||
}).addTo(map);
|
||||
if (loc.name || loc.address || loc.description) {
|
||||
let popup = '';
|
||||
if (loc.name) popup += '<h6>' + loc.name + '</h6>';
|
||||
if (loc.address) popup += '<p><i class="fa fa-map-marker"></i> ' + loc.address + '</p>';
|
||||
if (loc.description) popup += '<p>' + loc.description + '</p>';
|
||||
if (popup) marker.bindPopup(popup);
|
||||
const listEl = document.getElementById('customer-list');
|
||||
const countEl = document.getElementById('customer-count');
|
||||
const locations = data.locations || [];
|
||||
|
||||
countEl.textContent = '(' + locations.length + ')';
|
||||
|
||||
locations.forEach(loc => {
|
||||
let marker;
|
||||
if (loc.icon) {
|
||||
const customIcon = L.icon({
|
||||
iconUrl: loc.icon,
|
||||
iconSize: [32, 32],
|
||||
iconAnchor: [16, 16],
|
||||
popupAnchor: [0, -16]
|
||||
});
|
||||
marker = L.marker([loc.lat, loc.lng], { icon: customIcon });
|
||||
} else {
|
||||
marker = L.marker([loc.lat, loc.lng], {
|
||||
icon: createNumberedIcon(loc.index, loc.color)
|
||||
});
|
||||
}
|
||||
marker.addTo(map);
|
||||
markers[loc.id] = marker;
|
||||
|
||||
if (loc.name || loc.address || loc.description) {
|
||||
let popup = '';
|
||||
if (loc.name) popup += '<h6>' + loc.name + '</h6>';
|
||||
if (loc.address) popup += '<p><i class="fa fa-map-marker"></i> ' + loc.address + '</p>';
|
||||
if (loc.description) popup += '<p>' + loc.description + '</p>';
|
||||
if (popup) marker.bindPopup(popup);
|
||||
}
|
||||
|
||||
const li = document.createElement('li');
|
||||
li.dataset.id = loc.id;
|
||||
li.innerHTML = '<span class="customer-index" style="background:' + (loc.color || '#165DFF') + ';">' + loc.index + '</span>' +
|
||||
'<span class="customer-name">' + (loc.name || '-') + '</span>';
|
||||
li.addEventListener('click', function() {
|
||||
document.querySelectorAll('#customer-list li').forEach(el => el.classList.remove('active'));
|
||||
li.classList.add('active');
|
||||
map.flyTo([loc.lat, loc.lng], 8, { duration: 0.8 });
|
||||
if (markers[loc.id]) {
|
||||
markers[loc.id].openPopup();
|
||||
}
|
||||
});
|
||||
listEl.appendChild(li);
|
||||
});
|
||||
|
||||
if (locations.length) {
|
||||
const bounds = L.latLngBounds(locations.map(loc => [loc.lat, loc.lng]));
|
||||
map.fitBounds(bounds, { padding: [60, 60] });
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user