Search code examples
javascriptreactjsyoutube-apigeocode

Extract address portion of strings and geocode the address using javascript or react


I've fetched some youtube video descriptions. The descriptions has address on them that looks like this :

- - - - - - - - - - - - - - - - -  
1. TRADITIONAL CAO LAU NOODLE FACTORY 

🍜CAO LAU NOODLES: Soak the rice for 24 hours then grind it. Cook and stir until thick for 30 minutes.
Steam for 30 minutes. Roll to make it flat and steam. 
- - - - - - - - - - - - - - - - -
2. Quán Cao Lầu Bá Lễ
ADDRESS: 49/3 Trần Hưng Đạo, Sơn Phong, Hội An, Quảng Nam 56000
OPENING TIME: 11AM - 8:30PM 

🍜CAO LAU: In a bowl, add herbs, bean sprouts, cao lau noodles, char siu, chả ram, deep fried pork fat (tốp mỡ) then meat sauce.

PRICE: 35.000 VND / $1.51 USD 
- - - - - - - - - - - - - - - - -
3. PHỞ SẮN FACTORY

🍜 PHỞ SẮN: Grind the dried cassava into powder. Soak the cassava powder and water for 3 days and 3 nights then filter the water out, keeping the cassava powder. Whisk the cassava mixture in a pot for 30 minutes. Smooth mixture out. Place batter in a pressing machine. Dry noodles in the sun. After drying, cut into squares. 
- - - - - - - - - - - - - - - - -
4. NHÀ HÀNG CỎ HỒNG

🍜FRIED PHỞ SẮN: Pestle the pearl onions and place vegetables in hot water. Make the sauce with fish sauce, vinegar, msg, and coconut nectar. In a pan, add peanut oil. Fry the garlic, shrimp, and squid, add sugar, and then the vegetables. Add water and add a little starch. In another pan, deep-fry the pho san noodles. In a bowl, add the fried pho san, sauce, shrimp, squid, and top with spring onions.

🍜PHỞ SẮN WITH FISH BROTH: Cook snakehead fish broth for 5 hours. Break the pho san into pieces and soak them in cold water for one minute. Place oil in a pot, add in pearl onions, garlic, tumeric, and the snakehead fish, and fry it for 10 minutes. Add in the pork. Pestle the fish. Add in the broth and cook
- - - - - - - - - - - - - - - - -
5. MÌ QUẢNG BÀ LÁNG
ADDRESS: Lê Thánh Tông street, An Phú ward, Tam Kỳ city, Quảng Nam Province

🍜MÌ QUẢNG NOODLES: Soak the noodles and grind with a machine. Spread mixture on a flat surface and steam. When it’s cool, fold and cut it. 

🍜MÌ QUẢNG: Add fresh mì quảng in the bowl. Add char siu meat sauce. Add char siu and shrimp. Top with the peanuts and spring onions. Add herbs and fresh vegetables. 

Now, how do I select the address and geocode it to return x, y coordinate?


Solution

    1. You can get the address as follows:
    const address = str.split('ADDRESS:')[1].split("\n")
    
    console.log(address[0]) //  49/3 Trần Hưng Đạo, Sơn Phong, Hội An, Quảng Nam 56000
    
    
    1. for getting the latitude, longitude of the address you will need to use a geolocating API such as Geoapify which can be used as follows:
    import { API_KEY } from "./config";
    
     getLocationFromName = async function (countryName) {
        try {
          console.log(`fetching info for ${countryName}`);
          const countryData = await getJsonFromFetch(countryName);
          const latlng = [countryData.results[0].lat, countryData.results[0].lon];
          return latlng;
        } catch (e) {
          console.error(e);
          throw new Error(
            `failed to get location for ${countryName}, ${e.message}`
          );
        }
      };
    
    export const getJsonFromFetch = async function (name) {
      const url = `https://api.geoapify.com/v1/geocode/search?text=${name}&format=json&apiKey=${API_KEY}`;
      console.log(url);
      try {
        const response = await fetch(url);
        return await response.json();
      } catch (e) {
        throw e;
      }
    };