[go: up one dir, main page]

Skip to content

Commit

Permalink
feat(core): support search location
Browse files Browse the repository at this point in the history
  • Loading branch information
CofCat456 committed Aug 12, 2023
1 parent 18a1878 commit b102e5f
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 6 deletions.
73 changes: 73 additions & 0 deletions packages/core/src/composables/useSearch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import type { Ref } from 'vue';
import { markRaw, onMounted, reactive, ref, toValue } from 'vue';
import { useMap } from './useMap';

export function useSearch(apiKey: string) {
const isLoading = ref(false);
const cGoogle = ref<typeof google>();
const cApi = ref<typeof google.maps>();
const cGeocoder = ref<google.maps.Geocoder>();
const coordinates = reactive<google.maps.LatLngLiteral>({
lat: 0,
lng: 0,
});

function resetCoordinates() {
Object.assign(coordinates, {
lat: 0,
lng: 0,
});
}

const getCoordinates = async (address: string | Ref<string>) => {
isLoading.value = true;

if (!cGeocoder.value)
return;

if (!address && !toValue(address))
return;

const request: google.maps.GeocoderRequest = {
address: toValue(address),
};

// NOTE: 使用地理編碼服務將地址轉換為地理座標
await cGeocoder.value.geocode(request, (results, status) => {
if (status === 'OK' && results && results.length > 0) {
const { location } = results[0].geometry;

if (!location)
return;

const { lat, lng } = location;

if (!lat || !lng)
return;

// 設置座標
Object.assign(coordinates, { lat: lat(), lng: lng() });

isLoading.value = false;
}
else {
resetCoordinates();

isLoading.value = false;
}
});
};

onMounted(async () => {
const { loader } = useMap(apiKey);
cGoogle.value = markRaw(await loader.load());
cApi.value = markRaw(cGoogle.value.maps);
cGeocoder.value = markRaw(new cApi.value.Geocoder());
});

return {
isLoading,
coordinates,
getCoordinates,
};
}
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './composables/useMap';
export * from './composables/useSearch';
19 changes: 18 additions & 1 deletion packages/map/src/components/GoogleMap/src/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,23 @@ import { apiSymbol, mapSymbol } from '@/utlis/symbol';
import { taiwanRestriction } from '@/utlis/mapUtlis';
import { hasChanged, transformCenter } from '@/utlis';
import { mapEvents } from '@/utlis/events';
import type { FullscreenControlOptions, IconMouseEvent, LatLng, LatLngLiteral, Map, MapMouseEvent, MapRestriction, MapTypeControlOptions, MapTypeStyle, PanControlOptions, RotateControlOptions, ScaleControlOptions, StreetViewControlOptions, StreetViewPanorama, ZoomControlOptions } from '@/types';
import type {
FullscreenControlOptions,
IconMouseEvent,
LatLng,
LatLngLiteral,
Map,
MapMouseEvent,
MapRestriction,
MapTypeControlOptions,
MapTypeStyle,
PanControlOptions,
RotateControlOptions,
ScaleControlOptions,
StreetViewControlOptions,
StreetViewPanorama,
ZoomControlOptions,
} from '@/types';
interface CofMap {
cGoogle: typeof google | undefined
Expand Down Expand Up @@ -196,6 +212,7 @@ defineExpose({
</template>

<style scoped>
/* FIXME: error css */
.map-container,
#map {
width: 100%;
Expand Down
13 changes: 8 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b102e5f

Please sign in to comment.