Link to home
Start Free TrialLog in
Avatar of roy_sanu
roy_sanuFlag for India

asked on

Geolocation issue

we have a Geo location issue. In our angularjs application we are using Geo location   which find the current position of the place. is there is any issue with the code below

error it gives

   
 getCurrentPosition() and watchPosition() no longer work on insecure origins. To use this feature, you should consider switching your application to a secure origin, such as HTTPS. See https://goo.gl/rStTGz for more details.

Open in new window


Code is below

In Component:
/*---- geo code start ----*/
   
 public loginUrl = 'http://ws.geonames.org/countryCodeJSON?';
    lat: any;
    lng: any;
    verify: boolean=false;
    location = {};
    geoDetails = new GeoCounty();
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(this.setPosition.bind(this));
        }

setPosition(position) {
        this.location = position.coords;
        this.lat = position.coords.latitude;
        this.lng = position.coords.longitude;
        this.loginUrl = this.loginUrl + 'lat=' + this.lat + '&lng=' + this.lng + '&username=demo';
        console.log(this.loginUrl);
        this.getGeodata();
    }
    getGeodata() {
        this.verify=true;
        this.geoService.getGeoData(this.loginUrl)
            .subscribe(
            geoDetails => {
                if (geoDetails.countryCode == undefined || geoDetails.countryCode == '') {
                    geoDetails.countryCode = this.defcountrycode;
                }
                this.getCountryLanguages(geoDetails.countryCode);
                this.geoDetails = geoDetails;
                alert(geoDetails.countryCode);
            },
            error =>{ this.errorMessage = <any>error; alert(this.errorMessage);});
    }

Open in new window


    /*---- geo code end ----*/


In Service:
 
getGeoData(url): Observable<GeoCounty> {
    return this.http.get(url)
      .map(this.extractData)
      .catch(this.handleError);
  }

  private extractData(res: Response) {
    let body = res.json();
    return body || {};
  }

Open in new window

Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Try changing "http://ws.geonames.org/countryCodeJSON?" to "https://ws.geonames.org/countryCodeJSON?".  If you still get the same error, then you may have to switch the whole page it's in to 'https' which means getting an SSL/TLS certificate for your site.
Avatar of roy_sanu

ASKER

Thanks Dave for the information, it does not work on https.

what we need a  node service served thru cloudfront that reads the request header, gets the country code and returns the country code to the angular2 client.




I have a small snippet of  code written in nodejs

app.get('/getcountrydetails', function(req, res) {
		console.log('inside get country details  api');
		var headersinfo = req.headers;
		console.log('headersinfo name:>>>>'+headersinfo);
		var countrycode=headersinfo.countrycode;
		var countryname=headersinfo.countryname;
		console.log('country name:>>>>'+countryname);
		//res.write('config updated');
		res.send('countrycode:'+countrycode+'countryname:'+countryname);
	});

Open in new window


which works fine in getting the country code and country name


By question how i can served through Amazon cloud front, let us know
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.