项目作者: benedictkhoo

项目描述 :
NativesScript plugin for Google Places SDK
高级语言: TypeScript
项目地址: git://github.com/benedictkhoo/nativescript-google-places-sdk.git
创建时间: 2019-08-08T05:37:56Z
项目社区:https://github.com/benedictkhoo/nativescript-google-places-sdk

开源协议:MIT License

下载


NativesScript Google Place SDK

A NativesScript plugin for Google Places SDK.

Documentation:

Requirements

Enable Google Places API if you have not already done so.

Configure skip library check in tsconfig.json

  1. {
  2. "compilerOptions": {
  3. ...
  4. "skipLibCheck": true
  5. ...
  6. }
  7. }

Installation

  1. tns plugin add nativescript-google-place-sdk

Usage

NativeScript

Initialize the Place SDK

app.ts

  1. ...
  2. Place.initialize(isAndroid ? 'GOOGLE_PLACE_ANDROID_API_KEY' : 'GOOGLE_PLACE_IOS_API_KEY');
  3. ...

Show the Autocomplete UI

my-awesome-page.xml

  1. <Button text="Search" tap="search()"></Button>

my-awesome-page.ts

  1. export function search(): void {
  2. PlaceAutocomplete.show()
  3. .then(
  4. (result) => {
  5. if (result) {
  6. console.log('Place Details:', result);
  7. }
  8. },
  9. (err) => console.error(err)
  10. );
  11. }

NativeScript + Angular

Initialize the Place SDK

app.component.ts

  1. @Component({
  2. moduleId: module.id,
  3. selector: 'ns-app',
  4. templateUrl: 'app.component.html'
  5. })
  6. export class AppComponent implements OnInit {
  7. ngOnInit(): void {
  8. Place.initialize(isAndroid ? 'GOOGLE_PLACE_ANDROID_API_KEY' : 'GOOGLE_PLACE_IOS_API_KEY');
  9. }
  10. }

Show the Autocomplete UI

my-awesome.component.html

  1. <Button [text]="Search" (tap)="search()"></Button>

my-awesome.component.ts

  1. @Component({
  2. moduleId: module.id,
  3. selector: 'ns-awesome',
  4. templateUrl: 'my-awesome.component.html'
  5. })
  6. export class MyAwesomeComponent {
  7. search(): void {
  8. PlaceAutocomplete.show()
  9. .then(
  10. (result) => {
  11. if (result) {
  12. console.log('Place Details:', result);
  13. }
  14. },
  15. (err) => console.error(err)
  16. );
  17. }
  18. }

API

Place

initialize(apiKey)

Property Default Description
apiKey undefined Google Place API key

Example

  1. Place.initialize('GOOGLE_PLACE_API_KEY');

Autocomplete

show(options)

Property Default Description
fields undefined Define the types of field to retrieve. By default will retrieve all types.
ios undefined iOS related options
ios.appearance undefined iOS appearance options. See documentation for more info.
ios.appearance.primaryTextColor undefined Primary text color
ios.appearance.primaryTextHighlightColor undefined Primary text highlight color
ios.appearance.secondaryTextColor undefined Secondary text color
ios.appearance.tableCellBackgroundColor undefined Table cell background color
ios.appearance.tableCellSeparatorColor undefined Table cell separator color
ios.appearance.tintColor undefined Tint color
locationBias.southwest undefined Southwest bound
locationBias.northeast undefined Northeast bound
Field Data Type Description

address | string | Address |
id | string | Id |
lat_lng | object | Coordinates |
name | string | Place name |
phone_number | string | Phone number |
price_level | number | Pricing |
rating | number | Rating |
user_ratings_total | number | Total ratings |
utc_offset_minutes | number | UTC offset in minutes |
viewport | object | Viewport |
website_uri | string | URL |

Example

  1. PlaceAutocomplete.show({ fields: ['name'] })
  2. .then(
  3. (result) => {
  4. if (result) {
  5. console.log('Place Details:', result);
  6. }
  7. },
  8. (err) => console.error(err)
  9. );

License

MIT