c# - Reverse Geocoding (generating address through latitude and longitude) -
i got json object when hit google reverse geocoding api through project..but problem need "form_address" part under "results" array ,but dont know how that.kindly me. fresh in programming..now how display map in view controller code..i have latitude , longitude in list object "sale"..and have location, google api.
{ "results" : [ { "address_components" : [ { "long_name" : "a-202", "short_name" : "a-202", "types" : [ "street_number" ] }, { "long_name" : "western express highway", "short_name" : "western express hwy", "types" : [ "route" ] }, { "long_name" : "chinchpada", "short_name" : "chinchpada", "types" : [ "political", "sublocality", "sublocality_level_2" ] }, { "long_name" : "borivali east", "short_name" : "borivali east", "types" : [ "political", "sublocality", "sublocality_level_1" ] }, { "long_name" : "mumbai", "short_name" : "mumbai", "types" : [ "locality", "political" ] }, { "long_name" : "mumbai suburban", "short_name" : "mumbai suburban", "types" : [ "administrative_area_level_2", "political" ] }, { "long_name" : "maharashtra", "short_name" : "mh", "types" : [ "administrative_area_level_1", "political" ] }, { "long_name" : "india", "short_name" : "in", "types" : [ "country", "political" ] }, { "long_name" : "400066", "short_name" : "400066", "types" : [ "postal_code" ] } ], "formatted_address" : "a-202, western express hwy, chinchpada, borivali east, mumbai, maharashtra 400066, india", "geometry" : { "location" : { "lat" : 19.2186167, "lng" : 72.86424029999999 }, "location_type" : "rooftop", "viewport" : { "northeast" : { "lat" : 19.2199656802915, "lng" : 72.86558928029149 }, "southwest" : { "lat" : 19.2172677197085, "lng" : 72.86289131970848 } } }, "place_id" : "chijefow2tgw5zsrl3x0in7nu7y", "types" : [ "street_address" ] } ], "status" : "ok" }
my controllercode.cs
public actionresult index() { list<salepros> sale = new list<salepros>(); string sqlquery = "select prospectid,latitude, longitude, location sale_prospectdet createdby = {0} order createdon desc"; sqlquery = string.format(sqlquery, "*"); datatable dt = dbhelper.executedataset(globals.getconnectionstring(), commandtype.text, sqlquery).tables[0]; (int = 0; < dt.rows.count; i++) { salepros saleobject = new salepros(); string conditionloc = convert.tostring(dt.rows[i]["location"]); string conditionlat = convert.tostring(dt.rows[i]["latitude"].tostring()); string conditionlong = convert.tostring(dt.rows[i]["latitude"].tostring()); if (conditionlat == "" && conditionlong == "") { saleobject.latitude = ""; saleobject.longitude = ""; saleobject.location = "no location found"; } else if (conditionloc == "") { saleobject.latitude = convert.tostring(dt.rows[i]["latitude"].tostring()); saleobject.longitude = convert.tostring(dt.rows[i]["longitude"].tostring()); saleobject.prospectdetid = convert.toint32(dt.rows[i]["prospectdetid"]); string apiurl = string.format("https://maps.googleapis.com/maps/api/geocode/json?key=mykey&latlng=" + saleobject.latitude + "," + saleobject.longitude); string strresponse = commonfunctions.getresponse(apiurl, "get", null, null); jsonclass jsonclass = jsonconvert.deserializeobject<jsonclass>(strresponse); if (jsonclass.status == "zero_results") { saleobject.location = "no location found"; } else { saleobject.location = jsonclass.results[0].formatted_address; string locationquery = "update `sale_prospectdet` set `location`='{0}' `prospectdetid`={1}"; locationquery = string.format(locationquery, saleobject.location, saleobject.prospectdetid); int rowsaffected = dbhelper.executenonquery(globals.getconnectionstring(), commandtype.text, locationquery); } } else { saleobject.latitude = convert.tostring(dt.rows[i]["latitude"].tostring()); saleobject.longitude = convert.tostring(dt.rows[i]["longitude"].tostring()); saleobject.location = convert.tostring(dt.rows[i]["location"]); } sale.add(saleobject); } return view("location", sale);
you can formatted_address results[0].formatted_address
of "results" array
var geocoder; var map; var infowindow = new google.maps.infowindow(); var marker; function initialize() { geocoder = new google.maps.geocoder(); map = new google.maps.map(document.getelementbyid("map"), { zoom: 8, center: new google.maps.latlng(12.960752, 77.641934), maptypeid: google.maps.maptypeid.roadmap }); } function codelatlng() { if (marker && marker.setmap) { marker.setmap(null); /*clear previous marker*/ } var input = document.getelementbyid("latlng").value; var latlngstr = input.split(",", 2); var lat = parsefloat(latlngstr[0]); var lng = parsefloat(latlngstr[1]); var latlng = new google.maps.latlng(lat, lng); geocoder.geocode({ 'latlng': latlng }, function(results, status) { if (status == google.maps.geocoderstatus.ok) { if (results[0]) { map.setzoom(11); marker = new google.maps.marker({ position: latlng, map: map }); infowindow.setcontent(results[0].formatted_address); infowindow.open(map, marker); /*formatted address*/ document.getelementbyid('formatted_address').innertext = results[0].formatted_address } else { alert("no results found"); document.getelementbyid('formatted_address').innertext = "no result" } } else { alert("geocoder failed due to: " + status); } }); }
<script type="text/javascript" src="https://maps.google.com/maps/api/js"></script> <body onload="initialize()"> <div align="center" style="height: 30px; width: 430px"> <input id="latlng" type="text" value="12.960752, 77.641934"> <input type="button" value="reverse geocode" onclick="codelatlng()"> </div> <div id="map" style="height: 200px; width: 430px"></div> <p> address: </p> <p id="formatted_address"> </p>
Comments
Post a Comment