我使用JavaScript API在网页上实现了地图,现在我想显示有关某些位置的基本信息。在JavaScript API文档中,我在“位置组件”部分中发现了其中一部分,称为“基本位置显示”,但是有一个示例如何使用placeId呈现信息。

如果可能,我需要能够使用位置坐标来检索信息。我尝试使用PHP代码显示信息,该代码定义了地图上某个位置的坐标,而不是使用placeId,但是它不起作用。

这是我使用的代码示例:

var basicPlace = new nokia.places.widgets.Place({
                    placeId: PHP code instead of placeId.
                    *exp: [<?php   code;?>, <?php echo code;?>],*
                    targetNode: "map",
                    template: "nokia.blue.place"
            });


是否有可能解决这样的问题,或者有一种不涉及placeId的方法。

链接:Here DeveloperHere JavaScript API

最佳答案

如果阅读nokia.places.widgets.Place documentation,您将看到placeId是必需参数。实际上,它是HERE所保存的位置信息的主键。因此,在显示之前,您需要使用JavaScript API发出另一个请求,以获取placeId,以便您可以显示位置详细信息。这里要做的显而易见的事情是首先发出类别请求,并将placeId与每个标记一起存储,如下所示:

// Function for receiving search results from places search and process them
var processResults = function (data, requestStatus, requestId) {
    var i, len, locations, marker;

    if (requestStatus == "OK") {
        locations = data.results ? data.results.items : [data.location];
        if (locations.length > 0) {
            for (i = 0, len = locations.length; i < len; i++) {
                // Add a marker and store the placeId
                marker = new nokia.maps.map.StandardMarker(locations[i].position,
                    { text: i+1 ,
                        placeId : locations[i].placeId});
                resultSet.objects.add(marker);
            }

        }
});
// etc.. etc...


第二部分是添加单击侦听器,该侦听器将显示一个信息气泡并使用存储的placeId填充Place Widget:

  resultSet.addListener("click" ,  function(evt) {
                infoBubbles.openBubble("<div id='basicPlaceContainer'></div>",
                                     evt.target.coordinate);

                var basicPlace = new nokia.places.widgets.Place({
                    placeId: evt.target.placeId,
                    targetNode: "basicPlaceContainer",
                    template: "nokia.blue.bubble"
                });

            }, false);


完整的工作示例如下所示:

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="X-UA-Compatible" content="IE=7; IE=EmulateIE9; IE=10" />
        <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
        <title>Nokia Maps API for JavaScript Example: Search by category</title>
        <meta name="description" content="Search by category"/>
        <script type="text/javascript" charset="UTF-8" src="http://js.cit.api.here.com/se/2.5.3/jsl.js?with=all"></script>
    </head>
    <body>
        <div id="mapContainer" style="width:540px; height:334px;"></div>

        <script type="text/javascript" id="exampleJsSource">
/*  Setup authentication app_id and app_code
*/
nokia.Settings.set("app_id", "YOUR APP ID");
nokia.Settings.set("app_code", "YOUR APP CODE");
// Use staging environment (remove the line for production environment)
nokia.Settings.set("serviceMode", "cit");


// Get the DOM node to which we will append the map
var mapContainer = document.getElementById("mapContainer");
// Create a map inside the map container DOM node
var map = new nokia.maps.map.Display(mapContainer, {
    // Initial center and zoom level of the map
    center: [52.51, 13.4],
    zoomLevel: 10,
    components: [
        new nokia.maps.map.component.Behavior()
    ]
});
this.infoBubbles = new nokia.maps.map.component.InfoBubbles();
map.components.add(infoBubbles);

var searchCenter = new nokia.maps.geo.Coordinate(52.51, 13.4),
    searchManager = nokia.places.search.manager,
    resultSet;

// Function for receiving search results from places search and process them
var processResults = function (data, requestStatus, requestId) {
    var i, len, locations, marker;

    if (requestStatus == "OK") {
        locations = data.results ? data.results.items : [data.location];
        if (locations.length > 0) {
            if (resultSet) map.objects.remove(resultSet);
            resultSet = new nokia.maps.map.Container();
            resultSet.addListener("click" ,  function(evt) {
                infoBubbles.openBubble("<div id='basicPlaceContainer'></div>", evt.target.coordinate);

                var basicPlace = new nokia.places.widgets.Place({
                    placeId: evt.target.placeId,
                    targetNode: "basicPlaceContainer",
                    template: "nokia.blue.bubble"
                });

            }, false);


            for (i = 0, len = locations.length; i < len; i++) {
                marker = new nokia.maps.map.StandardMarker(locations[i].position,
                    { text: i+1 ,
                        placeId : locations[i].placeId});
                resultSet.objects.add(marker);
            }
            map.objects.add(resultSet);
            map.zoomTo(resultSet.getBoundingBox(), false);
        } else {
            alert("Your search produced no results!");
        }
    } else {
        alert("The search request failed");
    }
};



// Make a place search request
var category = "eat-drink";

map.addListener("displayready", function () {
    searchManager.findPlacesByCategory({
        category: category,
        onComplete: processResults,
        searchCenter: searchCenter
    });
});

        </script>
    </body>
</html>


结果如下所示:

关于javascript - 此处Maps API-基本地点显示(placeId),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18807620/

10-11 12:35