Geolocation with amp-list
Introduction
If you need more fine-grained geolocation support than provided by the amp-geo extension, you can implement geo-based features with geolocation logic on your backend and amp-list. This adds an additional XHR request but you can mitigate the negative UX effects with a skeleton layout in the placeholder attribute.
Setup
Import the amp-list
component ...
<script async custom-element="amp-list" src="https://cdn.ampproject.org/v0/amp-list-0.1.js"></script>
... and the amp-mustache
component
<script async custom-template="amp-mustache" src="https://cdn.ampproject.org/v0/amp-mustache-0.2.js"></script>
Geo-based search results
amp-list
makes a request to a server backend which does IP-based geolocation and returns relevant search results. For this demo, it calls an API to get nearby cities. The backend returns JSON results which amp-list
renders with amp-mustache
. The backend makes two remote API calls (and also adds 500ms of simulated delay); to mitigate the UX effects of this delay we use amp-list
's placeholder
to lay out a skeleton.
Since we need to have an initial fixed-size for amp-list we manually calculate that each card is 100 + 15px margin and the title is 16px + 20px margin:
16px + 20px + (5 * (100 + 15px)) = 611px
The placeholder list items largely use CSS to draw the skeleton elements as described in CSS-Tricks. The skeleton includes a tiny blurred map to add to the realism, and that map is also used while <amp-img>
loads the actual images to prevent a flash when the placeholder is removed.
You are in {{location}}
- {{#results}}
-
{{placeName}}
{{adminName1}}
{{countryCode}} {{/results}}
You are in
<amp-list class="geolist-preview" width="auto" height="640" layout="fixed-height" noloading src="https://caramel-wolf.glitch.me//location-specific-results.json" binding="no" single-item items=".">
<template type="amp-mustache" id="amp-template-id">
<h3>You are in {{location}}</h3>
<ul class="results">
{{#results}}
<li>
<amp-img alt="{{placeName}} Map" noloading layout="fixed" width="150" height="100" src="https://maps.googleapis.com/maps/api/staticmap?markers={{lat}},{{lng}}&zoom=9&size=150x100&maptype=roadmap&key=AIzaSyByT-0aYa-nEF0gGqJHNpEEK1bus00losI">
</amp-img>
<p>
<strong>{{placeName}}</strong><br>
{{adminName1}}<br>
{{countryCode}}
</p>
</li>
{{/results}}
</ul>
</template>
<div placeholder>
<h3>You are in <span class="placeholder"></span></h3>
<ul class="results">
<li></li><li></li><li></li><li></li><li></li>
</ul>
</div>
</amp-list>
이 페이지의 설명만으로 궁금한 점이 모두 해결되지 않는다면 다른 AMP 사용자에게 문의하여 구체적인 활용 사례를 논의해 보세요.
Stack Overflow로 이동 설명이 부족한 기능을 발견하셨나요?AMP 프로젝트는 여러분의 참여와 기여를 적극 환영합니다! 오픈 소스 커뮤니티를 통해 지속적으로 활동해 주셔도 좋지만 관심 있는 주제에 한 번만 기여하셔도 큰 도움이 됩니다.
GitHub에서 샘플 수정하기-
Written by @jamesshannon