1

I am using googlePlace api in angularJs and I want to change type of places dynamically as needed. Like I use controllers to bind the values in view part using $scope but it's not working in this situation also tried $rootScope.

Tried many other things too but they all are not working and I'm also new to angularJs so don't know much.

Here is code:-

app.config(function(ngGPlacesAPIProvider){
  ngGPlacesAPIProvider.setDefaults({
    radius:1000000,
	types:['electronics_store','bakery','bank','beauty_salon','bicycle_store','book_store','cafe','car_dealer','car_wash','car_repair','clothing_store','dentist','department_store'],
	nearbySearchKeys: ['name','geometry', 'reference'],
	placeDetailsKeys: ['name','formatted_address', 'formatted_phone_number',
        'reference', 'website', 'geometry', 'email'],
  });
});

Controller code:-

  app.config(function(ngGPlacesAPIProvider, ngGPlacesDefaults){
	  ngGPlacesAPIProvider.setDefaults(ngGPlacesDefaults);
	});

app.controller('scdfindCustomerCtrl',function($scope,ngGPlacesAPI,$http,ngGPlacesDefaults){

	  ngGPlacesDefaults.types = ["atm"];
	
	$scope.getDetails = function(ref){
		$scope.details = ngGPlacesAPI.placeDetails({reference:ref}).then(
			    function (data) {
			    	$scope.det=data;
			      console.log(data);
			      return data;
			    });
	}
	
	$scope.positions = [{lat:37.7699298,lng:-122.4469157}];
	
	$scope.addMarker = function(event) {
    var ll = event.latLng;
		
    $scope.positions.push({lat:ll.lat(), lng: ll.lng()});
  
	
	$scope.data = ngGPlacesAPI.nearbySearch({latitude:ll.lat(), longitude:ll.lng()}).then(
    function(data){
		$scope.person=data;
		console.log(data);
	  return data;
    });
}

So basically I want to change "types:[]" array from view part.

Any help would be appreciated.

2 Answers 2

2

You could have those set of default values as an constant, so that you could get those value inside config phase directly, as angular constants are accessible over there & in all other component of angular like controller, factory, directive, etc just by injecting dependency of it.

Constant

app.constant('ngGPlacesDefaults', {
    radius:1000000,
    types:['electronics_store','bakery','bank','beauty_salon','bicycle_store','book_store','cafe','car_dealer','car_wash','car_repair','clothing_store','dentist','department_store'],
    nearbySearchKeys: ['name','geometry', 'reference'],
    placeDetailsKeys: ['name','formatted_address', 'formatted_phone_number',
        'reference', 'website', 'geometry', 'email'],
  });
})

Config

app.config(function(ngGPlacesAPIProvider, ngGPlacesDefaults){
  ngGPlacesAPIProvider.setDefaults(ngGPlacesDefaults);
});

Whenever you wanted to change the value of ngGPlacesDefaults configuration, you can have handle to those value by injecting ngGPlacesDefaults dependency

app.controller('myController', function($scope, ngGPlacesDefaults){
  //other code here

  ngGPlacesDefaults.types = ["some", "different", "values"]; //you could change value like this
})
Sign up to request clarification or add additional context in comments.

5 Comments

I added that "constant part" in my app.js file and left "types:[] array " blank. now I am trying to set the value of types array in my controller let's say type: "atm" but it's not showing list of atms.
@Anonymous you need to follow the same step which I did in answer..It would be something like ngGPlacesDefaults.types = ["atms"];
It seems like It's loading the values before initialisation in controller.
@Anonymous can you add some code..how you are calling ngGPlacesAPI methods inside your controller
0

I found a different way to solve this problem. You don't need to extra just pass the types:[] as an argument in 'nearBySearch' function

$scope.data = ngGPlacesAPI.nearbySearch({latitude:ll.lat(), longitude:ll.lng(), types:[$scope.business.selected.businessType]}).then(
    function(data){
		$scope.person=data;
		console.log(data);
	  return data;
    });

"$scope.business.selected.businessType" is bindable dynamically from view, that's it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.