【问题标题】:How to get country, state and city name globally from google auto complete service如何从谷歌自动完成服务获取全球国家、州和城市名称
【发布时间】:2020-08-14 11:25:57
【问题描述】:

在我的前端(反应)要求之一中,一旦从自动完成建议中选择了一个地方,我需要获取全球国家名称、州名、城市名称、placeId、纬度和经度。我能够从响应的几何键中获取城市名称(来自名称键)、placeId、纬度和经度。我正在使用这个 google 脚本,使用 place api 服务 => ---------------------------------- this.autocomplete = 新 google.maps.places.Autocomplete(this.autocompleteInput.current, {“类型”:['(城市)']}); this.autocomplete.addListener('place_changed', this.handlePlaceChanged); ---------------------------------- 在 handlePlaceChanged 函数中,我得到以下响应 =>>

 {"address_components":[{"long_name":"Bengaluru","short_name":"Bengaluru","types": 

["locality","political"]},{"long_name":"Bangalore Urban","short_name":"Bangalore 城市","类型":["administrative_area_level_2","political"]}, {"long_name":"卡纳塔克邦","short_name":"KA","types": ["administrative_area_level_1","political"]},{"long_name":"India","short_name":"IN","types": ["country","political"]}],"adr_address":"Bengaluru, Karnataka, India","formatted_address":"Bengaluru, Karnataka, India","geometry": {"location":{"lat":12.9715987,"lng":77.5945627},"viewport":{"south":12.7342888,"west":77.3791981,"north":13.173706,"east":77.8826809}}, "icon":"https://maps.gstatic.com/mapfiles/place_api/icons/geocode-71.png","id":"0862832923832bfb1e46cbe843cdaa03a9ee8aa1","name":"Bengaluru","photos":[{"height":405,"html_attributions":["https://maps. google.com/maps/contrib/113170619945048632846\">abdusamad k"],"width":720},{"height":385,"html_attributions":["https://maps.google.com/maps/contrib /105596602452460947497\">Rince TR"],"width":796},{"height":2340,"html_attributions":["https://maps.google.com/maps/contrib/107994729306954910118\">Riju Pal "],"width":4160},{"height":315,"html_attributions":["https://maps.google.com/maps/contrib/101926288772104749853\">Dipendra Bist"],"width": 534},{"高度":2592,"html_attributions":["https://maps.google.com/maps/contrib/100418682723295961842\">Achyuth rupavatharam"],"宽度":1944},{"高度" :424,"html_attributions":["https://maps.google.com/maps/contrib/118332000005283281641\">Jerin Asher Sojan"],"width":640},{"heigh t":2448,"html_attributions":["https://maps.google.com/maps/contrib/102738049583390423145\">DEBAJYOTI GHATAK"],"width":3264},{"height":789,"html_attributions ":["https://maps.google.com/maps/contrib/109699434808207248873\">nitin kumar"],"width":1080},{"height":3024,"html_attributions":["https:// /maps.google.com/maps/contrib/109460450047584922534\">Blue Dares"],"width":4032},{"height":738,"html_attributions":["https://maps.google.com/ maps/contrib/116995855942077255039\">Chelsy Halder"],"width":1599}],"place_id":"ChIJbU60yXAWrjsR4E9-UejD3_g","re​​ference":"ChIJbU60yXAWrjsR4E9-UejD3_g","scope":"GOOGLE","类型":["locality","political"],"url":"https://maps.google.com/?q=Bengaluru,+Karnataka,+India&ftid=0x3bae1670c9b44e6d:0xf8dfc3e8517e4fe0","utc_offset":330,"vicinity":"Bengaluru","html_attributions":[],"utc_offset_minutes":330}

=============================================================

I need guidance on how should I get Country name, State name, City name globally

【问题讨论】:

    标签: reactjs googleplacesautocomplete


    【解决方案1】:

    好吧,我在谷歌上搜索了一些解决方案,最终制作了自己的解决方案,这对我有用。它是解决方案代码的主要部分:

    compIsType(t, s) { 
        for(let z = 0; z < t.length; ++z) 
           if(t[z] == s)
          return true;
    
        return false;
    }
    
      handlePlaceChanged(){      
    const place = this.autocomplete.getPlace();
    //console.log(" from google auto complete place===>",place);
    let lat, lng, addrSel, placeName, placeId =''; 
    let country, state, city =null;  
    if(place.geometry!==undefined ){
      const plcGeom = place.geometry;
      if(plcGeom.location!==undefined){
        lat = plcGeom.location ? plcGeom.location.lat():'';
        lng = plcGeom.location ? plcGeom.location.lng():'';
      }      
    }   
    
    addrSel  = place.formatted_address!==undefined?place.formatted_address:"";
    placeName  = place.name!==undefined?place.name:"";
    placeId = place.place_id!==undefined?place.place_id:"";
    
    if(place.address_components!==undefined){
      let addrComp = place.address_components;
      for(let i = 0; i < addrComp.length; ++i)
      {
        var typ = addrComp[i].types;
        if(this.compIsType(typ, 'administrative_area_level_1'))
            state = addrComp[i].long_name; //store the state
        else if(this.compIsType(typ, 'locality'))
            city = addrComp[i].long_name; //store the city
        else if(this.compIsType(typ, 'country'))
          country = addrComp[i].long_name; //store the country        
    
        //we can break early if we find all three data
        if(state != null && city != null && country != null) break;
      }
      
      
    
    
    }
    
    let nameData = "";
      if(this.props.showKey!==undefined ){
    
        
        if(this.props.showKey=="CITY"){
          nameData = city;
        }
        else if(this.props.showKey=="STATE"){
          nameData = state;
        }
        else if(this.props.showKey=="COUNTRY"){
          nameData = country;
        }
         else if(this.props.showKey=="PLACE_NAME"){
          nameData = country;
        }
        else if(this.props.showKey=="FORMATTED_ADDRESS"){
          nameData = addrSel;
        }
         else if(this.props.showKey=="PLACE_ID"){
          nameData = placeId;
        }
        else{
          nameData = addrSel;
        }
       
      }else{
        
        nameData = addrSel;
      }
    
      
    
      if(this.props.removeTextFlag!==undefined && this.props.removeTextFlag=="YES" ){
        nameData = "";
      }
    
    let stateResp = {
      'lat':lat,
      'lng': lng,
      'formattedAddress':addrSel, 
      'placeName': placeName,
      'placeId': placeId,
      'city':city,
      'state':state,
      'country':country,
      'textboxtext':nameData
    };
    this.setState(stateResp,()=>{
     this.props.selectedGoogleDataInfo!==undefined && this.props.selectedGoogleDataInfo(stateResp);    
    });
    //console.log(place);
    //console.log("==lat==>", lat," ==lng===>",lng, "==addrSel==>",addrSel);
    //this.props.onPlaceLoaded(place);
       }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-08
      • 1970-01-01
      • 2012-01-07
      • 2019-05-27
      • 2013-11-13
      • 1970-01-01
      相关资源
      最近更新 更多