2012-11-12 24 views
10

एक काम कर रहे jquery मोबाइल मानचित्र दिशा पृष्ठ प्राप्त करने का प्रयास करें। इस उदाहरण सूचीबद्ध नहीं है:jquery मोबाइल में Google नक्शे के साथ दिशानिर्देशों का साफ उदाहरण?

http://jquery-ui-map.googlecode.com/svn/trunk/demos/jquery-google-maps-mobile.html#directions_map 

लेकिन उदाहरण कोड गंदा है और न यह काम कर पाने के :( Ikea इसे इस्तेमाल करता है सक्षम किया गया है

http://m.ikea.com/nl/nl/stores/415/map/ 

लेकिन वे भी बहुत अनुकूलित।। इसलिए इसकी बहुत मुश्किल js क्या होता है देखने के लिए।

किसी को भी एक अच्छा साफ उदाहरण या कार्यान्वयन का पालन करने के लिए आसान है कि के बारे में पता है?

+1

मेरा सुझाव मूल डेवलपर से संपर्क करना होगा क्योंकि यह 1.0 https://code.google.com/p/jquery-ui-map/source/browse/tags/2.0 के जेक्यूएम अल्फा संस्करण का उपयोग कर रहा है। 2/डेमो/jquery-mobile-example-3.html –

उत्तर

41

नीचे आप एक jQuery मोबाइल पेज में Google मानचित्र दिशानिर्देश सेवा का उपयोग करने का एक मूल उदाहरण पा सकते हैं।

<!DOCTYPE html> 
<html> 
    <head> 
     <title>jQuery mobile with Google maps geo directions example</title> 
     <meta content="en" http-equiv="content-language"> 
     <link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" /> 
     <script src="http://code.jquery.com/jquery-1.8.2.min.js"></script> 
     <script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script> 
     <script type="text/javascript" src="http://maps.google.com/maps/api/js?v=3&sensor=false&language=en"></script> 
     <script type="text/javascript"> 
      $(document).on("pageinit", "#map_page", function() { 
       initialize(); 
      }); 

      $(document).on('click', '#submit', function(e) { 
       e.preventDefault(); 
       calculateRoute(); 
      }); 

      var directionDisplay, 
       directionsService = new google.maps.DirectionsService(), 
       map; 

      function initialize() 
      { 
       directionsDisplay = new google.maps.DirectionsRenderer(); 
       var mapCenter = new google.maps.LatLng(59.3426606750, 18.0736160278); 

       var myOptions = { 
        zoom:10, 
        mapTypeId: google.maps.MapTypeId.ROADMAP, 
        center: mapCenter 
       } 

       map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); 
       directionsDisplay.setMap(map); 
       directionsDisplay.setPanel(document.getElementById("directions")); 
      } 

      function calculateRoute() 
      { 
       var selectedMode = $("#mode").val(), 
        start = $("#from").val(), 
        end = $("#to").val(); 

       if(start == '' || end == '') 
       { 
        // cannot calculate route 
        $("#results").hide(); 
        return; 
       } 
       else 
       { 
        var request = { 
         origin:start, 
         destination:end, 
         travelMode: google.maps.DirectionsTravelMode[selectedMode] 
        }; 

        directionsService.route(request, function(response, status) { 
         if (status == google.maps.DirectionsStatus.OK) { 
          directionsDisplay.setDirections(response); 
          $("#results").show(); 
          /* 
           var myRoute = response.routes[0].legs[0]; 
           for (var i = 0; i < myRoute.steps.length; i++) { 
            alert(myRoute.steps[i].instructions); 
           } 
          */ 
         } 
         else { 
          $("#results").hide(); 
         } 
        }); 

       } 
      } 
     </script> 
    </head> 
    <body> 
     <div data-role="page" id="map_page"> 
      <div data-role="header"> 
       <h1><a href="#">jQuery mobile - Google maps directions service</h1> 
      </div> 
      <div data-role="content"> 
       <div class="ui-bar-c ui-corner-all ui-shadow" style="padding:1em;"> 
        <div id="map_canvas" style="height:300px;"></div> 
        <div data-role="fieldcontain"> 
         <label for="from">From</label> 
         <input type="text" id="from" value="Goteborg, Sweden"/> 
        </div> 
        <div data-role="fieldcontain"> 
         <label for="to">To</label> 
         <input type="text" id="to" value="Stockholm, Sweden"/> 
        </div> 
        <div data-role="fieldcontain"> 
         <label for="mode" class="select">Transportation method:</label> 
         <select name="select-choice-0" id="mode"> 
          <option value="DRIVING">Driving</option> 
          <option value="WALKING">Walking</option> 
          <option value="BICYCLING">Bicycling</option> 
         </select> 
        </div> 
        <a data-icon="search" data-role="button" href="#" id="submit">Get directions</a> 
       </div> 
       <div id="results" style="display:none;"> 
        <div id="directions"></div> 
       </div> 
      </div> 
     </div> 
    </body> 
</html> 

enter image description here

अद्यतन 1: उदाहरण नीचे

वर्तमान स्थिति जो स्वचालित रूप से स्थित है और एक लक्ष्य गंतव्य पता है जो उपयोगकर्ता से दिया जाता है का उपयोग करता है।

<!doctype html> 
<html lang="en"> 
    <head> 
     <title>jQuery mobile with Google maps</title> 
     <link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" /> 
     <script src="http://code.jquery.com/jquery-1.8.2.min.js"></script> 
     <script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script> 
     <script type="text/javascript" src="http://maps.google.com/maps/api/js?v=3&sensor=false&language=en"></script> 
     <script type="text/javascript"> 

      var map, 
       currentPosition, 
       directionsDisplay, 
       directionsService; 

      function initialize(lat, lon) 
      { 
       directionsDisplay = new google.maps.DirectionsRenderer(); 
       directionsService = new google.maps.DirectionsService(); 

       currentPosition = new google.maps.LatLng(lat, lon); 

       map = new google.maps.Map(document.getElementById('map_canvas'), { 
        zoom: 15, 
        center: currentPosition, 
        mapTypeId: google.maps.MapTypeId.ROADMAP 
       }); 

       directionsDisplay.setMap(map); 

       var currentPositionMarker = new google.maps.Marker({ 
        position: currentPosition, 
        map: map, 
        title: "Current position" 
       }); 

       var infowindow = new google.maps.InfoWindow(); 
       google.maps.event.addListener(currentPositionMarker, 'click', function() { 
        infowindow.setContent("Current position: latitude: " + lat +" longitude: " + lon); 
        infowindow.open(map, currentPositionMarker); 
       }); 
      } 

      function locError(error) { 
       // initialize map with a static predefined latitude, longitude 
       initialize(59.3426606750, 18.0736160278); 
      } 

      function locSuccess(position) { 
       initialize(position.coords.latitude, position.coords.longitude); 
      } 

      function calculateRoute() { 
       var targetDestination = $("#target-dest").val(); 
       if (currentPosition && currentPosition != '' && targetDestination && targetDestination != '') { 
        var request = { 
         origin:currentPosition, 
         destination:targetDestination, 
         travelMode: google.maps.DirectionsTravelMode["DRIVING"] 
        }; 

        directionsService.route(request, function(response, status) { 
         if (status == google.maps.DirectionsStatus.OK) { 
          directionsDisplay.setPanel(document.getElementById("directions")); 
          directionsDisplay.setDirections(response); 

          /* 
           var myRoute = response.routes[0].legs[0]; 
           for (var i = 0; i < myRoute.steps.length; i++) { 
            alert(myRoute.steps[i].instructions); 
           } 
          */ 
          $("#results").show(); 
         } 
         else { 
          $("#results").hide(); 
         } 
        }); 
       } 
       else { 
        $("#results").hide(); 
       } 
      } 

      $(document).live("pagebeforeshow", "#map_page", function() { 
       navigator.geolocation.getCurrentPosition(locSuccess, locError); 
      }); 

      $(document).on('click', '#directions-button', function(e){ 
       e.preventDefault(); 
       calculateRoute(); 
      }); 

     </script> 
    </head> 
    <body> 
     <div id="basic-map" data-role="page"> 
      <div data-role="header"> 
       <h1><a data-ajax="false" href="/">jQuery mobile with Google maps v3</a></h1> 
      </div> 
      <div data-role="content"> 
       <div class="ui-bar-c ui-corner-all ui-shadow" style="padding:1em;"> 
        <div id="map_canvas" style="height:350px;"></div> 
       </div> 
       <div data-role="fieldcontain"> 
        <label for="target-dest">Target Destination:</label> 
        <input type="text" name="target-dest" id="target-dest" value="" /> 
       </div> 
       <a href="#" id="directions-button" data-role="button" data-inline="true" data-mini="true">Get Directions</a> 
       <div id="results" style="display:none;"> 
        <div id="directions"></div> 
       </div> 
      </div> 
     </div>  
    </body> 
</html> 

अद्यतन 2:

उदाहरण नीचे

वर्तमान स्थिति पता लगाता है और यह स्वचालित रूप से दूरी की गणना करता है और वर्तमान स्थान और एक स्थिर पूर्वनिर्धारित गंतव्य स्थान के बीच के मार्ग विवरण दिखाता है।

<!doctype html> 
<html lang="en"> 
    <head> 
     <title>jQuery mobile with Google maps</title> 
     <link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" /> 
     <script src="http://code.jquery.com/jquery-1.8.2.min.js"></script> 
     <script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script> 
     <script type="text/javascript" src="http://maps.google.com/maps/api/js?v=3&sensor=false&language=en"></script> 
     <script type="text/javascript"> 

      var map, 
       currentPosition, 
       directionsDisplay, 
       directionsService, 
       destinationLatitude = 59.3426606750, 
       destinationLongitude = 18.0736160278; 

      function initializeMapAndCalculateRoute(lat, lon) 
      { 
       directionsDisplay = new google.maps.DirectionsRenderer(); 
       directionsService = new google.maps.DirectionsService(); 

       currentPosition = new google.maps.LatLng(lat, lon); 

       map = new google.maps.Map(document.getElementById('map_canvas'), { 
        zoom: 15, 
        center: currentPosition, 
        mapTypeId: google.maps.MapTypeId.ROADMAP 
       }); 

       directionsDisplay.setMap(map); 

       var currentPositionMarker = new google.maps.Marker({ 
        position: currentPosition, 
        map: map, 
        title: "Current position" 
       }); 

       // current position marker info 
       /* 
       var infowindow = new google.maps.InfoWindow(); 
       google.maps.event.addListener(currentPositionMarker, 'click', function() { 
        infowindow.setContent("Current position: latitude: " + lat +" longitude: " + lon); 
        infowindow.open(map, currentPositionMarker); 
       }); 
       */ 

       // calculate Route 
       calculateRoute(); 
      } 

      function locError(error) { 
       // the current position could not be located 
      } 

      function locSuccess(position) { 
       // initialize map with current position and calculate the route 
       initializeMapAndCalculateRoute(position.coords.latitude, position.coords.longitude); 
      } 

      function calculateRoute() { 

       var targetDestination = new google.maps.LatLng(destinationLatitude, destinationLongitude); 
       if (currentPosition != '' && targetDestination != '') { 

        var request = { 
         origin: currentPosition, 
         destination: targetDestination, 
         travelMode: google.maps.DirectionsTravelMode["DRIVING"] 
        }; 

        directionsService.route(request, function(response, status) { 
         if (status == google.maps.DirectionsStatus.OK) { 
          directionsDisplay.setPanel(document.getElementById("directions")); 
          directionsDisplay.setDirections(response); 

          /* 
           var myRoute = response.routes[0].legs[0]; 
           for (var i = 0; i < myRoute.steps.length; i++) { 
            alert(myRoute.steps[i].instructions); 
           } 
          */ 
          $("#results").show(); 
         } 
         else { 
          $("#results").hide(); 
         } 
        }); 
       } 
       else { 
        $("#results").hide(); 
       } 
      } 

      $(document).live("pagebeforeshow", "#map_page", function() { 
       // find current position and on success initialize map and calculate the route 
       navigator.geolocation.getCurrentPosition(locSuccess, locError); 
      }); 

     </script> 
    </head> 
    <body> 
     <div id="basic-map" data-role="page"> 
      <div data-role="header"> 
       <h1><a data-ajax="false" href="/">jQuery mobile with Google maps v3</a></h1> 
      </div> 
      <div data-role="content"> 
       <div class="ui-bar-c ui-corner-all ui-shadow" style="padding:1em;"> 
        <div id="map_canvas" style="height:350px;"></div> 
       </div> 
       <div id="results" style="display:none;"> 
        <div id="directions"></div> 
       </div> 
      </div> 
     </div>  
    </body> 
</html> 

मुझे उम्मीद है कि इससे मदद मिलती है।

+0

धन्यवाद यह साइट thx पर प्रदान किए गए लोगों की तुलना में एक क्लीनर उदाहरण है। लेकिन समस्या यह है कि अभी भी वर्तमान स्थान (उदाहरण के लिए आईफोन पर जीपीएस) के आधार पर कोई दिशा नहीं है ** इसलिए मैं इसे सही उत्तर के रूप में चिह्नित नहीं कर सकता। मैं आपके उदाहरण पर निर्माण करने की कोशिश करता हूं और भौगोलिक सामग्री – Rubytastic

+0

जोड़ता हूं क्या आप मेरा अद्यतन उत्तर जांचेंगे। मैंने एक उदाहरण जोड़ा जो वर्तमान स्थान का उपयोग करता है। –

+0

बहुत बहुत धन्यवाद, वास्तव में इसके साथ गड़बड़ हो गया लेकिन Google मानचित्र काम करने के लिए मुश्किल हो सकता है। मेरे पास केवल 1 आखिरी समस्या है, मैं * जब उपयोगकर्ता अपना स्थान देता है तो मैं दिशा निर्देशों की गणना करना चाहता हूं। क्या आप जानते हैं कि यह कैसे करना है? मैंने निर्देश फ़ील्ड और बटन को हटा दिया और जेएस में हार्डकोड किए गए गंतव्य का मान सेट किया। अंतिम चरण यह होगा कि उपयोगकर्ता अपने वर्तमान स्थान – Rubytastic