2013-02-28 2 views
2

Может кто-нибудь скажет мне, как добавить список местоположений в Великобритании на карту Google, а затем использовать поиск по ближайшему адресу в почтовом индексе.почтовый индекс поиск ближайшего местоположения

У меня есть google api, может ли кто-нибудь помочь?

Большое спасибо

UPDATE: Большое спасибо за ваши ответы.

У меня есть файл CSV с адресами, возможно ли получить геокод для каждого адреса на лету и заполнить мою карту google?

В настоящее время я запрашиваю свою базу данных, чтобы получить сохраненный геокод.

Вот мой пример:

http://www.mharrisweb.co.uk/maps/map.html

ответ

0

Я предполагаю, что вы хотите добавить маркеры, когда вы говорите, «Добавить список местоположений» так, чтобы сделать это, вы можете сделать петлю и добавить для каждого укажите маркер.

Вы можете сделать что-то подобное:

function addMarker (locations){ 
    for(var i =0; i< locations.length; i ++) 
    { 
     // Create a new location 
     var loc = new google.maps.LatLng(locations[i].latitude, locations[i].longitude); 

     // Create the marker options 
     var markerOptions = { 
      map: yourMap, 
      position: loc 
     }; 
     // Add the marker on map 
     var marker = new google.maps.Marker(markerOptions); 
    } 
} 

И у вас есть все ваши места на вашей карте.

Вы сказали, что хотите выполнить поиск в почтовом индексе, чтобы найти ближайший.

Так что я нахожу это link, что может вас заинтересовать.

Надеюсь, что это поможет.

2
/*jslint browser: true */ 
/*global google */ 
function toRad(value) { 
    "use strict"; 
    /** Converts numeric degrees to radians */ 
    return value * (Math.PI/180); 
} 

function getDistanceFromLatLonInKm(lat1, lon1, lat2, lon2, unit) { 
    "use strict"; 
    var a, c, d, R, dLat, dLon; 
    R = (unit === 'mi') ? 3958.75587 : (unit === 'ft') ? 20902230.9711286 : 6371; // Mean radius of the earth in km 
    dLat = toRad(lat2 - lat1); // toRad below 
    dLon = toRad(lon2 - lon1); 
    a = Math.sin(dLat/2) * Math.sin(dLat/2) + Math.cos(toRad(lat1)) * Math.cos(toRad(lat2)) * Math.sin(dLon/2) * Math.sin(dLon/2); 
    c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); 
    d = R * c; // Distance in km 
    return d; 
} 

function calcDistance(checkCoords, lat, lng) { 
    "use strict"; 
    var closestLatLng, distance = 7000, i, latlng; 
    for (i = 0; i < checkCoords.length; i = i + 1) { 
     latlng = checkCoords[i].split(","); 
     if (getDistanceFromLatLonInKm(latlng[0], latlng[1], lat, lng) < distance) { 
      distance = getDistanceFromLatLonInKm(latlng[0], latlng[1], lat, lng); 
      closestLatLng = [latlng[0], latlng[1]]; 
     } 
    } 
    return closestLatLng; 
} 

function addMarker(lat, lng, title, map) { 
    "use strict"; 
    var marker = new google.maps.Marker({ 
     position: new google.maps.LatLng(lat, lng), 
     title: title 
    }); 
    marker.setMap(map); 
    return (lat + ',' + lng); 
} 

function show_map(lat, lng) { 
    "use strict"; 
    var checkCoords = [], closestLatLng, latlng, map, myOptions; 
    latlng = new google.maps.LatLng(lat, lng); 
    myOptions = { 
     zoom: 10, 
     center: latlng, 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
    }; 
    map = new google.maps.Map(document.getElementById("map_container"), myOptions); 
    checkCoords[0] = addMarker(51.477118, -0.000732, 'Royal Observatory, Greenwich, London', map); 
    checkCoords[1] = addMarker(38.92126, -77.066442, 'US Naval Observatory, Washington, DC', map); 
    checkCoords[2] = addMarker(48.853499, 2.348090, 'Notre Dame Cathedral, Paris', map); 
    closestLatLng = calcDistance(checkCoords, lat, lng); 
    map.panTo(new google.maps.LatLng(closestLatLng[0], closestLatLng[1])); 
} 

function getLatLng(postcode) { 
    "use strict"; 
    var geocoder = new google.maps.Geocoder(); 
    geocoder.geocode({ 
     'address': postcode 
    }, function (results, status) { 
     if (status === google.maps.GeocoderStatus.OK) { 
      show_map(results[0].geometry.location.lat(), results[0].geometry.location.lng()); 
     } 
    }); 
} 

function get_location() { 
    "use strict"; 
    document.getElementById('map').innerHTML = '<form id="postcodeEntry" method="post" action="#" onsubmit="getLatLng(this.postcode.value);return false;">' 
     + '<fieldset>' 
     + '<label for="postcode">Enter your postcode for directions</label>' 
     + '<input type="text" name="postcode" id="postcode" />' + '<input type="submit" />' 
     + '</fieldset>' 
     + '</form>' 
     + '<div id="map_container" style="width:300px;height:300px">Map will appear after postcode entry</div>'; 
} 

get_location(); 

* изм пройти JSLint

Это основной вид, что вы после этого. Вы можете увидеть это в действии на этом (обновленном) jsfiddle.

Смежные вопросы