简体   繁体   English

使用ajax返回谷歌地图标记始终为空

[英]Returning google map markers with ajax always null

EDIT: Actual error I'm getting from the network data:编辑:我从网络数据中得到的实际错误:

在此处输入图片说明

"Invalid JSON primitive: position" “无效的 JSON 原语:位置”

Code calling for it:调用它的代码:

function AddMarkerAndPanTo(latLng, map) {
    var marker = new google.maps.Marker({
        position: latLng,
        map: map
    });

    var x = marker.position;

    $.ajax({
        type: "POST",
        url: '@Url.Action("AddMarker", "Map")', // Map Controller, AddMarker Action
        data: JSON.stringify({ "position": x }),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {
            if (data.done)
                alert("Marker added");
            else
                alert("MARKER FAILED");
        }
    });
    map.panTo(latLng);
}

The server side function using a view model posted below it:使用在其下方发布的视图模型的服务器端功能:

[HttpPost]
public ActionResult AddMarker(LatLngViewModel position)
{            
    if (position != null)
        return Json(true, JsonRequestBehavior.AllowGet);
    else
        return Json(false, JsonRequestBehavior.AllowGet);
}

ViewModel:视图模型:

public class LatLngViewModel
{
    double latitude { get; set; }
    double longitude { get; set; }
}

I tried using with quotes and without on position, stringifying the result or returning it with toJSON().我尝试使用引号而不是位置,将结果字符串化或使用 toJSON() 返回它。 Tried stitching the toSend together with various quotes etc. At this point I have no idea what's causing it and I have no other solutions at hand.尝试将 toSend 与各种引号等缝合在一起。此时我不知道是什么原因造成的,我手头没有其他解决方案。 Appreciate any help on resolving this and sending the bloody coordinates back to bind them to the model somehow.感谢任何帮助解决这个问题并将血腥坐标发送回以某种方式将它们绑定到模型的帮助。

You need to pass an object with latitude and longitude properties to match your view model.您需要传递具有纬度和经度属性的对象以匹配您的视图模型。 Like that:像那样:

function AddMarkerAndPanTo(latLng, map) {
    var marker = new google.maps.Marker({
        position: latLng,
        map: map
    });

    $.ajax({
        type: "POST",
        url: '@Url.Action("AddMarker", "Map")', // Map Controller, AddMarker Action
        data: { 
            latitude: latLng.lat(), 
            longitude: latLng.lng() 
        },
        success: function (data) {
            if (data.done)
                alert("Marker added");
            else
                alert("MARKER FAILED");
        }
    });

    map.panTo(latLng);
}

This is the important part that you were missing:这是您错过的重要部分:

data: { 
    latitude: latLng.lat(), 
    longitude: latLng.lng() 
},

The latLng object that is passed to this function has 2 properties lat and lng which in turn are functions that you need to invoke in order to get the corresponding coordinates as decimal values.传递给此函数的latLng对象具有 2 个属性latlng ,它们又是您需要调用的函数,以便将相应的坐标作为十进制值获取。


UPDATE:更新:

Make sure that your view model properties are public :确保您的视图模型属性是public

public class LatLngViewModel
{
    public double latitude { get; set; }
    public double longitude { get; set; }
}

Don't use JSON.stringify to prepare the post-data.不要使用JSON.stringify来准备后期数据。

Simply use :只需使用:

data: { "position": latLng.toJSON()},

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM