pub fn address_to_ll(query: &str, conn: &PgConnection) -> Result<LatLong> {
    use crate::schema::geoloc_cache::{self, dsl};

    // coba ambil dulu dari cache apabila ada
    if let Ok(geoloc) = geoloc_cache::table
        .filter(lower(dsl::name).eq(query.to_lowercase()))
        .select((dsl::latitude, dsl::longitude))
        .first::<(f64, f64)>(conn)
    {
        return Ok(LatLong {
            latitude: geoloc.0,
            longitude: geoloc.1,
        });
    }

    // tidak ada di cache, ambil dari source luar
    let query = normalize_query(query.to_lowercase());

    let (country, province, city) = {
        let mut s: Vec<&str> = query.split('/').collect();
        s = s.into_iter().filter(|a| a.trim().len() > 0).collect();
        match &s[0..] {
            &[a] => (a, "", ""),
            &[a, b] => (a, b, ""),
            &[a, b, c] => (a, b, c),
            _ => ("", "", ""),
        }
    };

    let url_query = format!(
        "https://geocoder.ls.hereapi.com/6.2/geocode.json?apiKey={}&country={}&county={}&city={}",
        env::var("GEOLOCATOR_API_KEY").expect("GEOLOCATOR_API_KEY env not set"),
        country.trim(),
        province.trim(),
        city.trim()
    );
    let mut resp = reqwest::get(&url_query)?;
    let resp_text = resp.text()?;
    let item: GeocoderResponseWrapper = serde_json::from_str(&resp_text)?;
    if item.response.view.is_empty() || item.response.view[0].result.is_empty() {
        error!("in getting geo locator data {:?}", item);
        error!("url_query: {}", url_query);
        return Err(Error::NotFound("geo locator data not found".to_string()));
    }
    let latlong = item.response.view[0].result[0].location.display_position;

    // simpan dalam cache
    {
        if let Err(e) = diesel::insert_into(geoloc_cache::table)
            .values(&NewGeolocCache {
                name: &query,
                latitude: latlong.latitude,
                longitude: latlong.longitude,
            })
            .execute(conn)
        {
            error!("Cannot insert new cache for location query {}", query);
        }
    }

    Ok(latlong)
}
/// Get latitude longitude from query location name like city etc.
Get latitude longitude from query location name like city etc.
Get latitude longitude from query location name like city etc .
pub fn address_to_ll(query: &str, conn: &PgConnection) -> Result<LatLong> {
    use crate::schema::geoloc_cache::{self, dsl};
    // coba ambil dulu dari cache apabila ada
    if let Ok(geoloc) = geoloc_cache::table
        .filter(lower(dsl::name).eq(query.to_lowercase()))
        .select((dsl::latitude, dsl::longitude))
        .first::<(f64, f64)>(conn)
    {
        return Ok(LatLong {
            latitude: geoloc.0,
            longitude: geoloc.1,
        });
    }
    // tidak ada di cache, ambil dari source luar
    let query = normalize_query(query.to_lowercase());
    let (country, province, city) = {
        let mut s: Vec<&str> = query.split('/').collect();
        s = s.into_iter().filter(|a| a.trim().len() > 0).collect();
        match &s[0..] {
            &[a] => (a, "", ""),
            &[a, b] => (a, b, ""),
            &[a, b, c] => (a, b, c),
            _ => ("", "", ""),
        }
    };
    let url_query = format!(
        "https://geocoder.ls.hereapi.com/6.2/geocode.json?apiKey={}&country={}&county={}&city={}",
        env::var("GEOLOCATOR_API_KEY").expect("GEOLOCATOR_API_KEY env not set"),
        country.trim(),
        province.trim(),
        city.trim()
    );
    let mut resp = reqwest::get(&url_query)?;
    let resp_text = resp.text()?;
    let item: GeocoderResponseWrapper = serde_json::from_str(&resp_text)?;
    if item.response.view.is_empty() || item.response.view[0].result.is_empty() {
        error!("in getting geo locator data {:?}", item);
        error!("url_query: {}", url_query);
        return Err(Error::NotFound("geo locator data not found".to_string()));
    }
    let latlong = item.response.view[0].result[0].location.display_position;
    // simpan dalam cache
    {
        if let Err(e) = diesel::insert_into(geoloc_cache::table)
            .values(&NewGeolocCache {
                name: &query,
                latitude: latlong.latitude,
                longitude: latlong.longitude,
            })
            .execute(conn)
        {
            error!("Cannot insert new cache for location query {}", query);
        }
    }
    Ok(latlong)
}
pub fn address_to_ll ( query : & str , conn : & PgConnection ) -> Result < LatLong > { use crate :: schema :: geoloc_cache :: { self , dsl } ; // coba ambil dulu dari cache apabila ada if let Ok ( geoloc ) = geoloc_cache :: table . filter ( lower ( dsl :: name ) . eq ( query . to_lowercase ( ) ) ) . select ( ( dsl :: latitude , dsl :: longitude ) ) . first :: < ( f64 , f64 ) > ( conn ) { return Ok ( LatLong { latitude : geoloc . 0 , longitude : geoloc . 1 , } ) ; } // tidak ada di cache, ambil dari source luar let query = normalize_query ( query . to_lowercase ( ) ) ; let ( country , province , city ) = { let mut s : Vec < & str > = query . split ( '/' ) . collect ( ) ; s = s . into_iter ( ) . filter ( | a | a . trim ( ) . len ( ) > 0 ) . collect ( ) ; match & s [ 0 .. ] { & [ a ] => ( a , " " , " " ) , & [ a , b ] => ( a , b , " " ) , & [ a , b , c ] => ( a , b , c ) , _ => ( " " , " " , " " ) , } } ; let url_query = format ! ( " " env var ( " " ) expect ( " " ) country trim ( ) province trim ( ) city trim ( ) ) ; let mut resp = reqwest :: get ( & url_query ) ? ; let resp_text = resp . text ( ) ? ; let item : GeocoderResponseWrapper = serde_json :: from_str ( & resp_text ) ? ; if item . response . view . is_empty ( ) || item . response . view [ 0 ] . result . is_empty ( ) { error ! ( " " item ) ; error ! ( " " url_query ) ; return Err ( Error :: NotFound ( " " . to_string ( ) ) ) ; } let latlong = item . response . view [ 0 ] . result [ 0 ] . location . display_position ; // simpan dalam cache { if let Err ( e ) = diesel :: insert_into ( geoloc_cache :: table ) . values ( & NewGeolocCache { name : & query , latitude : latlong . latitude , longitude : latlong . longitude , } ) . execute ( conn ) { error ! ( " " query ) ; } } Ok ( latlong ) }
Get latitude longitude from query location name like city etc.
Get latitude longitude from query location name like city etc .
query
&str
conn
&PgConnection
query
&str
conn
&PgConnection