How can i make a Dynamic Map listed in Website?

≯℡__Kan透↙ 提交于 2019-12-13 04:14:59

问题


In my application, I have a store search form.

When user search for near by stores,
It generates a list of locations with a map link.

When user click on the map link, it should build a code for that specifics location.

Can any one suggest me how can i get this done.

I am trying to do soothing like this

 public ActionResult GoogleMap(string address)
    {
        StringBuilder map = new StringBuilder();
        map.Append("<h1>");
        map.Append(address);
        map.Append("</h1>");
        ViewBag.Address = map;
        return PartialView("_GoogleMap");
    }

More Details:


I don't have any working code yet for retrieving map from Google API. What i want to do is I can pass String Address of the location in to my action method and I want to Build Markup that can display the Map Block.

Working Code Link


Here is some working code I hope this will give more details of what I am trying to achieve

If i can built my string builder with all the markup that will do all.

thanks


回答1:


I got this done by my self

my Script(GoogleMap.js) is like this


var ad1 = $("#ad1").val();
var ad2 = $("#ad2").val();
var address1 = ad1;
var address2 = ad2;
//var lat = $("#lat").val();
//var lng = $("#lng").val();
var map;
var map2;
var gdir;
var gdir2;
var geocoder = null;
var geocoder2 = null;
var addressMarker;
var addressMarker2;


function initialize() {
if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("map_canvas"));
    gdir = new GDirections(map, document.getElementById("directions"));
    GEvent.addListener(gdir, "load", onGDirectionsLoad);
    GEvent.addListener(gdir, "error", handleErrors);

    setDirections(address2, address1, "en_US");
    // setDirections("San Francisco", "Mountain View", "en_US");
}
}

function setDirections(fromAddress, toAddress, locale) {
gdir.load("from: " + fromAddress + " to: " + toAddress,
            { "locale": locale });
}

My View is like this


include this with your API key
<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;sensor=true_or_false&amp;key=ABQIAAAAOdhD1B3BVBtgDtcx-d52URS1RRrijDm1gy4LClE7B_C-myLe1RRtDZ0LHqXIq8q0z4mVVysLOLUxtA"></script>


@Html.Raw("<h1>"+PharmacyLocation+"</h1>")
@Html.Hidden("Adrs1", PharmacyLocation, new { id = "ad1" })
@Html.Hidden("Adrs2", DistanceFrom, new { id = "ad2" })
@Html.Hidden("lat", lt, new { id = "lat" })
@Html.Hidden("lng", lg, new { id = "lng" })

<style type="text/css">
  body {
    font-family: Verdana, Arial, sans serif;
    font-size: 11px;
    margin: 2px;
  }
  table.directions th {
background-color:#EEEEEE;
  }

  img {
    color: #000000;
  }
  .directions
  {
  }
  .directions td
  {
      width:300px;
  }
 #directions
 {
  width: 300px; height: auto;
  float: left;

 }
 #map_canvas{ 
  width: 300px; height: 400px;
  float:right;
  margin-top: 16px;
 }
</style>
<script src="../../Scripts/GoogleMap.js" type="text/javascript"></script>
<script>
  $(document).ready(function () {
     initialize()
 });

 </script>
<table class="directions">
<tr><th>Directions</th><th>Map</th></tr>

<tr>
<td valign="top"><div id="directions"></div></td>
<td valign="top"><div id="map_canvas"></div></td>

</tr>
</table>
<script>
    $("#map_canvas").attr("style", "position: absolute; background-color: rgb(229, 227, 223); padding-top:25px;");
</script>

Hope this will be useful for anyone who is in need.



来源:https://stackoverflow.com/questions/7842403/how-can-i-make-a-dynamic-map-listed-in-website

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!