説明なし

placedetail.page.ts 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import { Component, OnInit, Input, ViewChild, ElementRef } from '@angular/core';
  2. import { ActivatedRoute, Router } from '@angular/router';
  3. import { WpServiceService } from '../wp-service.service';
  4. import { Geolocation } from '@ionic-native/geolocation/ngx';
  5. import { NativeGeocoder, NativeGeocoderResult, NativeGeocoderOptions } from '@ionic-native/native-geocoder/ngx';
  6. declare var google;
  7. @Component({
  8. selector: 'app-placedetail',
  9. templateUrl: './placedetail.page.html',
  10. styleUrls: ['./placedetail.page.scss'],
  11. })
  12. export class PlacedetailPage implements OnInit {
  13. data: any;
  14. placelist: any;
  15. @ViewChild('map', { static: false }) mapElement: ElementRef;
  16. map: any;
  17. address: string;
  18. latitude: number;
  19. longitude: number;
  20. latdynamic: any;
  21. londynamic: any;
  22. constructor(private wpservice: WpServiceService, private route: ActivatedRoute, private router: Router, private geolocation: Geolocation, private nativeGeocoder: NativeGeocoder) { }
  23. @Input() id: string;
  24. showDiss = false;
  25. ngOnInit() {
  26. if (this.id != undefined) {
  27. this.showDiss = true;
  28. }
  29. this.loadMap();
  30. // console.log("showDiss ", this.showDiss);
  31. // let id = this.route.snapshot.paramMap.get('id') || this.id;
  32. // console.log("fetching ...");
  33. // this.wpservice.getPlaceDetail(id).subscribe(data => {
  34. // let post = JSON.parse(data.data);
  35. // console.log(" get data ", post);
  36. // post['media_url'] = post['_embedded']['wp:featuredmedia'][0]['media_details'].sizes['medium'].source_url;
  37. // this.data = post;
  38. let id = this.route.snapshot.paramMap.get('id') || this.id;
  39. console.log("fetching ...");
  40. this.wpservice.getPlaceDetail(id).subscribe((data) => {
  41. this.placelist = data;
  42. this.latdynamic = data['acf']['gmap']['lat'];
  43. this.londynamic = data['acf']['gmap']['lng'];
  44. console.log("load Place Detail ...");
  45. console.log(this.latdynamic);
  46. console.log("load Lati ...");
  47. console.log(this.londynamic);
  48. console.log("load Lon ...");
  49. console.log(data);
  50. }, error => {
  51. console.log("errror ", error);
  52. });
  53. }
  54. loadMap() {
  55. this.geolocation.getCurrentPosition().then((resp) => {
  56. this.latitude = resp.coords.latitude;
  57. this.longitude = resp.coords.longitude;
  58. // this.latdynamic = resp.coords.latitude;
  59. // this.londynamic = resp.coords.longitude;
  60. let latLng = new google.maps.LatLng(resp.coords.latitude, resp.coords.longitude);
  61. let mapOptions = {
  62. center: latLng,
  63. zoom: 15,
  64. mapTypeId: google.maps.MapTypeId.ROADMAP
  65. }
  66. this.getAddressFromCoords(resp.coords.latitude, resp.coords.longitude);
  67. this.map = new google.maps.Map(this.mapElement.nativeElement, mapOptions);
  68. this.map.addListener('dragend', () => {
  69. this.latitude = this.map.center.lat();
  70. this.longitude = this.map.center.lng();
  71. this.getAddressFromCoords(this.map.center.lat(), this.map.center.lng())
  72. });
  73. }).catch((error) => {
  74. console.log('Error getting location', error);
  75. });
  76. }
  77. getAddressFromCoords(lattitude, longitude) {
  78. console.log("getAddressFromCoords " + lattitude + " " + longitude);
  79. let options: NativeGeocoderOptions = {
  80. useLocale: true,
  81. maxResults: 5
  82. };
  83. this.nativeGeocoder.reverseGeocode(lattitude, longitude, options)
  84. .then((result: NativeGeocoderResult[]) => {
  85. this.address = "";
  86. let responseAddress = [];
  87. for (let [key, value] of Object.entries(result[0])) {
  88. if (value.length > 0)
  89. responseAddress.push(value);
  90. }
  91. responseAddress.reverse();
  92. for (let value of responseAddress) {
  93. this.address += value + ", ";
  94. }
  95. this.address = this.address.slice(0, -2);
  96. })
  97. .catch((error: any) => {
  98. this.address = "Address Not Available!";
  99. });
  100. }
  101. // getAddressFromCoords2(lattitude, longitude) {
  102. // console.log("getAddressFromCoords2 " + lattitude + " " + longitude);
  103. // let options: NativeGeocoderOptions = {
  104. // useLocale: true,
  105. // maxResults: 5
  106. // };
  107. // }
  108. // getGeoencoder(latitude, longitude) {
  109. // this.nativeGeocoder.reverseGeocode(latitude, longitude, this.geoencoderOptions)
  110. // .then((result: NativeGeocoderResult[]) => {
  111. // this.address = this.generateAddress(result[0]);
  112. // })
  113. // .catch((error: any) => {
  114. // alert('Error getting location' + JSON.stringify(error));
  115. // });
  116. // }
  117. }