Skip to content

Commit 93d4e32

Browse files
committed
fixed route
1 parent f5fc5e8 commit 93d4e32

File tree

7 files changed

+169
-34
lines changed

7 files changed

+169
-34
lines changed

app/Http/Controllers/ClinicController.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\Http\Controllers;
44

55
use App\Clinic;
6+
use App\Services\MapV2;
67
use App\Speciality;
78
use Illuminate\Database\Eloquent\Collection;
89
use Illuminate\Http\Request;
@@ -56,7 +57,22 @@ public function index(string $locale)
5657
*/
5758
public function show(string $locale, Clinic $clinic)
5859
{
60+
$config['center'] = "{$clinic->country->name}, {$clinic->city}, {$clinic->address}";
61+
$config['zoom'] = '15';
62+
$config['height'] = '450px';
63+
$config['apiKey'] = config('services.google.maps.api-key');
64+
$config['language'] = $locale;
65+
66+
$map = new MapV2($config);
67+
$map->add_marker([
68+
'position' => $config['center'],
69+
'title' => $clinic->name,
70+
]);
71+
$renderedMap = $map->create_map();
72+
73+
5974
return view('frontend.clinic-details')
60-
->with('clinic', $clinic);
75+
->with('clinic', $clinic)
76+
->with('map', $renderedMap);
6177
}
6278
}

app/Services/MapV2.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
namespace App\Services;
4+
5+
use GeneaLabs\LaravelMaps\Map;
6+
7+
class MapV2 extends Map
8+
{
9+
public function get_lat_long_from_address($address, $attempts = 0)
10+
{
11+
$lat = 0;
12+
$lng = 0;
13+
14+
$error = '';
15+
16+
if ($this->geocodeCaching) { // if caching of geocode requests is activated
17+
18+
$CI = & get_instance();
19+
$CI->load->database();
20+
$CI->db->select("latitude,longitude");
21+
$CI->db->from("geocoding");
22+
$CI->db->where("address", trim(strtolower($address)));
23+
$query = $CI->db->get();
24+
25+
if ($query->num_rows() > 0) {
26+
$row = $query->row();
27+
28+
return array($row->latitude, $row->longitude);
29+
}
30+
}
31+
//utf8_encode($address) will return only english adress mean it's take only english address.
32+
// Remove utf8_encode from urlencode then it'll support all languages(eg. en, ur, chinese, russian, japanese, greek etc.)
33+
// $data_location = "https://maps.google.com/maps/api/geocode/json?address=".urlencode(utf8_encode($address)); //Old One just for english
34+
$data_location = "https://maps.google.com/maps/api/geocode/json?address=".urlencode($address); // New One for every language.
35+
36+
if ($this->apiKey != "") {
37+
$data_location .= '&key='.$this->apiKey.'&';
38+
}
39+
40+
if ($this->region != "" && strlen($this->region) == 2) {
41+
$data_location .= "&region=".$this->region;
42+
}
43+
$data = file_get_contents($data_location);
44+
$data = json_decode($data);
45+
46+
if ($data->status == "OK") {
47+
$lat = $data->results[0]->geometry->location->lat;
48+
$lng = $data->results[0]->geometry->location->lng;
49+
50+
if ($this->geocodeCaching) { // if we to need to cache this result
51+
if ($address != "" && $lat != 0 && $lng != 0) {
52+
$data = array(
53+
"address" => trim(strtolower($address)),
54+
"latitude" => $lat,
55+
"longitude" => $lng,
56+
);
57+
$CI->db->insert("geocoding", $data);
58+
}
59+
}
60+
} else {
61+
if ($data->status == "OVER_QUERY_LIMIT") {
62+
$error = $data->status;
63+
if ($attempts < 2) {
64+
sleep(1);
65+
++$attempts;
66+
list($lat, $lng, $error) = $this->get_lat_long_from_address($address, $attempts);
67+
}
68+
}
69+
}
70+
71+
return array($lat, $lng, $error);
72+
}
73+
}

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"fideloper/proxy": "^4.2",
1919
"flynsarmy/csv-seeder": "^2.0",
2020
"fruitcake/laravel-cors": "^1.0",
21+
"genealabs/laravel-maps": "^0.7.0",
2122
"guzzlehttp/guzzle": "^6.3",
2223
"laravel/framework": "^7.0",
2324
"laravel/tinker": "^2.0",

composer.lock

Lines changed: 67 additions & 29 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/services.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,9 @@
3030
'region' => env('AWS_DEFAULT_REGION', 'us-east-1'),
3131
],
3232

33+
'google' => [
34+
'maps' => [
35+
'api-key' => env('GOOGLE_MAPS_API_KEY'),
36+
],
37+
],
3338
];

resources/views/frontend/clinic-details.blade.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,10 @@
9696
</div>
9797
</div>
9898
<section class="mb-0 clinic-map">
99-
{{-- <iframe src="https://www.google.com/maps/embed?q=1%20Grafton%20Street,%20Dublin,%20Ireland" width="100%" height="450" frameborder="0" style="border:0;" allowfullscreen="" aria-hidden="false" tabindex="0"></iframe>--}}
100-
<iframe width="100%" height="450" frameborder="0" style="border:0;" allowfullscreen="" aria-hidden="false" tabindex="0"
101-
src="https://maps.google.com/maps?height=450&amp;hl={{ request()->route()->parameters['locale'] }}&amp;q={{ urlencode("{$clinic->country->name}, {$clinic->city}, {$clinic->address}") }}&amp;t=&amp;z=15&amp;ie=UTF8&amp;iwloc=B&amp;output=embed"
102-
></iframe>
99+
{!! $map['html'] !!}
103100
</section>
104101
@endsection
102+
103+
@section('head-scripts')
104+
{!! $map['js'] !!}
105+
@endsection

resources/views/layouts/app.blade.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
<link href="{{ asset('/css/font-awesome.css') }}" rel="stylesheet"/>
3939
<link href="{{ mix('/css/app.css') }}" rel="stylesheet">
4040
<link type="text/css" href="{{ mix('/css/argon-design-system.css') }}" rel="stylesheet">
41+
@yield('head-scripts')
4142
</head>
4243
<body>
4344
<div id="app">

0 commit comments

Comments
 (0)