Google使用存储的结果映射路线

时间:2011-07-28 17:03:15

标签: json google-maps

我正在尝试整理一个应用程序,我可以在其中查询googles指示服务,存储结果以构建缓存,然后根据需要呈现指示。

我可以获取方向数据并将其存储在数据库中就好了,这一切都很好,现在当我在地图上渲染方向时,我的javascript真的让我失望了。 (strData)是一个字符串,包含数据库中json格式的方向。

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="map.aspx.cs" Inherits="panto" %>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no"/>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>Google Maps JavaScript API v3 Example: Directions Simple</title>
<link href="http://code.google.com/apis/maps/documentation/javascript/examples/default.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
var directionDisplay;
var directionsService = new google.maps.DirectionsService();
var map;

function initialize() {
    directionsDisplay = new google.maps.DirectionsRenderer();
    var chicago = new google.maps.LatLng(41.850033, -87.6500523);
    var myOptions = {
        zoom: 7,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        center: chicago
    }
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    directionsDisplay.setMap(map);
    directionsDisplay.setDirections(<%= strData %>);
}

function calcRoute() {
    var start = document.getElementById("start").value;
    var end = document.getElementById("end").value;
    var request = {
        origin: start,
        destination: end,
        travelMode: google.maps.DirectionsTravelMode.DRIVING
    };
    directionsService.route(request, function (response, status) {
        if (status == google.maps.DirectionsStatus.OK) {
            alert(response);
            directionsDisplay.setDirections(response);
        }
    });
}
</script>
</head>
<body onload="initialize()">
<div>

</div>
<div id="map_canvas" style="top:30px;"></div>
</body>
</html>

firefox错误控制台报告错误

Error: g[Xb] is not a function
Source File: http://maps.gstatic.com/intl/en_gb/mapfiles/api-3/5/11/main.js
Line: 109

任何帮助将不胜感激

2 个答案:

答案 0 :(得分:9)

要从Google Web Service(与JavaScript API相比)呈现路线结果,我们可以在结果中使用编码路径。

  1. 添加几何库以解码路径:

    http://maps.google.com/maps/api/js?libraries=geometry&sensor=false
    
  2. 解码路径并渲染折线

    var rideCoordinates = google.maps.geometry.encoding.decodePath(results.routes[0].overview_polyline.points);       
    var ridePath = new google.maps.Polyline({
        map:            map_object,       
        path:           rideCoordinates,
        strokeColor:    "#FF0000",
        strokeOpacity:  1.0,
        strokeWeight:   3
    });
    
  3. 设置地图的边界

    var ne = new google.maps.LatLng(results.routes[0].bounds.northeast.lat, route.bounds.northeast.lng);
    var sw = new google.maps.LatLng(results.routes[0].bounds.southwest.lat, route.bounds.southwest.lng);
    map_object.fitBounds(new google.maps.LatLngBounds(sw, ne));
    
  4. HTH

答案 1 :(得分:2)

序列化对象时,所有方法都将消失。 g [Zb]不是函数,实际意味着找不到属性'bounds'上的方法'getSouthWest'。

如果你得到结果,然后像这样重新创建对象:results.routes [0] .bounds = new google.maps.LatLngBound(results.routes [0] .bounds);然后至少第一个错误消息将被修复......