static void Main(string[] args) { string apiKey = "648173182770.apps.googleusercontent.com"; IGeoCoder geoCoder = new GoogleGeoCoder(); IEnumerable<Address> addresses = geoCoder.GeoCode("26 Bluff St, Salem, NH 03079"); foreach (var address in addresses) { Debug.Print(address.Coordinates.Latitude.ToString()); Debug.Print(address.Coordinates.Longitude.ToString()); } }
private static void SetLalngByAddress(ImportExportFields writedata, PolinatorInformation polinatorInformation) { string physicalLocation; GoogleGeoCoder geoCoder = new GoogleGeoCoder(); bool getLocation = false; Address[] matchAddress = null; int numTry = 0; while (!getLocation && numTry < 5) { try { numTry++; physicalLocation = writedata.LandscapeStreet + " " + writedata.LandscapeZipcode + " " + writedata.LandscapeCity + " " + writedata.LandscapeState + " " + writedata.LandscapeCountry; matchAddress = geoCoder.GeoCode(physicalLocation).ToArray(); getLocation = true; break; } catch (Exception ex) { System.Threading.Thread.Sleep(2000); } } if (matchAddress != null && matchAddress.Length > 0) { polinatorInformation.Latitude = (decimal)matchAddress[0].Coordinates.Latitude; polinatorInformation.Longitude = (decimal)matchAddress[0].Coordinates.Longitude; if (matchAddress.Length > 1) polinatorInformation.FuzyLocation = true; } }
public ActionResult GeoCodeTest(string address) { IGeoCoder coder = new GoogleGeoCoder(); var res = coder.GeoCode(address); return Json(res, JsonRequestBehavior.AllowGet); }
private List<ImportFields> ParseDemoInfo(string filePath, string sDirPath) { string content = File.ReadAllText(filePath); File.Delete(filePath); // Parse content string[] stringSeparators = new string[] { "\r\n" }; string[] lines = content.Split(stringSeparators, StringSplitOptions.RemoveEmptyEntries); int numRecord = lines.Length; List<ImportFields> listData = new List<ImportFields>(); List<ImportFields> notConvertList = new List<ImportFields>(); ImportFields data; IGeoCoder geoCoder = new GoogleGeoCoder(); string nameList = "Tom, Marry, Adinath, Ajitesh, Akshaj, Akshobhya, Ameyatma, Badri, Badrinath, Bhudhav, Chakradev, Chhatrabhuj, Eashan, Eha, Eka, Ekana, Evyavan, Harinarayan, Hemang, Ijay, Indivar, Ish, Jaipal, Jaithra, Kamalakar, Kamalkant, Kamalnath, Lakshmidhar, Lakshmigopal, Lakshmiraman, Liladhar, Lohitaksh, Lohitaksha, Loknath, Lokranjan, Madhuban, Mahakram, Mahatru, Namish, Narahari, Nityanta, Padmanabha, Padmapati, Padmesh, Parmesh, Phanindranath, Pramodan, Rakshan, Ramakaant, Ramashray, Ranganath, Ratannabha, Ratnabhu, Ratnanidhi, Sadabindu, Sadru, Sahishnu, Samarendra, Samarendu, Samarjit, Samavart, Samendu, Saprathas, Satanand, Satkartar, Satveer,Tommy, Denny, Herry, Nate, Yathavan, David, Aadinath, Aaditeya, Zacharry, Aamod, Zuhayr"; string[] firstnames = nameList.Split(new char[] { '\n', ',' }); string orgNameList = "Coca, Pollinator, Friendly Farmers, Microsoft, Hobbyist, Beekeeper, Gardener"; string[] orgNames = orgNameList.Split(new char[] { '\n', ',' }); int numFirstName = firstnames.Length; string physicalLocation; Random random = new Random(); for (int i = 1; i < numRecord; i++) { data = new ImportFields(); string[] values = ImportExportUltility.GetCsvRecord(lines[i]); if (values.Length > 2) { physicalLocation = values[1].Replace("See map: Google Maps ", ""); string[] sAddress = physicalLocation.Split(new char[] { '\n', ',' }, StringSplitOptions.RemoveEmptyEntries); int len = sAddress.Length; int startDataIndex = 0; while (string.IsNullOrWhiteSpace(sAddress[startDataIndex]) && startDataIndex < len) { startDataIndex++; } if (startDataIndex<len) data.LandscapeStreet = sAddress[startDataIndex].Trim(); if (startDataIndex+1 < len) data.LandscapeZipcode = sAddress[startDataIndex + 1].Trim(); if (startDataIndex +2< len) data.LandscapeCity = sAddress[startDataIndex + 2].Trim(); data.LandscapeState = values[2].Trim(); data.Premium = 1; data.PollinatorSize = random.Next(1, 8); data.PollinatorType = random.Next(1, 9); data.FirstName = firstnames[random.Next(1, numFirstName - 1)]; data.OrganizationName = orgNames[random.Next(1, 8)]; data.PhotoUrl = "";//UploadFiles/458586204/Burts Bees logo.png"; if (IS_GET_LOCATION) { //Get location from address physicalLocation = physicalLocation.Replace("\n", "").Trim(); geoCoder = new GoogleGeoCoder(); bool getLocation = false; Address[] matchAddress = null; int numTry = 0; while (!getLocation && numTry < 5) { try { matchAddress = geoCoder.GeoCode(physicalLocation).ToArray(); getLocation = true; numTry++; break; } catch (Exception ex) { System.Threading.Thread.Sleep(2000); // Pollinator.Common.Logger.Error("Error occured at " + typeof(Admin_DataMigration).Name + "Get location from address. Exception:", ex); } } if (matchAddress != null && matchAddress.Length > 0) { data.Latitude = matchAddress[0].Coordinates.Latitude; data.Longitude = matchAddress[0].Coordinates.Longitude; listData.Add(data); } else { data.LineNumber = (i + 1).ToString(); data.PhysicalLocation = physicalLocation; notConvertList.Add(data); } } else { if (i != 29 && i != 53) { listData.Add(data); } } } } WriteCsvFile(listData, sDirPath, @"\output.csv"); if (IS_GET_LOCATION) { //write file to check error line (can't get location) var csv = new StringBuilder(); string newLine = "LineNumber,PhysicalLocation,OrganizationName,PollinatorSize,PollinatorType, LandscapeCity,LandscapeState,LandscapeZipcode," + Environment.NewLine; csv.Append(newLine); for (int i = 0; i < notConvertList.Count; i++) { ImportFields writedata = notConvertList.ElementAt(i); newLine = string.Format("{0}, \"{1}\",\"{2}\",\"{3}\",\"{4}\",\"{5}\",\"{6}\",\"{7}\",{8}", writedata.LineNumber, writedata.PhysicalLocation, writedata.OrganizationName, writedata.PollinatorSize, writedata.PollinatorType, writedata.LandscapeCity, writedata.LandscapeState, writedata.LandscapeZipcode, Environment.NewLine); csv.Append(newLine); } File.WriteAllText(sDirPath + @"\output_error.csv", csv.ToString()); } return listData; }
private static List<ImportFields> GetLocationForOldData(List<ImportFields> listdata) { int numRecord = listdata.Count; List<ImportFields> outputList = new List<ImportFields>(); GoogleGeoCoder geoCoder; ImportFields writedata; string physicalLocation; for (int i = 0; i < numRecord; i++) { writedata = listdata.ElementAt(i); if (!writedata.Longitude.HasValue) { geoCoder = new GoogleGeoCoder(); bool getLocation = false; Address[] matchAddress = null; int numTry = 0; while (!getLocation && numTry < 5) { try { numTry++; physicalLocation = writedata.LandscapeStreet + " " + writedata.LandscapeZipcode + " " + writedata.LandscapeCity + " " + writedata.LandscapeState + " " + writedata.LandscapeCountry; matchAddress = geoCoder.GeoCode(physicalLocation).ToArray(); getLocation = true; break; } catch (Exception ex) { System.Threading.Thread.Sleep(2000); // Pollinator.Common.Logger.Error("Error occured at " + typeof(Admin_ImportData).Name + "Get location from address. Exception:", ex); } } if (matchAddress != null && matchAddress.Length > 0) { writedata.Latitude = matchAddress[0].Coordinates.Latitude; writedata.Longitude = matchAddress[0].Coordinates.Longitude; if (matchAddress.Length > 1) writedata.FuzyLocation = true; } } outputList.Add(writedata); } return outputList; }
private void CreateMarker(string title, CityStateZip csz, string address) { this.Title = title; IGeoCoder geocoder = new GoogleGeoCoder(); this.GeocodingOutput = geocoder.GeoCode(address, csz.City, csz.State, csz.ZipCode, "").ToList(); }