PHP谷歌地图页面复制及现有代码迁移适配技术求助
Let's break down how to tackle both of your requirements step by step, using the code snippet from models/project.php as a starting point.
To replicate the Google Maps feature on your new page, you'll need to handle both backend data retrieval and frontend map rendering:
Extract and reuse location logic from
getDetailProject():
The existing method already fetches project data (including location) via$row->load($id);. You can expand this to explicitly pull location fields and make it reusable for your new page. Here's an adapted version:function getDetailProject(){ $app = JFactory::getApplication(); $db = JFactory::getDbo(); $user = JFactory::getUser(); // Keep or adjust the parameter name based on your new page's URL structure $id = $app->input->get('id', 0, 'int'); $config = JblanceHelper::getConfig(); $sealProjectBids = $config->sealProjectBids; $row = JTable::getInstance('project', 'Table'); $row->load($id); // Extract location data (match field names to your project table schema) $locationData = [ 'address' => $row->address, 'latitude' => $row->lat, 'longitude' => $row->lng ]; // Merge project data with location for easy access in the view return (object) array_merge((array)$row, ['location' => $locationData]); }Replicate frontend map initialization:
Copy the HTML/JavaScript from your original page that renders the map, and adjust it to work with the new page's data:- Add the Google Maps API script to your new page's
<head>(replace with your API key):<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap" async defer></script> - Add a map container element:
<div id="map" style="width: 100%; height: 400px;"></div> - Copy the map initialization function, using the location data passed from PHP:
function initMap() { // Pull location data from the view (passed via Joomla's controller) const projectLocation = <?php echo json_encode($this->project->location); ?>; const map = new google.maps.Map(document.getElementById('map'), { center: { lat: parseFloat(projectLocation.latitude), lng: parseFloat(projectLocation.longitude) }, zoom: 12 }); // Add a marker for the project's location new google.maps.Marker({ position: { lat: parseFloat(projectLocation.latitude), lng: parseFloat(projectLocation.longitude) }, map: map, title: projectLocation.address }); }
- Add the Google Maps API script to your new page's
Pass data to the new page's view:
In your new page's controller, call the adaptedgetDetailProject()method and assign the result to the view, just like the original page does:$model = $this->getModel('Project'); $this->project = $model->getDetailProject();
Since the new page has similar but modified call logic, focus on adjusting the parts that differ from the original page:
Adjust parameter retrieval:
If the new page uses a different query parameter for the project ID (e.g.,project_idinstead ofid), update the$idline in the model method:// Original: $id = $app->input->get('id', 0, 'int'); // Modified for new page: $id = $app->input->get('project_id', 0, 'int');Update access/permission checks:
If the new page requires different user permissions (e.g., public access instead of logged-in users), tweak the user validation logic. For example:// Original (example login check): if (!$user->id) { $app->redirect(JRoute::_('index.php?option=com_users&view=login', false)); return; } // Modified for public access: // Remove the redirect, or add a check for specific user groups if neededAlign data with the new view structure:
If the new page's view expects data under a different variable name (e.g.,$iteminstead of$project), adjust the controller assignment:// Original: $this->project = $model->getDetailProject(); // Modified: $this->item = $model->getDetailProject();Then update your frontend template to reference
$this->iteminstead of$this->project.Test edge cases:
Verify that:- The new page loads project data correctly when the ID parameter is passed
- The map renders without JavaScript errors
- Custom logic like sealed bids (
$sealProjectBids) behaves as expected in the new context
内容的提问来源于stack exchange,提问作者DoMiester

