2016-11-03 12 views
5

के लिए जेएसओएन डेटा से प्लेस मार्कर मेरे पास एक MySQL डीबी है जिसे मैंने जेएसओएन प्रारूप में खींचने के लिए एक PHP स्क्रिप्ट बनाई है। मुझे पता है कि जेएसओएन आउटपुट लेने और Google मानचित्र पर मार्कर बनाने की आवश्यकता है। सरल लगता है, लेकिन मुझे केवल यह दिखाने के लिए मार्करों की आवश्यकता है कि JSON आउटपुट के मानों में से कोई एक सत्य सत्य करता है या नहीं। मैं बताऊंगा कि मार्करों को कैसे प्रदर्शित करना चाहिए।Google एमएपीएस एपीआई v3

JSON आउटपुट

gpsStatus": "true", = Show streamOffline.png icon/marker 

If gpsStatus & "streamStatus": "true", Then show the streamOnine.png icon/marker 

If gpsStatus": "false" the show or remove from map the streamOffline.png icon/marker 

तो मूल रूप से केवल समय एक आइकन/निर्माता दिखाया जाना चाहिए है अगर gpsStatus ":" सच "लेकिन streamStatus की स्थिति के आधार अगर यह streamOffline.png आइकन/मार्कर से पता चलता निर्धारित करता है या streamOnline.png आइकन/marker.gpsStatus ":" false "स्ट्रीमस्टैटस मान के बावजूद मार्कर को हटाता या दिखाता नहीं है।

एक और मोड़ यह है कि मैं जेएसओएन आउटपुट से लैट/एलएनजी मूल्यों से उस डेटा के आधार पर मानचित्र को पुनः लोड किए बिना मार्कर को अपडेट/रीफ्रेश करने का प्रयास कर रहा हूं। यदि मैं जेएसओएन आउटपुट से अन्य मूल्यों को खींचने की भी कोशिश कर रहा हूं तो मैं डेटा को इन्फॉइडोज़ इत्यादि में डाल सकता हूं।

मैं स्टैक, Google खोज और यूट्यूब दोनों पर यह कैसे करना है और विभिन्न चीजों को आजमाने के तरीकों की खोज कर रहा हूं (यहां सूचीबद्ध करने और पोस्ट करने के लिए बहुत सी चीजें हैं) लेकिन, अधिकांश उदाहरण ईथर मेरे लिए लागू नहीं होते हैं या पुराने हैं और मैं अपनी ज़रूरतों के लिए काम नहीं कर सकता हूं। जब जावास्क्रिप्ट और Google मानचित्र की बात आती है तो मैं भयानक हूं।

तो क्या कोई ऐसा व्यक्ति है जो मुझे जेएसओएन डेटा और लूप को कैसे ले सकता है, इस बारे में मेरी स्थिति के आधार पर कुछ उदाहरणों के मूल्य के आधार पर मानचित्र पर गतिशील मार्करों को साजिश करने और रीफ्रेश/उन्हें अपडेट करें जब lat/lng मान बदलते हैं और फिर मार्कर को हटाते हैं जब "gpsStatus" झूठा दिखाता है और साथ ही यह जानता है कि अन्य क्षेत्रों में किन कुंजीों का उपयोग करना है?

यहां मेरा JSON आउटपुट है।

http://stream.dfwstormforce.com/test/api.php

यहाँ एक स्थिर मार्कर और क्या यह आबादी वाले डेटा है कि मैं पूरा करने के लिए कोशिश कर रहा हूँ के साथ की तरह दिखना चाहिए के साथ अपने परीक्षण मानचित्र है।

http://stream.dfwstormforce.com/test/test.html

उत्तर

3

मैं मैं क्या वर्णन ऊपर से समझा द्वारा कोड बदलने की कोशिश की है, तो आप इसे अब बढ़ा सकता है।

  var customIcons = { 
        offline: { 
        icon: 'http://stream.dfwstormforce.com/images/icons/gpsOffline.png' 
        }, 
        online: { 
        icon: 'http://stream.dfwstormforce.com/images/icons/gpsOnline.png' 
        } 
       }; 

      var getCustomIcons= function (streamdata){ 
        var iconUrl=customIcons.online; 
         if(streamdata.gpsStatus=='false') 
         { 
         iconUrl=customIcons.offline.icon; 
         }else 
         { 
         iconUrl=customIcons.online.icon; 
         } 
         return iconUrl; 
       } 


       $.getJSON('http://stream.dfwstormforce.com/inc/api.php', function(responsedata) { 
         $.each(responsedata.streamers, function(i, value) { 
          if(value.lat!="" && value.lng!="") 
          { 
            var marker = new google.maps.Marker({ 
             position: {lat: parseFloat(value.lat), lng: parseFloat(value.lng)}, 
             animation: google.maps.Animation.DROP, 
             title: value.DisplayName+','+ value.ChaserLocation, 
             icon: getCustomIcons(value), 
             map: map 
            }); 

            marker.addListener('click', function() { 
              infowindow.open(map, marker); 
             }); 
          } 
         }); 
      }); 
+0

सहायता के लिए धन्यवाद, हालांकि यह काम नहीं कर रहा है। मैं प्रतीक का नाम बदल लें शायद यह कम भ्रामक मैं पूरा किया और साथ ही उस जानकारी के साथ अपने मूल पोस्ट को अद्यतन करने के लिए कोशिश कर रहा हूँ के साथ बनाने के लिए। लिंक में कोड भी अपडेट किया गया है। कोई मार्कर प्लॉट नहीं किया जा रहा है और मुझे कंसोल में कोई त्रुटि नहीं मिल रही है। कोई सुझाव? मैं जेएस के साथ बिल्कुल अच्छा नहीं हूँ। मुझे पता है कि अगर मेरे पास जाने के लिए अच्छा आधार है तो मुझे चारों ओर पोक करने के लिए पर्याप्त जानकारी है। – Texan78

1

समस्या है जब आप, अपने अक्षांश और एक मार्कर के रूप देशांतर योजना बना रहे हैं $.getJSON आप उन्हें केवल parseFloat का उपयोग कर परिवर्तित कर रहे हैं से प्रतिक्रिया प्राप्त करने के बाद - लेकिन अक्षांश/एलएनजी शामिल एन, ई, डब्ल्यू और एस अक्षर तो parseFloat विधि विफल रहा है।

यानी।आप/एलएनजी अक्षांश इस

 "lat": "N32.95421", 
     "lng": "W96.38196", 

की तरह हैं तो जब आप कोड के इस हिस्से में parseFloat का उपयोग करें - परिणाम NaN

$.each(responsedata.streamers, function(i, value) { 
    if (value.lat != "" && value.lng != "") { 

    var myLat = parseFloat(value.lat); 

    // HERE on this line check in your CONSOLE the result is NaN 
    console.log(myLat); 

    var marker = new google.maps.Marker({ 
     position: { 
     lat: value.lat, 
     lng: value.lng 
     }, 
     animation: google.maps.Animation.DROP, 
     title: value.DisplayName + ',' + value.ChaserLocation, 
     map: map 
    }); 
    } 
}); 

है तो तुम क्या करने की जरूरत के लिए अक्षांश और देशांतर कन्वर्ट है पहले दशमलव प्रारूप:

$.each(responsedata.streamers, function(i, value) { 
    if (value.lat != "" && value.lng != "") { 
    var myLat = ConvertPoint(value.lat); //parseFloat(value.lat); 

    var myLng = ConvertPoint(value.lng); 

    var marker = new google.maps.Marker({ 
     position: { 
     lat: myLat, 
     lng: myLng 
     }, 
     animation: google.maps.Animation.DROP, 
     title: value.DisplayName + ',' + value.ChaserLocation, 
     //icon: getcustomicons(value), 
     map: map 
    }); 

    //marker.addlistener('click', function() { 
    //  infowindow.open(map, marker); 
    // }); 
    } 
}); 

अपने जे एस कोड को यह समारोह जोड़े

function ConvertPoint(stringVal) { 
    // This function will convert you lat, lng - instead of using parseFloat 

    var direction = stringVal.substring(0, 1); 

    var withOutChar = stringVal.substring(1); 

    var point = parseFloat(withOutChar); 

    if (direction == "S" || direction == "W") { 
     point = point * -1; 
    } // Don't do anything for N or E 

    return point; 
    } 

मार्कर प्लॉट सही तरीके से मुझे बताएं।

कई मार्कर और जानकारी विंडो को संबंधित और फिर उन्हें ताज़ा करता है, तो वे बदल जाते हैं आप कुछ और काम करने के लिए होगा - लेकिन पहले मार्कर प्लॉट करने के लिए मिलता है - अब के लिए कस्टम आइकन के बिना प्रयास करें। इसे आसान रखें और फिर अगले चरण पर जाएं।

1

यहां अपने पृष्ठ के लिए पूरी तरह से काम कर रहे उदाहरण है। इस सरलीकृत संस्करण पर, आपके JSON API http://stream.dfwstormforce.com/test/api.php को हर 10 सेकंड में समय-समय पर लाया जाएगा, और योग्य डेटा (gpsStatus = true) मानचित्र पर प्रदर्शित किया जाएगा।

प्रदर्शित मार्कर आइकन streamStatus मान पर निर्भर करेगा।

<!doctype html> 
<html> 
<head> 
    <title>Google Map Test Page</title> 
    <style> 
     html, body, .map { 
      width: 100%; 
      height: 100%; 
      background-color: #eee; 
     } 
    </style> 
</head> 
<body> 
    <div id="map" class="map"></div> 

    <script> 
     var map = null; 
     var markers = []; 

     // Reload interval in milliseconds. 
     var reloadInterval = 10000; 

     var intervalId = window.setInterval(loadData, reloadInterval); 

     function initMap() { 
      var northTexasPosition = { lat: 32.836, lng: -96.995 }; 

      map = new google.maps.Map(document.getElementById('map'), { 
       center: northTexasPosition, 
       zoom: 7, 
       mapTypeControl: true, 
       mapTypeControlOptions: { 
        style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR, 
        position: google.maps.ControlPosition.TOP_RIGHT 
       }, 
      }); 

      loadData(); 
     } 

     // Load the markers data from API and display them on the map. 
     function loadData() { 
      $.getJSON('http://stream.dfwstormforce.com/test/api.php', function(responsedata) { 
       clearMarkers(); 

       $.each(responsedata.streamers, function (key, item) { 
        item.lat = parseCoordinateNumber(item.lat); 
        item.lng = parseCoordinateNumber(item.lng); 
        item.gpsStatus = parseBoolean(item.gpsStatus); 
        item.streamStatus = parseBoolean(item.streamStatus); 

        if (shouldAddToMap(item)) { 
         addToMap(item); 
        } 
       }); 
      }); 
     } 

     // Clear all markers from the map. 
     function clearMarkers() { 
      $.each(markers, function (key, marker) { 
       marker.setMap(null); 
      }); 

      markers = []; 
     } 

     // Add marker to the map. 
     function addToMap(item) { 
      var marker = new google.maps.Marker({ 
       position: { lat: item.lat, lng: item.lng }, 
       title: item.DisplayName + ', ' + item.ChaserLocation, 
       icon: getIcon(item.streamStatus), 
       map: map 
      }); 

      markers.push(marker); 
     } 

     // Get the appropiate image url for marker icon. 
     function getIcon(streamStatus) { 
      if (! streamStatus) { 
       return 'http://stream.dfwstormforce.com/images/icons/streamOffline.png'; 
      } 

      return 'http://stream.dfwstormforce.com/images/icons/streamOnline.png'; 
     } 

     // Should we add the marker to the map? 
     function shouldAddToMap(item) { 
      if ($.type(item.lat) === 'null' || $.type(item.lng) === 'null') { 
       return false; 
      } 

      return item.gpsStatus === true; 
     } 

     // Parse coordinate number like 'W96.38188' to appropiate decimal value: -96.38188 
     // return null if it's invalid. 
     function parseCoordinateNumber(val) { 
      if ($.type(val) === 'number') { 
       return parseFloat(val); 
      } 

      if ($.type('val') !== 'string') { 
       return null; 
      } 

      val = val.trim(); 

      if (val.length === 0) { 
       return null; 
      } 

      var directionPart = val.substr(0, 1).toUpperCase(); 
      var valuePart = parseFloat(val.substring(1)); 

      if ($.inArray(directionPart, ['N', 'E']) >= 0) { 
       return isNaN(valuePart) ? null : valuePart; 
      } 

      if ($.inArray(directionPart, ['S', 'W']) >= 0) { 
       return isNaN(valuePart) ? null : -valuePart; 
      } 

      val = parseFloat(val); 

      return isNaN(val) ? null : val; 
     } 

     // Parse boolean value. 
     function parseBoolean(val) { 
      if ($.type(val) === 'boolean') { 
       return val; 
      } 

      if (val === 1) { 
       return true; 
      } 

      if (val === 0) { 
       return false; 
      } 

      if ($.type('val') !== 'string') { 
       return null; 
      } 

      val = val.trim().toUpperCase(); 

      if (val === 'TRUE') { 
       return true; 
      } 

      if (val === 'FALSE') { 
       return false; 
      } 

      return null; 
     } 
    </script> 

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> 
    <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap" async="defer"></script> 
</body> 
</html> 

रूप @Dawood Awan से कहा, मुख्य समस्या अपने lat और lng डेटा एक डिग्री की दिशा (एन, एस, ई, डब्ल्यू) का इस्तेमाल किया है। आपको इन डेटा को दशमलव में एप्राइपिएट समन्वय मान में पार्स करना होगा। यहाँ मेरी कोड का हिस्सा है ऊपर है कि lat और lng पार्स जाएगा:

function parseCoordinateNumber(val) { 
    if ($.type(val) === 'number') { 
     return parseFloat(val); 
    } 

    if ($.type('val') !== 'string') { 
     return null; 
    } 

    val = val.trim(); 

    if (val.length === 0) { 
     return null; 
    } 

    var directionPart = val.substr(0, 1).toUpperCase(); 
    var valuePart = parseFloat(val.substring(1)); 

    if ($.inArray(directionPart, ['N', 'E']) >= 0) { 
     return isNaN(valuePart) ? null : valuePart; 
    } 

    if ($.inArray(directionPart, ['S', 'W']) >= 0) { 
     return isNaN(valuePart) ? null : -valuePart; 
    } 

    val = parseFloat(val); 

    return isNaN(val) ? null : val; 
} 

यह या संख्या में निर्देशांक मान दिशा (N, S, E, या W) के साथ एक स्ट्रिंग की अनुमति देगा। मैंने सही अक्षांश या देशांतर सीमा को संभाला नहीं है, लेकिन आप इसे भी जोड़ सकते हैं। यदि दिया गया समन्वय अमान्य है तो फ़ंक्शन null लौटाएगा।

भी तुम्हारा साथ गूगल मैप्स एपीआई को बदलने के लिए मत भूलना:

<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap" async="defer"></script> 

आशा इस यदि आपको मदद की!

संबंधित मुद्दे