Google地图方向查询

时间:2011-09-07 05:17:21

标签: asp.net xml json google-maps-api-3

如何根据google api返回的XML或json绘制地图?

例如,我有一个旅游信息网站。管理方面的经理可以添加包括多个目的地在内的旅游详情。期望的结果将是显示方向,地图和目的地详细信息的动态地图。

我已经读过谷歌api可以输出XML或者json,那么我如何从解析的XML或json代码中绘制一张地图?

1 个答案:

答案 0 :(得分:1)

这些是我在我的网站中使用的功能(以及一些jQuery:

var directionsDisplay;
var directionsService = new google.maps.DirectionsService();

function setMap(midpoint)
{
    var latlng = new google.maps.LatLng(midpoint.lat(), midpoint.lng());
    var myOptions = {
        zoom: 12,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var map = new google.maps.Map(document.getElementById("map_canvas"), 
                                             myOptions);
    directionsDisplay.setMap(map);

    var request = {
        origin:"[Put origin here]",
        destination:"[Put destination here]",
        travelMode: google.maps.TravelMode.DRIVING
    };

    directionsService.route(request, function(result, status) {
        if (status == google.maps.DirectionsStatus.OK) {
            directionsDisplay.setDirections(result);
        }
    });

}

function initialize() {
    var address = "[Put destination here]";
    var geocoder = new google.maps.Geocoder();
    directionsDisplay = new google.maps.DirectionsRenderer();

    var result = "";
    geocoder.geocode({'address': address}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK)
        {
            result = results[0].geometry.location;
            setMap(result);
        }
        else
        {
            $("#map_canvas").hide();
        }
    });
}