简体   繁体   中英

Google Places API not working with getCurrentPosition Geolocation in Ionic app

Trying to get Google Places API to notice my location and apply the functions below. Very new to this and not sure what I am doing wrong below as the API and all the functions works initially, but after I'm asked for my location, it shows where I am but nothing else works/aren't working together.

Cordova Geolocation plugin I added to my ionic app:

cordova plugin add https://git-wip-us.apache.org/repos/asf/cordova-plugin-geolocation.git

App.js

 app.controller("MapController", function($scope, $ionicLoading) { var map; var infowindow; var request; var service; var markers = []; google.maps.event.addDomListener(window, 'load', function() { var center = new google.maps.LatLng(42.3625441, -71.0864435); var mapOptions = { center:center, zoom:16 }; map = new google.maps.Map(document.getElementById('map'), mapOptions); navigator.geolocation.getCurrentPosition(function(pos) { map.setCenter(new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude)); var myLocation = new google.maps.Marker({ position: new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude), map: map, title: "My Location" }); }); $scope.map = map; request = { location: center, radius: 1650, types: ['bakery', 'bar'] }; infowindow = new google.maps.InfoWindow(); var service = new google.maps.places.PlacesService(map); service.nearbySearch(request, callback); function callback (results, status) { if (status == google.maps.places.PlacesServiceStatus.OK) { for (var i = 0; i < results.length; i++) { createMarker(results[i]); } } } function createMarker(place){ var placeLoc = place.geometry.location; var marker = new google.maps.Marker({ map: map, position: place.geometry.location }); google.maps.event.addListener(marker, 'click', function(){ infowindow.setContent(place.name); infowindow.open(map,this); }); } }); }); 

When you get the position, you update the map's location, but you don't run a new Nearby Search.

So I think you want to call service.nearbySearch(...) in the getCurrentPosition callback.

You may also want to have your nearbySearch callback keep an array of the markers created via createMarker , so you can remove them when you run a new search (eg by calling marker.setMap(null) on each old marker).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM