2013-01-16 4 views
14

का उपयोग करके अक्षांश और देशांतर कैसे प्राप्त करें मेरे पास सी # में डब्ल्यूसीएफ सेवा है। सेवा कॉल क्लाइंट में शहर का नाम भेजता है। मैं जनसांख्यिकीय के तहत डेटाबेस में अक्षांश और अक्षांश और स्टोर में शहर का नाम बदलना चाहता हूं। मैं उपरोक्त कार्यक्षमता को लागू करने के लिए Google API का उपयोग करने की योजना बना रहा हूं। मैंने Google और उसके प्रकार के 'सेवा खाते' से एक एपीआई कुंजी प्राप्त की है। SO अब मैं एपीआई का उपयोग करके अक्षांश और देशांतर कैसे प्राप्त कर सकता हूं? क्या मुझे कुछ एसडीके स्थापित करने की आवश्यकता है या कोई आरईएसटी सेवा करेगा?सी # - सी #

उत्तर

16

आप गूगल मैप्स एपीआई का उपयोग करने के लिए अपने बाकी एपीआई पर एक नजर है चाहते हैं, आप जैसे

http://maps.googleapis.com/maps/api/geocode/xml?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=true_or_false 

एक अनुरोध भेजने के एक गूगल मैप्स एपीआई स्थापित करने की आवश्यकता नहीं है और आप एक प्रतिक्रिया मिल जाएगा एक्सएमएल।

अधिक जानकारी के लिए NuGet पैकेज GoogleMaps.LocationServices, या उसके source code का सिर्फ स्पिन की कोशिश कर सकते

https://developers.google.com/maps/documentation/geocoding/index#GeocodingRequests

+0

आप कर सकते हैं एक्सएमएल का विश्लेषण कैसे करें? क्या सिरदर्द है! – TheOptimusPrimus

+1

यदि आप JSON चाहते हैं तो आप http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway ,+Mountain+View,+CA&sensor=false – hriziya

+0

का उपयोग कर सकते हैं अद्भुत और बहुत उपयोगी –

47

पर एक नजर है। यह किसी दिए गए पते के लिए लेट/लम्बाई पाने के लिए Google की REST API का उपयोग करता है और इसके विपरीत, एपीआई कुंजी की आवश्यकता के बिना।

आप इस तरह इसका इस्तेमाल:

public static void Main() 
{ 
    var address = "Stavanger, Norway"; 

    var locationService = new GoogleLocationService(); 
    var point = locationService.GetLatLongFromAddress(address); 

    var latitude = point.Latitude; 
    var longitude = point.Longitude; 

    // Save lat/long values to DB... 
} 
+0

क्या इस पर कोई सीमा है? पहले जवाब में Google ने स्पष्ट रूप से कहा कि Google नक्शे को छोड़कर कानूनी रूप से उपयोग नहीं किया जा सकता है और अनुरोधों की संख्या पर कुछ निर्दिष्ट सीमा है। – MaxRecursion

+1

उपर्युक्त उत्तर के समान सीमा इस पर लागू होती है। यह आरईएसटी एपीआई के आसपास केवल एक सी # रैपर है। [स्रोत कोड] देखें (https://github.com/sethwebster/GoogleMaps.LocationServices/blob/master/GoogleMaps.LocationServices/GoogleLocationService.cs#L14) – khellang

+1

इससे मुझे एक्सएमएल पार्सिंग के घंटे बचाए। +1, यह एक सुरुचिपूर्ण समाधान है !! – TheOptimusPrimus

6

आप विशेष यूआरएल में पता पारित कर सकते हैं .. और आप वापसी मान डीटी में अक्षांश और देशांतर (datatable) प्राप्त

string url = "http://maps.google.com/maps/api/geocode/xml?address=" + address+ "&sensor=false"; 
WebRequest request = WebRequest.Create(url); 

using (WebResponse response = (HttpWebResponse)request.GetResponse()) 
{ 
    using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8)) 
    { 
     DataSet dsResult = new DataSet(); 
     dsResult.ReadXml(reader); 
     DataTable dtCoordinates = new DataTable(); 
     dtCoordinates.Columns.AddRange(new DataColumn[4] { new DataColumn("Id", typeof(int)), 
        new DataColumn("Address", typeof(string)), 
        new DataColumn("Latitude",typeof(string)), 
        new DataColumn("Longitude",typeof(string)) }); 
     foreach (DataRow row in dsResult.Tables["result"].Rows) 
     { 
      string geometry_id = dsResult.Tables["geometry"].Select("result_id = " + row["result_id"].ToString())[0]["geometry_id"].ToString(); 
      DataRow location = dsResult.Tables["location"].Select("geometry_id = " + geometry_id)[0]; 
      dtCoordinates.Rows.Add(row["result_id"], row["formatted_address"], location["lat"], location["lng"]); 
     } 
    } 
    return dtCoordinates; 
} 
+0

यह मुझे ROOFTOP की आवश्यकता है, लेकिन dsresult में location_type नहीं मिला? – LuckyS

4
 /*Ready to use code : simple copy paste GetLatLong*/ 
    public class AddressComponent 
    { 
     public string long_name { get; set; } 
     public string short_name { get; set; } 
     public List<string> types { get; set; } 
    } 

    public class Northeast 
    { 
     public double lat { get; set; } 
     public double lng { get; set; } 
    } 

    public class Southwest 
    { 
     public double lat { get; set; } 
     public double lng { get; set; } 
    } 

    public class Bounds 
    { 
     public Northeast northeast { get; set; } 
     public Southwest southwest { get; set; } 
    } 

    public class Location 
    { 
     public double lat { get; set; } 
     public double lng { get; set; } 
    } 

    public class Northeast2 
    { 
     public double lat { get; set; } 
     public double lng { get; set; } 
    } 

    public class Southwest2 
    { 
     public double lat { get; set; } 
     public double lng { get; set; } 
    } 

    public class Viewport 
    { 
     public Northeast2 northeast { get; set; } 
     public Southwest2 southwest { get; set; } 
    } 

    public class Geometry 
    { 
     public Bounds bounds { get; set; } 
     public Location location { get; set; } 
     public string location_type { get; set; } 
     public Viewport viewport { get; set; } 
    } 

    public class Result 
    { 
     public List<AddressComponent> address_components { get; set; } 
     public string formatted_address { get; set; } 
     public Geometry geometry { get; set; } 
     public string place_id { get; set; } 
     public List<string> types { get; set; } 
    } 

    public class RootObject 
    { 
     public List<Result> results { get; set; } 
     public string status { get; set; } 
    } 


    public static RootObject GetLatLongByAddress(string address) 
    { 
     var root = new RootObject(); 

     var url = 
      string.Format(
       "http://maps.googleapis.com/maps/api/geocode/json?address={0}&sensor=true_or_false", address); 
     var req = (HttpWebRequest)WebRequest.Create(url); 

     var res = (HttpWebResponse)req.GetResponse(); 

     using (var streamreader=new StreamReader(res.GetResponseStream())) 
     { 
      var result = streamreader.ReadToEnd(); 

      if (!string.IsNullOrWhiteSpace(result)) 
      { 
       root = JsonConvert.DeserializeObject<RootObject>(result); 
      } 
     } 
     return root; 


    } 


      /* Call This*/ 

var destination_latLong = GetLatLongByAddress(um.RouteDestination); 

var lattitude =Convert.ToString(destination_latLong.results[0].geometry.location.lat, CultureInfo.InvariantCulture); 

var longitude=Convert.ToString(destination_latLong.results[0].geometry.location.lng, CultureInfo.InvariantCulture); 
+0

मेरे मामले में, Google मानचित्र पते के लिए परिणाम देता है, और यह विधि अक्षांश/लंबे परिणाम के लिए शून्य मान देती है, समस्या क्या हो सकती है? –

+0

cos 'यह विधि lat/long –

+0

के साथ काम नहीं करेगी इस एपीआई को रिवर्स जियोकोडिंग के लिए उपयोग करें .: http://maps.google.com/maps/api/geocode/xml?latlng=40.714224,-73.961452&sensor=false –