I'm stuck and need help. I'm building a app which uses the Google Maps API and I decided to use Angular because I enjoy it's instant querying feel. But, I ran into a problem where I need to pass the values of the destination chosen (which was spit out in angular) to my already made javascript function (which would then give me the turn by turn directions I need). Been stuck for a hour, and any help would be appreciated. I tried using ng-click and making my function $scope.go = function (value) {} but still no luck. I tried getting the element chosen by name and it's text value associated, but it only returns to me the FIRST value in the list, not the specific one I clicked on. . Thanks again
Function:
function go(value)
{
val = document.getElementById('name').text;
alert(val);
var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
directionsDisplay = new google.maps.DirectionsRenderer();
var mapOptions = {
zoom: 7,
center: new google.maps.LatLng(41.850033, -87.6500523)
};
var map = new google.maps.Map(document.getElementById('googleMap'),
mapOptions);
directionsDisplay.setMap(map);
directionsDisplay.setPanel(document.getElementById('side'));
var request = {
origin: new google.maps.LatLng(41.850033, -87.6500523),
destination: new google.maps.LatLng(33.4625,-88.8200),
travelMode: google.maps.TravelMode.DRIVING
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
}
});
}
HTML:
<table>
<tr>
<td>
<input ng-model ="filterKey">
<ul ng-repeat = "location in locations | filter:filterKey "> <a id='name', onclick = "go(location.name)" > go {{location.name}} </a> <br> {{location.phone}} <br> {{location.addr}} <br> <br>
</ul></ul>
</td>
full code:
<!DOCTYPE html>
<html ng-app>
<head>
<script src="http://maps.googleapis.com/maps/api/js">
</script>
<link rel="stylesheet" type="text/css" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" />
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.13/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script>
var initialLocation = null;
var myCenter = new google.maps.LatLng(33.4625,-88.8200);
function initialize()
{
var mapProp = {
center: myCenter,
zoom:11,
mapTypeId:google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("googleMap"),mapProp);
var myCity = new google.maps.Circle({
center: myCenter,
radius:20000,
strokeColor:"#0000FF",
strokeOpacity:0.8,
strokeWeight:2,
fillColor:"#0000FF",
fillOpacity:0.4
});
var locations = [
['Harveys','33.4554015','-88.814941','food_pointer.jpg' ],
['The Veranda','33.4526087', '-88.8194257','food_pointer.jpg' ],
['Casa Bravo Mexican','33.4532565', '-88.8101641','food_pointer.jpg' ],
['Papa Johns','33.4565651', '-88.8135248','food_pointer.jpg' ],
['Umi Hibachi','33.4515882', '-88.822193','food_pointer.jpg' ],
['Christy"s Hamburgers','33.454202', '-88.8312274','food_pointer.jpg' ],
['Subway','33.4530025', '-88.8174516','food_pointer.jpg' ],
['Petty"s BBQ','33.4530025', '-88.8174516','food_pointer.jpg' ],
];
for(i = 0; i < locations.length; i++)
{
var marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1],locations[i][2]),
icon: locations[i][3] });
marker.setMap(map);
};
myCity.setMap(map);
}
var browserSupportFlag = new Boolean();
if(navigator.geolocation) {
browserSupportFlag = true;
alert("location Tracked");
navigator.geolocation.getCurrentPosition(function(position) {
initialLocation = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
alert(initialLocation);
map.setCenter(initialLocation);
}, function() {
handleNoGeolocation(browserSupportFlag);
});
}
// Browser doesn't support Geolocation
else {
browserSupportFlag = false;
handleNoGeolocation(browserSupportFlag);
}
function handleNoGeolocation(errorFlag) {
if (errorFlag == true) {
alert("Geolocation service failed.");
} else {
alert("Your browser doesn't support geolocation. We've placed you in Siberia.");
}
map.setCenter(initialLocation);
}
$scope.go = function(value)
{
val = document.getElementById('name').text;
alert(val);
var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
directionsDisplay = new google.maps.DirectionsRenderer();
var mapOptions = {
zoom: 7,
center: new google.maps.LatLng(41.850033, -87.6500523)
};
var map = new google.maps.Map(document.getElementById('googleMap'),
mapOptions);
directionsDisplay.setMap(map);
directionsDisplay.setPanel(document.getElementById('side'));
var request = {
origin: new google.maps.LatLng(41.850033, -87.6500523),
destination: new google.maps.LatLng(33.4625,-88.8200),
travelMode: google.maps.TravelMode.DRIVING
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
}
});
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<ul ng-init=" locations = [
{name: 'Harveys', type: 'American', special: 'Classic Dining' , phone: '111-222-3333', addr: '121 Cashview Ave',lat: '33.4554015', long: '-88.814941', i: '1'},
{name: 'The Veranda', type: 'American' , special: 'Neo Dining' , phone: '111-222-3333' , addr: '121 Cashview Ave'},
{name: 'Casa Bravo Mexican', type: 'Meixcan', special: 'Meixcan Dining' , phone: '111-222-3333' , addr: '121 Cashview Ave'},
{name: 'Papa Johns', type: 'American', special: 'Fast Food Pizza delivery' , phone: '111-222-3333', addr: '121 Cashview Ave'},
{name: 'Umi Hibachi', type: 'American', special: 'Japanese Dining', phone: '111-222-3333', addr: '121 Cashview Ave'},
{name: 'Christys Hamburgers', type: 'American', special: 'Fast Food Burgers' , phone: '111-222-3333', addr: '121 Cashview Ave'},
{name: 'Subway', type: 'American', special: 'Fast Food Sandwiches subs', phone: '111-222-3333', addr: '121 Cashview Ave'},
{name: 'Pettys BBQ', type: 'American', special: 'Fast Food barbecue southern', phone: '111-222-3333', addr: '121 Cashview Ave'}
]"> </ul>
<table>
<tr>
<td>
<input ng-model ="filterKey">
<ul ng-repeat = "location in locations | filter:filterKey "> <a id='name', ng-click = "go(location.name)" > go {{location.name}} </a> <br> {{location.phone}} <br> {{location.addr}} <br> <br>
</ul></ul>
</td>
<center>
<td id="googleMap" align = "right" style="width:1200px;height:500px;;">
</td>
</center>
<td id= "side"> </td>
</tr>
</body>
</html>
onclick, useng-click-$scope.go = function(value) { your code };thenng-click="go(location.name)"should work