import { HttpClient } from '@angular/common/http' ; import { Injectable } from '@angular/core' ; @ Injectable ( ) export class GeolocationProvider { currentCity : string ; currentCityId : number ; constructor ( public http ) { } getCurrentCity ( ) { let AMapWebServiceKey = "e24694ee3d12cdc0804aa0040a731cb2" ; let AMapIPLocatingUrl = "http://restapi.amap.com/v3/ip?key=" + AMapWebServiceKey ; this . http . get ( AMapIPLocatingUrl ) . subscribe ( data => { let AMapGeocodeingUrl = "http://restapi.amap.com/v3/geocode/geo?key=" + AMapWebServiceKey + "&address=" + data [ "city" ] ; this . http . get ( AMapGeocodeingUrl ) . subscribe ( data => { let longitude = data [ "geocodes" ] [ 0 ] [ "location" ] . split ( "," ) [ 0 ] ; let latitude = data [ "geocodes" ] [ 0 ] [ "location" ] . split ( "," ) [ 1 ] ; let meituanLocatingUrl = "http://api.mobile.meituan.com/group/v1/city/latlng/" + latitude + "," + longitude + "?tag=0" ; this . http . get ( meituanLocatingUrl ) . subscribe ( data => { this . currentCity = data [ "data" ] [ "city" ] ; this . currentCityId = data [ "data" ] [ "id" ] ; console . log ( "current city" , this . currentCity ) ; console . log ( "current city id" , this . currentCityId ) ; } ) ; } ) ; } ) ; } }