0

私はこのプロジェクトを介してGoogleのリバースジオコーディングAPIをヒットしたときにこのjsonオブジェクトを取得しました...しかし、問題は "結果"配列の下にある "form_address"しかし、それを行う方法を知らない。親切に私を助けてください。私はプログラミングで新鮮です。今どのように私はこのコントローラコードからのビューで地図を表示します..私はリストオブジェクト "販売"で緯度と経度を持って..また、私は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" 
    } 

私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); 

答えて

0

あなたは "結果" 全ページ上のアレイ

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>

+0

がクリックより良い結果を見ることのresults[0].formatted_addressによってformatted_addressを得ることができますあなたはスニペットを実行しています –

+0

t hanx alot bro。 –

+0

bt私はコントローラを介して逆ジオメトリAPIを呼び出しています。そして、ビュー内にマップを表示しなければなりません。 –

関連する問題