Geocoding with Russian keywords

Aug 5, 2010 at 2:06 PM
Hi, I try to the followed code: MainMap.Manager.Language = LanguageType.Russian; var ll = MainMap.Manager.GetLatLngFromGeocoder(keyws, out dd); Where keyws - some keywords in Russian language. I got dd = G_GEO_UNKNOWN_ADDRESS. Is it possible to search on Russian keywords? Thanks Alex
Aug 5, 2010 at 3:33 PM

i don't know, hm.. does maps.google.com work with Russian keywords?

Aug 8, 2010 at 7:14 AM
Why no? On http://maps.google.com/maps?hl=ru&tab=wl I can search with russian keywords.
Aug 9, 2010 at 8:47 AM

i see, then need i'll need to make some investigation ;}

Aug 10, 2010 at 6:57 AM
I think in internal string MakeGeocoderUrl(string keywords) { string key = keywords.Replace(' ', '+'); return string.Format("http://maps.google.com/maps/geo?q={0}&output=csv&key={1}", key, GoogleMapsAPIKey); } language parameter missing hl={0}. Must be: return string.Format("http://maps.google.com/maps/geo?hl={0}&q={1}&output=csv&key={1}", LanguageStr, key, GoogleMapsAPIKey); How do you think?
Aug 10, 2010 at 7:02 AM

is it working?

Aug 11, 2010 at 8:10 AM
I changed the function and now search by keyword works in different languages. internal string MakeGeocoderUrl(string keywords) { string key = keywords.Replace(' ', '+'); return string.Format("http://maps.google.com/maps/geo?q={0}&hl={1}&output=csv&key={2}", key, LanguageStr, GoogleMapsAPIKey); } But I have other problem. Geocoding function does not work for all countries. For example, for Israel this function does not work. This is written in the documentation Google map API. Nevertheless, the following query works fine in browser even with keywords in Hebrew: http://maps.google.com/maps?q=Stengade,2200,Denmark&hl=iw Is there a library function that uses only search for keywords without geocoding?
Aug 11, 2010 at 9:23 AM

seems maps.google.com use search in more advanced way somehow, i don't know for such a library,

but thanks for fixed code, i'l commit it soon

Aug 11, 2010 at 1:23 PM
OK. I added the following function to gmapcontrol.cs /// <summary> /// set current position using keywords without Geocoding /// </summary> /// <param name="keys"></param> /// <returns>true if successfull</returns> public GeoCoderStatusCode SetCurrentPositionByKeywords2(string keys) { GeoCoderStatusCode status = GeoCoderStatusCode.Unknow; PointLatLng? pos = Manager.GetLatLngFromAddress(keys, out status); if (pos.HasValue && status == GeoCoderStatusCode.G_GEO_SUCCESS) { CurrentPosition = pos.Value; } return status; } And to following functions to gmap.cs /// <summary> /// gets lat, lng from address keys /// </summary> /// <param name="keywords"></param> /// <param name="status"></param> /// <returns></returns> public PointLatLng? GetLatLngFromAddress(string keywords, out GeoCoderStatusCode status) { return GetLatLngFromAddressUrl(MakeAddressUrl(keywords), UseGeocoderCache, out status); } /// <summary> /// makes url for address /// </summary> /// <param name="keywords"></param> /// <returns></returns> internal string MakeAddressUrl(string keywords) { string key = keywords.Replace(' ', '+'); return string.Format("http://maps.google.com/maps?q={0}&hl={1}", key, LanguageStr); } /// <summary> /// gets lat and lng from address url /// </summary> /// <param name="url"></param> /// <param name="useCache"></param> /// <param name="status"></param> /// <returns></returns> internal PointLatLng? GetLatLngFromAddressUrl(string url, bool useCache, out GeoCoderStatusCode status) { status = GeoCoderStatusCode.Unknow; PointLatLng? ret = null; try { string urlEnd = url.Substring(url.IndexOf("maps?q=")); #if !PocketPC char[] ilg = Path.GetInvalidFileNameChars(); #else char[] ilg = new char[41]; for(int i = 0; i < 32; i++) ilg[i] = (char) i; ilg[32] = '"'; ilg[33] = '<'; ilg[34] = '>'; ilg[35] = '|'; ilg[36] = '?'; ilg[37] = ':'; ilg[38] = '/'; ilg[39] = '\\'; ilg[39] = '*'; #endif foreach (char c in ilg) { urlEnd = urlEnd.Replace(c, '_'); } string addr = useCache ? Cache.Instance.GetGeocoderFromCache(urlEnd) : string.Empty; if (string.IsNullOrEmpty(addr)) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); if (Proxy != null) { request.Proxy = Proxy; #if !PocketPC request.PreAuthenticate = true; #endif } else { #if !PocketPC request.Proxy = WebRequest.DefaultWebProxy; #else request.Proxy = GlobalProxySelection.GetEmptyWebProxy(); #endif } request.UserAgent = UserAgent; request.Timeout = Timeout; request.ReadWriteTimeout = Timeout * 6; request.KeepAlive = false; using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) { using (Stream responseStream = response.GetResponseStream()) { using (StreamReader read = new StreamReader(responseStream)) { addr = read.ReadToEnd(); } } } } // parse values if (addr != String.Empty) { var lt_ln_s = addr.IndexOf("latlng:{"); if (lt_ln_s != -1) { var lt_ln_e = addr.IndexOf("}", lt_ln_s); if (lt_ln_e != -1) { try { var ltlns = addr.Substring(lt_ln_s, lt_ln_e - lt_ln_s); var lts = ltlns.Substring(12, 18); var ltn = ltlns.Substring(35, 18); double lat = double.Parse(lts, CultureInfo.InvariantCulture); double lng = double.Parse(ltn, CultureInfo.InvariantCulture); status = GeoCoderStatusCode.G_GEO_SUCCESS; ret = new PointLatLng(lat, lng); } catch (Exception) { throw; } } } } } catch (Exception ex) { ret = null; Debug.WriteLine("GetLatLngFromGeocoderUrl: " + ex.ToString()); } return ret; } Now on calling function SetCurrentPositionByKeywords2 you can search without geocoding. Only problem is that if Google changes the answer then the function will not work. Perhaps you can find a more elegant solution of this problem.
Aug 11, 2010 at 1:27 PM

any way to format code?

Aug 11, 2010 at 1:53 PM
After copying the code looked OK. How to format code in posts?
Aug 12, 2010 at 7:27 AM
OK. I added the following function to gmapcontrol.cs /// <summary> /// set current position using keywords without Geocoding /// </summary> /// <param name="keys"></param> /// <returns>true if successfull</returns> public GeoCoderStatusCode SetCurrentPositionByKeywords2(string keys) { GeoCoderStatusCode status = GeoCoderStatusCode.Unknow; PointLatLng? pos = Manager.GetLatLngFromAddress(keys, out status); if (pos.HasValue && status == GeoCoderStatusCode.G_GEO_SUCCESS) { CurrentPosition = pos.Value; } return status; } And to following functions to gmap.cs /// <summary> /// gets lat, lng from address keys /// </summary> /// <param name="keywords"></param> /// <param name="status"></param> /// <returns></returns> public PointLatLng? GetLatLngFromAddress(string keywords, out GeoCoderStatusCode status) { return GetLatLngFromAddressUrl(MakeAddressUrl(keywords), UseGeocoderCache, out status); } /// <summary> /// makes url for address /// </summary> /// <param name="keywords"></param> /// <returns></returns> internal string MakeAddressUrl(string keywords) { string key = keywords.Replace(' ', '+'); return string.Format("http://maps.google.com/maps?q={0}&hl={1}", key, LanguageStr); } /// <summary> /// gets lat and lng from address url /// </summary> /// <param name="url"></param> /// <param name="useCache"></param> /// <param name="status"></param> /// <returns></returns> internal PointLatLng? GetLatLngFromAddressUrl(string url, bool useCache, out GeoCoderStatusCode status) { status = GeoCoderStatusCode.Unknow; PointLatLng? ret = null; try { string urlEnd = url.Substring(url.IndexOf("maps?q=")); #if !PocketPC char[] ilg = Path.GetInvalidFileNameChars(); #else char[] ilg = new char[41]; for(int i = 0; i < 32; i++) ilg[i] = (char) i; ilg[32] = '"'; ilg[33] = '<'; ilg[34] = '>'; ilg[35] = '|'; ilg[36] = '?'; ilg[37] = ':'; ilg[38] = '/'; ilg[39] = '\\'; ilg[39] = '*'; #endif foreach (char c in ilg) { urlEnd = urlEnd.Replace(c, '_'); } string addr = useCache ? Cache.Instance.GetGeocoderFromCache(urlEnd) : string.Empty; if (string.IsNullOrEmpty(addr)) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); if (Proxy != null) { request.Proxy = Proxy; #if !PocketPC request.PreAuthenticate = true; #endif } else { #if !PocketPC request.Proxy = WebRequest.DefaultWebProxy; #else request.Proxy = GlobalProxySelection.GetEmptyWebProxy(); #endif } request.UserAgent = UserAgent; request.Timeout = Timeout; request.ReadWriteTimeout = Timeout * 6; request.KeepAlive = false; using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) { using (Stream responseStream = response.GetResponseStream()) { using (StreamReader read = new StreamReader(responseStream)) { addr = read.ReadToEnd(); } } } } // parse values if (addr != String.Empty) { var lt_ln_s = addr.IndexOf("latlng:{"); if (lt_ln_s != -1) { var lt_ln_e = addr.IndexOf("}", lt_ln_s); if (lt_ln_e != -1) { try { var ltlns = addr.Substring(lt_ln_s, lt_ln_e - lt_ln_s); var lts = ltlns.Substring(12, 18); var ltn = ltlns.Substring(35, 18); double lat = double.Parse(lts, CultureInfo.InvariantCulture); double lng = double.Parse(ltn, CultureInfo.InvariantCulture); status = GeoCoderStatusCode.G_GEO_SUCCESS; ret = new PointLatLng(lat, lng); } catch (Exception) { throw; } } } } } catch (Exception ex) { ret = null; Debug.WriteLine("GetLatLngFromGeocoderUrl: " + ex.ToString()); } return ret; } Now on calling function SetCurrentPositionByKeywords2 you can search without geocoding. Only problem is that if Google changes the answer then the function will not work. Perhaps you can find a more elegant solution of this problem.
Aug 12, 2010 at 7:30 AM
On my previous post you can press 'Reply with quote' for obtaining formatted code.
Aug 12, 2010 at 7:31 AM

just click 'insert code snippet' </>

Aug 12, 2010 at 9:43 AM

I used Google Chrome browser and it does not display the panel with editiong tools.

OK. I added the following function to gmapcontrol.cs

      public GeoCoderStatusCode SetCurrentPositionByKeywords2(string keys)
      {
          GeoCoderStatusCode status = GeoCoderStatusCode.Unknow;
          PointLatLng? pos = Manager.GetLatLngFromAddress(keys, out status);
          if (pos.HasValue && status == GeoCoderStatusCode.G_GEO_SUCCESS)
          {
              CurrentPosition = pos.Value;
          }

          return status;
      }

And to following functions to gmap.cs

      public PointLatLng? GetLatLngFromAddress(string keywords, out GeoCoderStatusCode status)
      {
          return GetLatLngFromAddressUrl(MakeAddressUrl(keywords), UseGeocoderCache, out status);
      }

      internal string MakeAddressUrl(string keywords)
      {
          string key = keywords.Replace(' ', '+');
          return string.Format(&quot;http://maps.google.com/maps?q={0}&hl={1}&quot;, key, LanguageStr);
      }

      internal PointLatLng? GetLatLngFromAddressUrl(string url, bool useCache, out GeoCoderStatusCode status)
      {
          status = GeoCoderStatusCode.Unknow;
          PointLatLng? ret = null;
          try
          {
              string urlEnd = url.Substring(url.IndexOf(&quot;maps?q=&quot;));

#if !PocketPC
              char[] ilg = Path.GetInvalidFileNameChars();
#else
            char[] ilg = new char[41];
            for(int i = 0; i &lt; 32; i++)
               ilg[i] = (char) i;

            ilg[32] = '&quot;';
            ilg[33] = '&lt;';
            ilg[34] = '&gt;';
            ilg[35] = '|';
            ilg[36] = '?';
            ilg[37] = ':';
            ilg[38] = '/';
            ilg[39] = '\\';
            ilg[39] = '*';
#endif

              foreach (char c in ilg)
              {
                  urlEnd = urlEnd.Replace(c, '_');
              }

              string addr = useCache ? Cache.Instance.GetGeocoderFromCache(urlEnd) : string.Empty;

              if (string.IsNullOrEmpty(addr))
              {
                  HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                  if (Proxy != null)
                  {
                      request.Proxy = Proxy;
#if !PocketPC
                      request.PreAuthenticate = true;
#endif
                  }
                  else
                  {
#if !PocketPC
                      request.Proxy = WebRequest.DefaultWebProxy;
#else
                  request.Proxy = GlobalProxySelection.GetEmptyWebProxy();
#endif
                  }

                  request.UserAgent = UserAgent;
                  request.Timeout = Timeout;
                  request.ReadWriteTimeout = Timeout * 6;
                  request.KeepAlive = false;

                  using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
                  {
                      using (Stream responseStream = response.GetResponseStream())
                      {
                          using (StreamReader read = new StreamReader(responseStream))
                          {
                              addr = read.ReadToEnd();
                          }
                      }
                  }
              }

              // parse values
              if (addr != String.Empty)
              {
                  var lt_ln_s = addr.IndexOf(&quot;latlng:{&quot;);
                  if (lt_ln_s != -1)
                  {
                      var lt_ln_e = addr.IndexOf(&quot;}&quot;, lt_ln_s);
                      if (lt_ln_e != -1)
                      {
                          try
                          {
                              var ltlns = addr.Substring(lt_ln_s, lt_ln_e - lt_ln_s);
                              var lts = ltlns.Substring(12, 18);
                              var ltn = ltlns.Substring(35, 18);
                              double lat = double.Parse(lts, CultureInfo.InvariantCulture);
                              double lng = double.Parse(ltn, CultureInfo.InvariantCulture);
                              status = GeoCoderStatusCode.G_GEO_SUCCESS;
                              ret = new PointLatLng(lat, lng);
                          }
                          catch (Exception)
                          {
                              throw;
                          }
                      }
                  }
              }
          }
          catch (Exception ex)
          {
              ret = null;
              Debug.WriteLine(&quot;GetLatLngFromGeocoderUrl: &quot; + ex.ToString());
          }
          return ret;
      }

Now on calling function SetCurrentPositionByKeywords2 you can search without geocoding.
Only problem is that if Google changes the answer then the function will not work.
Perhaps you can find a more elegant solution of this problem.