Google绘制了如何在最近的道路上强制标记 [英] Google maps how to force marker on the nearest road

查看:267
本文介绍了Google绘制了如何在最近的道路上强制标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个车辆追踪项目,我正在从数据库中获取coordiantes,并显示在Google地图上. 这是我的代码..... !!

I am doing a vehicle traking project, i am getting coordiantes from the databases, and showing on the google maps. here is my code.....!!

function get_coordinates(checkbox){
    var v_id=checkbox.id;
    if(checkbox.checked){
        var hr = new XMLHttpRequest();
        hr.open("POST", "fetch_coordinates.php", true);
        hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        hr.onreadystatechange = function() {
            if(hr.readyState == 4 && hr.status == 200) {
                var data = JSON.parse(hr.responseText);
                var lat=data.loc.lat;
                var lon=data.loc.lon;
                addmarker(lat,lon,v_id);
            }
        }
         hr.send("id="+v_id);   
    } else{
        var mark = markers[v_id]; // find the marker by given id
        mark.setMap(null);
        delete markers[v_id];

    }    
}
function addmarker(lat,lon,v_id){
    var marker = new google.maps.Marker({
        id: v_id,
        position: new google.maps.LatLng(lat, lon),
        zoom: 8,
        map: map,
        title: 'Vehicle No: '+v_id,
        icon: 'live.gif',
        optimized:false

    });
    markers[v_id] = marker;
    bounds.extend(new google.maps.LatLng(lat,lon));
   // map.setOptions({center:new google.maps.LatLng(lat,lon),zoom:8});
    map.fitBounds(bounds);
}

但是问题是,有时我会得到距路面1,2英寸的GPS coordiantes(可能是因为设备的精度降低或信号失真等) 我应该如何强制标记器在路上自动调整?有没有办法使用方向渲染或任何其他提示?请帮助

But the problem is, sometimes i get GPS coordiantes which are 1,2 inches away from the road (possibly because of less precison of device or signal distortion etc) How should I force my marker to automatically adjust on the road? Is there a some way, using direction rendering or any other hint ??? please help

推荐答案

https://developers.google.com/maps/documentation/directions/intro

您可以使用所需的URL来获取json请求.

You can use your desired url to get json request..

在编码"部分中,您可以编写此代码以获取道路上的位置.

In the Coding section you can write this code to get the location on the road..

    Marker liveloc;
    JSONObject obj1=new JSONObject(s);
    JSONArray arr1=obj1.getJSONArray("routes");

    for (int i=0;i<arr1.length();i++){

    JSONObject obj2=arr1.getJSONObject(i);
    JSONArray arr2=obj2.getJSONArray("legs");

    JSONObject obj3=arr2.getJSONObject(0);
    JSONObject obj4=obj3.getJSONObject("start_location");
    String k=obj4.getString("lat");
    String k2=obj4.getString("lng");
    double k3=Double.parseDouble(k);
    double k4=Double.parseDouble(k2);
    LatLng myloc=new LatLng(k3,k4);

    if (liveloc !=null){
        liveloc.remove();
        }
   liveloc=mMap.addMarker(new MarkerOptions().position(myloc));

}

您需要从json请求中获取第一个起始位置.

You need to get the first start location from the json request..

希望有帮助...

这篇关于Google绘制了如何在最近的道路上强制标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆