- getMapCenter
This commit is contained in:
@@ -3,6 +3,7 @@ package cms
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"math"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -35,8 +36,10 @@ type StoreExt struct {
|
||||
}
|
||||
|
||||
type StoresInfo struct {
|
||||
TotalCount int `json:"totalCount"`
|
||||
Stores []*StoreExt `json:"stores"`
|
||||
TotalCount int `json:"totalCount"`
|
||||
MapCenterLng float64 `json:"mapCenterLng"`
|
||||
MapCenterLat float64 `json:"mapCenterLat"`
|
||||
Stores []*StoreExt `json:"stores"`
|
||||
}
|
||||
|
||||
var (
|
||||
@@ -228,10 +231,10 @@ func GetStores(ctx *jxcontext.Context, keyword string, params map[string]interfa
|
||||
mapLatitude, mapLongitude float64
|
||||
mapRadius int
|
||||
)
|
||||
if mapLatitude2, ok := params["mapLatitude"].(string); ok {
|
||||
if mapLongitude2, ok := params["mapLongitude"].(string); ok {
|
||||
mapLimit = true
|
||||
mapLatitude = utils.Str2Float64(mapLatitude2)
|
||||
mapLongitude = utils.Str2Float64(params["mapLongitude"].(string))
|
||||
mapLongitude = utils.Str2Float64(mapLongitude2)
|
||||
mapLatitude = utils.Str2Float64(params["mapLatitude"].(string))
|
||||
mapRadius = params["mapRadius"].(int)
|
||||
}
|
||||
for _, v := range storeList {
|
||||
@@ -262,9 +265,49 @@ func GetStores(ctx *jxcontext.Context, keyword string, params map[string]interfa
|
||||
}
|
||||
}
|
||||
dao.Commit(db)
|
||||
|
||||
retVal.MapCenterLng, retVal.MapCenterLat = getMapCenter(retVal.Stores)
|
||||
return retVal, err
|
||||
}
|
||||
|
||||
func getMapCenter(storeList []*StoreExt) (lng, lat float64) {
|
||||
if len(storeList) == 0 {
|
||||
return 0, 0
|
||||
}
|
||||
|
||||
lngAvg := float64(0)
|
||||
latAvg := float64(0)
|
||||
storeListLenFloat := float64(len(storeList))
|
||||
for _, store := range storeList {
|
||||
lngAvg += store.FloatLng
|
||||
latAvg += store.FloatLat
|
||||
}
|
||||
lngAvg = lngAvg / storeListLenFloat
|
||||
latAvg = latAvg / storeListLenFloat
|
||||
|
||||
lngMean := float64(0)
|
||||
latMean := float64(0)
|
||||
for _, store := range storeList {
|
||||
lngMean += (store.FloatLng - lngAvg) * (store.FloatLng - lngAvg)
|
||||
latMean += (store.FloatLat - latAvg) * (store.FloatLat - latAvg)
|
||||
}
|
||||
lngMean = math.Sqrt(lngMean / storeListLenFloat)
|
||||
latMean = math.Sqrt(latMean / storeListLenFloat)
|
||||
newStoreList := []*StoreExt{}
|
||||
for _, store := range storeList {
|
||||
if store.FloatLng >= lngMean-lngAvg && store.FloatLng <= lngMean+lngAvg &&
|
||||
store.FloatLat >= latMean-latAvg && store.FloatLat <= latMean+latAvg {
|
||||
lng += store.FloatLng
|
||||
lat += store.FloatLat
|
||||
newStoreList = append(newStoreList, store)
|
||||
}
|
||||
}
|
||||
if len(newStoreList) == len(storeList) {
|
||||
return lng / float64(len(newStoreList)), lat / float64(len(newStoreList))
|
||||
}
|
||||
return getMapCenter(newStoreList)
|
||||
}
|
||||
|
||||
func GetVendorStore(ctx *jxcontext.Context, vendorStoreID string, vendorID int) (retVal *StoreExt, err error) {
|
||||
if handler := CurVendorSync.GetStoreHandler(vendorID); handler != nil {
|
||||
result, err2 := handler.ReadStore(vendorStoreID)
|
||||
|
||||
Reference in New Issue
Block a user