反向地理编码(通过经纬度生成地址)

Reverse Geocoding (generating address through latitude and longitude)

当我通过我的项目点击 google 反向地理编码 api 时,我得到了这个 json 对象..但问题是我只需要 "form_address" 部分在 "results" 数组下,但不知道该怎么做 that.kindly 帮助我。我是编程新手..现在我将如何从这个控制器代码中显示地图..我在列表对象 "sale" 中有纬度和经度..还有我从 [=23= 得到的位置] 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"
   }

我的controllercode.cs

public ActionResult Index()
    {
        List<salepros> sale = new List<salepros>();           
        string sqlquery = "select prospectid,latitude, longitude, location from sale_prospectdet where createdby = {0} order by createdon desc";
        sqlquery = string.Format(sqlquery, "*");
        DataTable dt = DbHelper.ExecuteDataset(Globals.GetConnectionString(), CommandType.Text, sqlquery).Tables[0];

        for (int i = 0; i < 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}' WHERE `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);

你可以通过 "results" 数组的 results[0].formatted_address 得到 formatted_address

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>