angular-google-maps icon indicating copy to clipboard operation
angular-google-maps copied to clipboard

[Question] How can I use custom html elements inside <agm-circle> directive?

Open faizansaiyed opened this issue 7 years ago • 8 comments

I have a requirement where I need to draw a circle (with lat/long and radius of the circle) with four equal quadrants, dividing the circle into four equal halves.

Is it possible to use custom HTML elements inside so using CSS, I can draw vertical and horizontal lines defining quadrants? Something like below image:

Screenshot from 2019-05-28 15-04-30

Can I do something like this:

<agm-circle *ngFor="let circle of circles" [latitude]="circle.lat" [longitude]="circle.lng" [radius]="circle.radius"
    [fillColor]="circle.color" [circleDraggable]="true" [editable]="true">
    <div class="quadrants">
      <div class="vertical"></div>
      <div class="horizontal"></div>
    </div>
  </agm-circle>

Using Angular CLI 7 and [email protected]

faizansaiyed avatar May 28 '19 10:05 faizansaiyed

why does it have to be inside the circle? you can have two polylines next to it in the DOM, with the coordinates inside the circle.

<ng-container *ngFor="let circle of circles" >
  <agm-circle [latitude]="circle.lat" [longitude]="circle.lng" [radius]="circle.radius" [fillColor]="circle.color" [circleDraggable]="true" [editable]="true">
  </agm-circle>
  <agm-polyline>
    <agm-polyline-point [latitude]="circle.lat" [longitude]="circle.lng - meterToLng(circle.radius/2, circle.lat)"> 
    </agm-polyline-point>
    <agm-polyline-point [latitude]="circle.lat" [longitude]="circle.lng + meterToLng(circle.radius/2, circle.lat)"> 
    </agm-polyline-point>
  </agm-polyline>
  <agm-polyline>
    <agm-polyline-point [latitude]="circle.lat - meterToLat(circle.radius/2, circle.lng)" [longitude]="circle.lng"> 
    </agm-polyline-point>
    <agm-polyline-point [latitude]="circle.lat + meterToLat(circle.radius/2, circle.lng)" [longitude]="circle.lng"> 
    </agm-polyline-point>
  </agm-polyline>
</ng-container>

ghost avatar May 30 '19 09:05 ghost

Can you please also provide meterToLat/Lng functions also? I cant find proper one.

faizansaiyed avatar May 30 '19 10:05 faizansaiyed

https://gis.stackexchange.com/a/2964/90193

ghost avatar May 30 '19 13:05 ghost

Can understand what to calculate with the parameters you passed in your first answer and calculation provided in your second answer URL. Can you please specify exactly how to calculate?

Now poly-lines are generated but they are going out of the circle: Screenshot from 2019-05-31 15-35-54

Here is the calculation what I did from the answer given on StackEx:

meterToLng(val1: number, val2: number): number {
    return ((val1 * 100) / 111111) * Math.cos(val2);
}
meterToLat(val1: number, val2: number): number {
    return ((val1 * 100) / 111111) * Math.cos(val2);
}

faizansaiyed avatar May 31 '19 10:05 faizansaiyed

try

meterToLng(radius: number, lat: number): number {
    return ((radius) / 111111) *Math.cos(lat);
}
meterToLat(radius: number): number {
    return ((radius) / 111111);
}

ghost avatar May 31 '19 13:05 ghost

@doom777 This is not working. Following is the o/p:

Screenshot from 2019-06-03 11-55-54

faizansaiyed avatar Jun 03 '19 06:06 faizansaiyed

Ok, this time I tested and it works.

Basically two mistakes were that Math.cos accepts radians, not degrees, and the entire radius length should be the start of the line, and not half.

  meterToLng(radius: number, lat: number): number {
    return radius /( 111111 * Math.cos(lat * Math.PI / 180));
  }
  meterToLat(radius: number): number {
    return radius / 111111;
  }
<ng-container *ngFor="let circle of circles" >
  <agm-circle [latitude]="circle.lat" [longitude]="circle.lng" [radius]="circle.radius" [fillColor]="circle.color" [circleDraggable]="true" [editable]="true">
  </agm-circle>
  <agm-polyline>
    <agm-polyline-point [latitude]="circle.lat" [longitude]="circle.lng - meterToLng(circle.radius, circle.lat)"> 
    </agm-polyline-point>
    <agm-polyline-point [latitude]="circle.lat" [longitude]="circle.lng + meterToLng(circle.radius, circle.lat)"> 
    </agm-polyline-point>
  </agm-polyline>
  <agm-polyline>
    <agm-polyline-point [latitude]="circle.lat - meterToLat(circle.radius, circle.lng)" [longitude]="circle.lng"> 
    </agm-polyline-point>
    <agm-polyline-point [latitude]="circle.lat + meterToLat(circle.radius, circle.lng)" [longitude]="circle.lng"> 
    </agm-polyline-point>
  </agm-polyline>
</ng-container>

image

ghost avatar Jun 03 '19 10:06 ghost

Thanks a lot, savior! One more little help? My circle is editable, so if I change the radius of the circle, it should update the polylines. How can I do that? Currently, polylines do not update if I update radius, but if I change the center point, it works well.

I did this:

<agm-circle [latitude]="circle.lat" [longitude]="circle.lng" [radius]="circle.radius" [fillColor]="circle.color" circleDraggable]="false" [editable]="true" (radiusChange)="updateCircle($event, i)" (centerChange)="updateQuadrants($event, i)"></agm-circle>

and

updateQuadrants(event, index) {
  this.circles[index].lat = event.lat;
  this.circles[index].lng = event.lng;
}

updateCircle(event, index) {
  this.circles[index].radius = event;
}

With centerChange event, lines are getting updated, but not with radiusChange event: screen-capture

Thanks again!

faizansaiyed avatar Jun 04 '19 04:06 faizansaiyed