عارض حي سكني

في هذا المثال، نضيف شريط البحث الإكمال التلقائي لمكان للعناوين للسماح للمستخدم بإدخال عنوان وتقييم وسائل الراحة القريبة. بالنسبة إلى المستخدمين المقيمين، تم ضبط placeTypePreferences على الأنواع المرتبطة عادةً بالحياة المنزلية. تقتصر العناوين الواردة في هذه العينة على الولايات المتحدة.

فهم الرمز

ضبط معدل تكرار أنواع الأماكن

عند تحديد أنواع الأماكن التي تريد عرضها في "مكتبة السياق المحلية"، يمكنك تخصيص وزن نسبي لكل خاصية باستخدام السمة weight (تظهر الأنواع التي لها ترجيح نسبي أعلى مرات أكثر). يحدد هذا المثال معدل تكرار أعلى للحدائق والمدارس إن توفر.

TypeScript

placeTypePreferences: [
  { type: "bakery", weight: 1 },
  { type: "bank", weight: 1 },
  { type: "cafe", weight: 2 },
  { type: "department_store", weight: 1 },
  { type: "drugstore", weight: 1 },
  { type: "park", weight: 3 },
  { type: "restaurant", weight: 2 },
  { type: "primary_school", weight: 3 },
  { type: "secondary_school", weight: 3 },
  { type: "supermarket", weight: 2 },
],

JavaScript

placeTypePreferences: [
  { type: "bakery", weight: 1 },
  { type: "bank", weight: 1 },
  { type: "cafe", weight: 2 },
  { type: "department_store", weight: 1 },
  { type: "drugstore", weight: 1 },
  { type: "park", weight: 3 },
  { type: "restaurant", weight: 2 },
  { type: "primary_school", weight: 3 },
  { type: "secondary_school", weight: 3 },
  { type: "supermarket", weight: 2 },
],

الإكمال التلقائي للأماكن

لاستخدام خدمة الإكمال التلقائي للأماكن، يجب أولاً تحميل مكتبة الأماكن، وواجهة برمجة تطبيقات JavaScript للخرائط. توضّح مستندات Autocomplete جميع المعلَمات المتاحة لتخصيص سلوك الإكمال التلقائي الخاص بالأماكن، مثل فلترة عبارات البحث المقترحة حسب الموقع الجغرافي أو نوع المكان.

  1. مكِّن واجهة برمجة تطبيقات الأماكن لمشروعك.
  2. حمِّل "مكتبة الأماكن" بالإضافة إلى مكتبة السياقات المحلية.

<script async
    src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=localContext,places&v=beta&callback=initMap">
</script>

  1. اتّبِع مستندات Autocomplete ونماذج الرموز للتعرّف على كيفية إضافة شريط بحث لميزة "الإكمال التلقائي" للأماكن إلى موقعك الإلكتروني أو خريطتك. في هذا المثال، الأسطر ذات الصلة هي:

TypeScript

// Build and add the Autocomplete search bar
const input = document.getElementById("input")! as HTMLInputElement;
const options = {
  types: ["address"],
  componentRestrictions: {
    country: "us",
  },
  fields: ["address_components", "geometry", "name"],
};
const autocomplete = new google.maps.places.Autocomplete(input, options);

JavaScript

// Build and add the Autocomplete search bar
const input = document.getElementById("input");
const options = {
  types: ["address"],
  componentRestrictions: {
    country: "us",
  },
  fields: ["address_components", "geometry", "name"],
};
const autocomplete = new google.maps.places.Autocomplete(input, options);

تجربة النموذج