alexpechkarev/laravel-places-autocomplete 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

alexpechkarev/laravel-places-autocomplete

Composer 安装命令:

composer require alexpechkarev/laravel-places-autocomplete

包简介

Easily integrate the Places (New) Autocomplete - JavaScript library into Laravel Blade views. Provides a user-friendly way to search for and retrieve detailed address information in any web application.

README 文档

README

GitHub Stars Latest Version on Packagist Total Downloads GitHub License

A Laravel package that provides a simple Blade component to integrate the Google Places Autocomplete functionality into your application with minimal setup.

It handles API loading, session tokens for cost-effective usage, and fetching place details, allowing you to focus on building your application.

Features

  • Simple Integration: Add address autocomplete to any form with a single Blade component.
  • Cost-Effective: Automatically handles session tokens to reduce Google API costs.
  • Customizable: Configure the component's behavior through an options array.
  • Event-Driven: Dispatches a custom event with the selected place data for easy handling in your frontend JavaScript.

Live Demo

See a comprehensive live demo of the underlying JavaScript library in action: pacservice.pages.dev

A video demonstrating the Places Autocomplete JavaScript component in action, showing address suggestions and selection.

Requirements

  • Laravel 9.x or higher
  • A Google Maps API Key with the Places API enabled. You can get one from the Google Cloud Console.

Installation

You can install the package via Composer:

composer require alexpechkarev/laravel-places-autocomplete

Configuration

  1. Publish the configuration file. This will create a config/places-autocomplete.php file where you can set default options. The view file will be published to resources/views/vendor/places-autocomplete and the JavaScript files to public/vendor/places-autocomplete/.
php artisan vendor:publish --provider="PlacesAutocomplete\PlacesAutocompleteServiceProvider"
  1. Add your Google Maps API key to your .env file. This is a required step.
// filepath: .env
GOOGLE_MAPS_API_KEY="YOUR_API_KEY_HERE"

Usage

1. Add the Component

Use the <x-places-autocomplete> component in your Blade views. Because the component's root element has a dynamically generated ID, the recommended approach is to wrap it in your own div with a stable ID. This makes it easy to target with JavaScript.

// filepath: resources/views/your-form.blade.php
<form>
    <label for="address">Address</label>

    {{-- Wrap the component in a div with a known ID --}}
    <div id="address-wrapper">
        <x-places-autocomplete
            root-div-id="address-wrapper"
            :options="[
                'placeholder' => 'Start typing your address...',
            ]"
        />
    </div>

    {{-- Other fields to be populated by JavaScript --}}
    <div class="mt-4">
        <label for="city">City</label>
        <input type="text" id="city" name="city" class="form-control">
    </div>
    <div>
        <label for="postcode">Postcode</label>
        <input type="text" id="postcode" name="postcode" class="form-control">
    </div>
</form>

2. Handle the Response

When a user selects an address, the component dispatches a pac-response custom event on its root div. You can listen for this event to receive the place data.

Add the following JavaScript to your application.

// filepath: resources/js/app.js
document.addEventListener("DOMContentLoaded", function () {
  // Listen for the custom event emitted by the PlacesAutocomplete component
  document
    .getElementById("address-wrapper")
    .addEventListener("pac-response", function (event) {
      const placeDetails = event.detail;
      //console.log('Place Selected:', placeDetails);

      // Populate other fields based on the selected place details
      if (placeDetails.addressComponents) {
        placeDetails.addressComponents.forEach((component) => {
          if (component.types.includes("postal_town")) {
            document.getElementById("city").value = component.longText;
          }
          if (component.types.includes("postal_code")) {
            document.getElementById("postcode").value = component.longText;
          }
        });
      }
    });

  // Handle errors from the PlacesAutocomplete component
  document
    .getElementById("address-wrapper")
    .addEventListener("pac-error", function (event) {
      console.error("Autocomplete Error:", event.detail);
    });
});

Component Options

You can customize the component by passing an :options array. These options are passed to the underlying places-autocomplete-js library.

Option Type Description
placeholder string Placeholder text for the input field.
debounce integer Delay in milliseconds before sending a request (default: 100).
clear_input boolean Whether to clear the input after selection.
language string The language code for results (e.g., 'en-GB').

For a full list of options, please refer to the JavaScript library's documentation.

Note: The name attribute of the generated <input> is not currently configurable. The input's data will not be submitted with a standard form post; you must use JavaScript to handle the pac-response event and populate your form fields as shown in the example above.

Support

If you encounter any issues or have questions, please open an issue on GitHub.

License

This package is open-source software licensed under the MIT license.

alexpechkarev/laravel-places-autocomplete 适用场景与选型建议

alexpechkarev/laravel-places-autocomplete 是一款 基于 JavaScript 开发的 Composer 扩展包,目前已累计 355 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 06 月 22 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「laravel」 「Google Places Api」 「address autocomplete」 「Blade component」 「Google Autocomplete」 「places autocomplete」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 alexpechkarev/laravel-places-autocomplete 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 alexpechkarev/laravel-places-autocomplete 我们能提供哪些服务?
定制开发 / 二次开发

基于 alexpechkarev/laravel-places-autocomplete 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 355
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 20
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 0
  • 开发语言: JavaScript

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-06-22