0

Coding ionic framework 2.

I have a locationtracker, that is giving me the exact latitude and longitude of a person.

I want to have ONLY the string printed from the storeArray, and not the number from the equation.

How is it possible, to ignore the number, from the variable, and only print the store-name out?

    this.storeArray = [distValby, distKbhK, distRoskilde];
    this.storeArray.sort();

Sorted by lowest number by default.

Fairly new to ionic 2 and typescript in all.

Thanks!

import { StoreLocation } from '../../providers/store-location';
import { Component, OnInit } from '@angular/core';
import { NavController } from 'ionic-angular';
import { LocationTracker } from '../../providers/location-tracker';

@Component({
  selector: 'page-location',
  templateUrl: 'location.html'
})

export class LocationPage implements OnInit{

  public usrStoreLat: any;
  public usrStoreLong: any;
  public usrDistance: any;
  public storeArray: Array<any> = [];

  public roskilde: StoreLocation = new StoreLocation("Bagel Roskilde", "Trekroner st. 4", 55.650107, 12.131997, null);
  public valby: StoreLocation = new StoreLocation("Bagel Valby", "Valby Langgade 12", 55.668071, 12.497865, null);
  public kbhK: StoreLocation = new StoreLocation("Indre København", "Axeltorv 5, 1609 København V", 55.6887527, 12.5404156, null);

  constructor(public navCtrl: NavController, public locationTracker: LocationTracker) {
  }

  // START GEO TRACKING FOR NUVÆRENDE POSITION

  ngOnInit(){
    this.start();
  }

    public start(){
  	this.locationTracker.startTracking();
    this.usrStoreLat = this.locationTracker.lat;
    this.usrStoreLong = this.locationTracker.lng;

    
    if(this.locationTracker.lat != null){
    console.log('Tracker nu!');
    console.log('person lokation lat:' + this.usrStoreLat);
    console.log('person lokation long:' + this.usrStoreLong);
    this.calculateDist();
    }
  }


  public calculateDist (){
    var distRoskilde  = ((this.usrStoreLat-this.roskilde.lat)+(this.usrStoreLong-this.roskilde.long)) + this.roskilde.name;
    var distValby     = ((this.usrStoreLat-this.valby.lat)+(this.usrStoreLong-this.valby.long)) + this.valby.name;
    var distKbhK      = ((this.usrStoreLat-this.kbhK.lat)+(this.usrStoreLong-this.kbhK.long)) + this.kbhK.name;

    this.storeArray = [distValby, distKbhK, distRoskilde];
    this.storeArray.sort();
    
    console.log('se:' + typeof(this.storeArray[0]) + this.storeArray[0].toString());    
    // console.log('store arrayet:' + this.storeArray[0]);
  }

  stop(){
  	this.locationTracker.stopTracking();
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<ion-header>
	<ion-navbar>
	  <ion-title>
	    Location Tracker
	  </ion-title>
	</ion-navbar>
</ion-header>

<ion-content>
	<h4>Current Latitude: {{locationTracker.lat}}</h4>
	<h4>Current Longitude: {{locationTracker.lng}}</h4>

		<p> Tætteste butik: {{this.storeArray[0]}} </p>

	<button ion-button full primary (click)="start()">Find nærmeste butik</button>
	<button ion-button full primary (click)="stop()">Stop Tracking</button>
</ion-content>

6
  • console.log(this.storeArray[0].name); Is that what you're asking? Commented Dec 4, 2016 at 14:24
  • I tried that, but it returns "undefined". But you get the idea. The name is what i want to have shown. Commented Dec 4, 2016 at 14:30
  • I've tried to include the StoreLocation in the constructor, so i could provide the name from the store class, but it returns some error looking something like this: "ORIGINAL EXCEPTION: No provider for String" Commented Dec 4, 2016 at 14:31
  • You're concatenating a unrelated things. Don't do that. Use objects, with fields: const distRoskilde = {latitudeDiff: this.usrStoreLat - this.roskilde.lat, longitudeDiff: this.usrStoreLong - this.roskilde.long, name: this.roskilde.name}; Commented Dec 4, 2016 at 14:35
  • I get your point! How should i later call them by name? console.log(this.storeArray[0].name)? Commented Dec 4, 2016 at 14:36

3 Answers 3

1

Instead of saving the distance and the name together as a String, you should create objects for each "store".

First, you calculate the distance without adding the name:

var distRoskilde  = ((this.usrStoreLat-this.roskilde.lat) + 
    (this.usrStoreLong-this.roskilde.long));

Then you create the array using objects, like so:

this.storeArray = [{dist: distRoskilde, name: this.rokslide.name},
    {dist: distValby, name: this.valby.name}, ...];

Now, in your HTML, you can access the name like this:

<p> Tætteste butik: {{this.storeArray[0].name}} </p>

If you need to print them together for logging purposes, do it like this:

console.log('se:' + typeof(this.storeArray[0]) + this.storeArray[0].dist 
    + " " + this.storeArray[0].name);
Sign up to request clarification or add additional context in comments.

4 Comments

I see the point, but it still prints out all the information from the array.
Ok, seems to work at the console.log output, but i get this error, if i run the HTML code {{this.storeArray[0].name}} = [16:07:16] console.error: ORIGINAL EXCEPTION: Cannot read property 'name' of undefined
try it without using "this" like so: {{storeArray[0].name}}
Now it prints [object Object] at the "Tætteste butik" in html.
0

  public calculateDist (){

    var distRoskilde  = ((this.usrStoreLat-this.roskilde.lat)+(this.usrStoreLong-this.roskilde.long));
    var distValby     = ((this.usrStoreLat-this.valby.lat)+(this.usrStoreLong-this.valby.long));
    var distKbhK      = ((this.usrStoreLat-this.kbhK.lat)+(this.usrStoreLong-this.kbhK.long));
    
    this.storeArray = [{dist: distRoskilde, name: this.roskilde.name}, {dist: distValby, name: this.valby.name}, {dist: distKbhK, name:this.kbhK.name}];
    this.storeArray.sort();
    
    console.log('se:' + typeof(this.storeArray[0]) + this.storeArray[0].dist + " " + this.storeArray[0].name);
  }

@ggrading So now this, right?

1 Comment

Exactly and don't forget to add the .name in your HTML as well like seen in my example
0

I did what @ggrading suggested, but needed to make a variabel for showing it in HTML section:

nearestStore : string;

this.nearestStore = this.storeArray[0].name;

in the html: {{nearestStore}}

Thank you!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.