1

I have a problem in my MVC4 project in visual studio 2012. I build a backoffice in symfony2 and there i can display google maps with polygons loaded from a kml file.

Now i am trying to do the same thing in asp.net (C#)... But my code doesn't seem able to access the kml-file.

I am not using the Model View Controller system to acces my file, and maybe that is the problem...

i made a directory in my project named 'Kmls' and i pasted my kml in there (1.kml, 2.kml, ...) this is my code of my view:

@model IEnumerable<SocialGeo.Models.district>
@{
   ViewBag.Title = "Index";
}

<h3>Choose a district first</h3>

<ul class="allDistricts" data-kml="">
    @foreach (var item in Model) {
        @Html.ActionLink(item.district_name, "DistrictNews", new { id=item.district_id})
    }
</ul>

<script type="text/javascript">
  $(document).ready(function () {
    var myOptions = {
        center: new google.maps.LatLng(51.087633, 3.711569),
        zoom: 11,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }



    var paths =
    {
        imagePath: "@Url.Content("~/Kmls/10.kml")"
    }


    $("section").append('<div class="mapscanvas" id="map_canvas' + 0 + '"></div>');

    map = new google.maps.Map(document.getElementById("map_canvas" + 0), myOptions);

    geoXml = geoXML3.parser({ map: map, singleInfoWindow: true, zoom: false });
    //var jej = @HttpContext.Current.Server.MapPath("~/Kmls/10.kml")

    geoXml.parse(paths.imagePath);

});
</script>

When i run the application (debugging) in firefox i get the following error in console:

GET: http://localhost:53760/Kmls/10.kml 404NotFound
HTTP error 404 retrieving /Kmls/10.kml in polys.js (line 990)
Unable to retrieve /Kmls/10.kml

I see the map, but not the polygon that I need.

Do I have to use a controller for this too? this is my controlleraction that is being called:

public ActionResult Index()
{
    var districts = db.districts.Include(d => d.city);
    return View(districts.ToList()); 
}

Any help would be highly appreciated!

1 Answers1

1

I think your problem may be that in order to load a KML file in Google Maps (at least for the free version), it needs to be Web Accessible (i.e. the URL you provide needs to be accessible from the web, not just locally on your development / production server). Therefore "/Kmls/10.kml" would not be a valid URL and something like "http://myserver/KML" is required.

As provided in the comments below, here is a link to a similar answered question: Google Maps API and KML File LocalHost Development Options

Community
  • 1
  • 1
Matthew
  • 9,851
  • 4
  • 46
  • 77
  • Can i reproduce this in my code or do I have to wait to test it when my project is being deployed online? Keep in mind that I was able to see the kml's in another local project (but that was in mac invironment and in php)... any thoughts on this would be awesome Or a way to test that my problem is indeed the problem that you say I have :) already a big thanks for looking into this. – Mathieu Spillebeen Jan 10 '13 at 01:12
  • Just dug back through for the answer I read when trying to do the same thing you are asking in Sept/Oct, found it here: http://stackoverflow.com/questions/6092110/google-maps-api-and-kml-file-localhost-development-options – Matthew Jan 10 '13 at 03:46
  • put the link in your initial answer and i accept... Thanks for clearing this up! – Mathieu Spillebeen Jan 10 '13 at 13:42