2010-08-16 15 views
5

मैं Google मानचित्र में इस प्रभाव को प्राप्त करने के तरीके के बारे में प्रासंगिक जानकारी खोजने के लिए संघर्ष कर रहा हूं। मुझे उम्मीद थी किसी को इस विषय पर मुझसे ज्यादा जानता है और सही दिशा में मुझे बिंदु सकता है ..Google मानचित्र - पोस्टकोड प्लॉटिंग

समस्या:

  • मैं एक पोस्टकोड के माध्यम से एक नक्शे पर पिन के एक्स राशि प्लॉट करने के लिए की जरूरत है।
  • दूसरा चरण अपना खुद का डाक कोड और उत्पाद दर्ज करना है जो चरण एक में प्लॉट किए गए निकटतम 5 पोस्टकोडों की एक सूची है।

उत्तर

18

यह कोड आपको बॉक्स में दर्ज किए गए एक को सबसे नज़दीकी ज़िपकोड बताएगा। मुझे यकीन है कि यहां से आप शीर्ष 5 परिणामों को देने के लिए इसे अनुकूलित कर सकते हैं।

<!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: Geocoding Simple</title> 
<link href="http://code.google.com/apis/maps/documentation/javascript/examples/standard.css" rel="stylesheet" type="text/css" /> 
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script> 
<script type="text/javascript"> 
    var geocoder; 
    var map; 
    var markers = []; 
    function initialize() { 
     geocoder = new google.maps.Geocoder(); 
     var latlng = new google.maps.LatLng(40.768505,-111.853244); 
     var myOptions = { 
      zoom: 8, 
      center: latlng, 
      mapTypeId: google.maps.MapTypeId.ROADMAP 
     } 
     map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); 
     addPostCode('84101'); 
     addPostCode('84102'); 
     addPostCode('84103'); 
     addPostCode('84104'); 
     addPostCode('84105'); 
    } 

    function addPostCode(zip) { 
     geocoder.geocode({ 'address': zip}, function(results, status) { 
     if (status == google.maps.GeocoderStatus.OK) 
     { 
      map.setCenter(results[0].geometry.location); 
      var marker = new google.maps.Marker({ 
      map: map, 
      position: results[0].geometry.location, 
      name: zip 
     }); 
     markers.push(marker); 
     } else { 
      alert("Geocode was not successful for the following reason: " + status); 
     } 
     }); 
    } 

    function checkZip(zip) 
    { 
     var distance = Number.MAX_VALUE; 
     var index = 0; 
     geocoder.geocode({ 'address': zip}, function(results, status) 
     { 
      if (status == google.maps.GeocoderStatus.OK) 
      { 
       for(ix=0; ix< markers.length; ix++) 
       { 
        var tmp = getDistance(results[0].geometry.location, markers[ix].position); 
        if (tmp < distance) 
        { 
         distance = tmp; 
         index = ix; 
        } 
       } 
       alert('nearest zipcode is :' + markers[index].name); 
      } 
     }); 
    } 

    function getDistance(latlng1, latlng2) 
    { 
     var R = 6371; // Radius of the earth in km 
     var dLat = (latlng2.lat()-latlng1.lat()) * Math.PI/180; // Javascript functions in radians 
     var dLon = (latlng2.lng()-latlng1.lng()) * Math.PI/180; 
     var a = Math.sin(dLat/2) * Math.sin(dLat/2) + 
      Math.cos(latlng1.lat() * Math.PI/180) * Math.cos(latlng2.lat() * Math.PI/180) * 
      Math.sin(dLon/2) * Math.sin(dLon/2); 
     var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
     var d = R * c; // Distance in km 
     d = d * 0.621371192; // convert to miles 
     return d; 
    } 
</script> 
</head> 
<body onload="initialize()"> 
    <div> 
    <input id="address" type="textbox" value=""> 
    <input type="button" value="Geocode" onclick="checkZip(getElementById('address').value)"> 
    </div> 
<div id="map_canvas" style="height:90%"></div> 
</body> 
</html> 
+0

धन्यवाद इस के लिए, अपने बहुत उपयोगी .. ब्रिटेन पोस्टकोड क्षमा करें जब मैं कहना पोस्टकोड, मेरा मतलब .. :) बीमार डॉक्स थोड़ा और ऊपर देखो:

मैं अनुकूल करने के लिए कर रहा था Geocoding sample code अपने आधार का उपयोग कर। – Lee

+0

एनएम यह यूके पोस्टकोड के साथ काम करता है .. फिर से मदद के लिए धन्यवाद .. आपने मूल रूप से इसे मेरे लिए किया है .. – Lee

+0

मुझे लगता है कि यह किसी भी पोस्टकोड के साथ काम करेगा जो Google पहचानता है। –

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