2013-11-28 2 views
1

특정 주소 집합이있는 데이터베이스가 있습니다. Google지도 API를 사용하여 반경 내에있는 모든 주소를 검색하고 마커를 사용하고 싶습니다.Google지도를 사용하여 반경 내에서 플롯 점

내가 PHP에서 무엇입니까 데이터는 다음과 같습니다, 내가를 추가 할 수 없습니다입니다 그러나

<!-- Snippet to convert Address to Geo code using Google APIs Starts --> 
<?php 
    $completeAddress = $row['address1'].",".$row['city'].",".$row['state'].",".$row['zipcode']; 
    $httpGetCallUrl = "http://maps.googleapis.com/maps/api/geocode/json?address=" . urlencode($completeAddress) . "&sensor=false"; 
    $curlObject = curl_init(); 
    curl_setopt($curlObject, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($curlObject, CURLOPT_URL, $httpGetCallUrl); 
    $resultGeocodeJson = curl_exec($curlObject); 
    $json = json_decode($resultGeocodeJson, true); 
    $volunteerLat = $json['results'][0]['geometry']['location']['lat']; 
    $volunteerLng = $json['results'][0]['geometry']['location']['lng']; 
    curl_close($curlObject); 
?> 

: 내가 사용하는 주소/경도 위도를 만드는 오전

<tr> 
    <td><?php echo $row['firstName']; echo " "; echo $row['lastName'] ?></td> 
    <td><?php echo $row['location'];?></td> 
    <td><?php echo $row['city'];?></td> 
    <td><?php echo $row['state'];?></td> 
    <td><?php echo $row['zipcode'];?></td> 
    <td><?php echo $row['phoneNumber'];?></td> 
</tr> 

을 반경별로 검색하고 해당 반경 내에서 마커를 플롯 할 수 있습니다. 마커를 추가 할 수

코드는 다음과 같습니다

function getMarkers() { 
    var contentString = "<?php echo $row['firstName'];?>" + " " + "<?php echo $row['lastName'];?>"; 
    var myLatlng = new google.maps.LatLng(<?php echo $volunteerLat;?>, <?php echo $volunteerLng;?>); 
    //Map Marker Object initialization 
    var marker = new google.maps.Marker({ 
     position: myLatlng, 
     map: map 
    }); 
    //Adding map marker content 
    var infowindow = new google.maps.InfoWindow({ 
     content: contentString 
    }); 
    //Event listner for map marker 
    google.maps.event.addListener(marker, 'click', function() { 
     infowindow.open(map,marker); 
    }); 
} 
+0

질문을 명확히 해주시겠습니까? 어떤 주소로 검색하길 원하십니까? 데이터베이스에서 어떤 데이터를 검색합니까? – sabotero

+0

@sabotero 내 DB에서 주소를 검색하고 있습니다. 그들은 캘리포니아의 적절한 위치입니다. 여기에서 일하십시오 http://sewafs.org/volunteer/ – CodeMonkey

+0

그래서 반경은 대략 무엇입니까? – sabotero

답변

1

이미 사용자가 입력 한 주소의 좌표가 있다고 가정 (당신은 달성 할 수 google.maps.Geocoder와)과 반경.

google.maps.geometry.spherical librarycomputeDistanceBetween 방법을 사용하면 사용자가 입력 한 주소와 다른 위치 (마커) 사이의 거리를 알 수 있습니다.

다음 예제 코드에서는 데이터베이스의 위치가 이미로드되어 있고 표식이 생성되어 markers 배열에 저장되어 있다고 가정합니다. 사용자가 입력 한 주소에 대한 LatLng이 계산되어 address 변수에 저장되므로 radius 변수의 반지름도 마찬가지입니다.

// markers already bound to the map. 
var markers; 
// LatLng calculated with Geocoder from the address entered by the user. 
var address; 
// radius (in meters) entered by the user. 
var radius; 

function MarkerIsInCircle(marker){ 
    var position = marker.getPosition(), 
     // distance in meters between the marker and the address 
     distance = google.maps.geometry 
         .spherical.computeDistanceBetween(position, address); 

     return radius >= distance; 
} 
function UpdateMarkersVisibility(){ 

    for(var i=0; i < markers.length; i++){ 
     // show the markers in the circle. Hide the markers out the circle. 
     markers[i].setVisible(MarkerIsInCircle(markers[i])); 
    } 
} 

당신은 사용자 검색 버튼을 클릭하면 완료 한 그가 입력 한 주소에 대한 LatLng을 계산하는 경우 UpdateMarkersVisibility 메소드를 호출해야합니다.

참고 :이 코드는 테스트되지 않았지만 제대로 작동하고 원하는 결과를 얻을 수 있습니다.

관련 문제