douchun1961
2018-01-10 17:21
浏览 53
已采纳

如何使用PHP修改Google Map的大型json结构

I have a Google Map using the Google Map API drawing polylines from a .json file. A portion of the file structure can be seen below, it just repeats for each polyine feature. It was created in GIS and converted to a .geojson and then json.

I am looking to allow a user to modify specific properties of a feature through a mapping interface. I have the iterface and user interactions all built out but am having trouble modifying the .json file.

I have tried the following:

PHP (Note: I am hard-coding some values to test/get it to work)

$jsonString = file_get_contents('../json/hartford.json');
$data = json_decode($jsonString, true);

foreach ($data as $key => $entry) {
    if ($entry['UID'] == 25301) {
        $data[$key]['RENDER_CL'] = "99";
    }
}

$newJsonString = json_encode($data);
file_put_contents('../json/hartford.json', $newJsonString);

Errors:

Warning: Illegal string offset 'UID' on line...

Notice: Undefined index: UID on line...

Thoughts:

I feel I am not going deep enough in to the json "tree" to access the properties. I have not modified json data before so I am a bit lost. Should I somehow be going deeper into the structure? UID is unique for each item. Thanks.

JSON file

{
"type": "FeatureCollection",
"crs": {
    "type": "name",
    "properties": {
        "name": "urn:ogc:def:crs:OGC:1.3:CRS84"
    }
},
"features": [{
    "type": "Feature",
    "properties": {
        "FULLNAME": "I-84 E",
        "RENDER_CL": 0,
        "FCC": "Highway",
        "clicked": "no",
        "UID": 25301
    },
    "geometry": {
        "type": "MultiLineString",
        "coordinates": [[[-72.70621818192563523552962578833103179931640625, 41.7494854544135023388662375509738922119140625], [-72.7059645455619971698979497887194156646728515625, 41.74953636350443275659927166998386383056640625], [-72.7057163637438179648597724735736846923828125, 41.749591818049879066165885888040065765380859375], [-72.705553636471080380943021737039089202880859375, 41.749633636231692435103468596935272216796875], [-72.7053663637438063460649573244154453277587890625, 41.749688181686252619329025037586688995361328125], [-72.705190909198364579424378462135791778564453125, 41.749742727140784381845151074230670928955078125], [-72.70496727283472182534751482307910919189453125, 41.749821818049866806177305988967418670654296875], [-72.704716363743813190012588165700435638427734375, 41.749924545322613766984432004392147064208984375], [-72.70461181828926555681391619145870208740234375, 41.74996818168625623002299107611179351806640625]]]
    }
}, {
    "type": "Feature",
    "properties": {
        "FULLNAME": "I-84 E",
        "RENDER_CL": 0,
        "FCC": "Highway",
        "clicked": "no",
        "UID": 25302
    },
    "geometry": {
        "type": "MultiLineString",
        "coordinates": [[[-72.7150890910165372815754381008446216583251953125, 41.749747272595328695388161577284336090087890625], [-72.7141218182892572485798154957592487335205078125, 41.7496918180498823858215473592281341552734375]]]
    }
},
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

  • dsdzvprlp51692469 2018-01-10 17:44
    最佳回答

    You are accessing your object's property incorrectly. the correct way would be $entry['properties']['UID'].

    Example

    <?php
    
    $json =
        '{
            "type": "Feature",
            "properties": {
                "FULLNAME": "I-84 E",
                "RENDER_CL": 0,
                "FCC": "Highway",
                "clicked": "no",
                "UID": 25302
            },
            "geometry": {
                "type": "MultiLineString",
                "coordinates": [[[-72.7150890910165372815754381008446216583251953125, 41.749747272595328695388161577284336090087890625], [-72.7141218182892572485798154957592487335205078125, 41.7496918180498823858215473592281341552734375]]]
            }
        }';
    
    $obj = json_decode($json, true);
    
    print $obj['properties']['UID'];
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题