I have seen previous posts regarding the conversion from a Google Place ID to an address; however I am interested in the opposite.
I have the GMS Address of the desired location but I want to acquire the Google Place ID in order to present more details to the user. From what I have seen on Google's iOS API website, you can attain a Google Place ID from an Autocomplete feature (for user searching), a selected location feature, or a current location feature.
My application intends to display multiple locations in which I already have the addresses for. The user can select individual locations for more details in addition to receiving details upon arrival. Therefore the features listed above are not ideal.
I have also tried using Google's Web API with features such as "Text Search Requests" & "Nearby Search Requests" however I am receiving "Zero Results".
Are there other methods that I haven't thought of and/or seen?
How about the Place ID finder in the Google Maps JavaScript API.
A place ID is a textual identifier that uniquely identifies a place. It is also available for most locations, including businesses, landmarks, parks, and intersections. These IDs are stable, meaning that once you've identified the place ID for a place, you can reuse that value when you next look up that place.
You can use the same place ID across the Google Places API and a number of Google Maps APIs. For example, you can use the same place ID to reference a place in the Places API, the Google Maps JavaScript API, the Google Maps Geocoding API, the Google Maps Embed API and the Google Maps Roads API.
Here is the sample code use for this.
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
center: {lat: -33.8688, lng: 151.2195},
zoom: 13
});
var input = document.getElementById('pac-input');
var autocomplete = new google.maps.places.Autocomplete(input);
autocomplete.bindTo('bounds', map);
map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);
var infowindow = new google.maps.InfoWindow();
var marker = new google.maps.Marker({
map: map
});
marker.addListener('click', function() {
infowindow.open(map, marker);
});
autocomplete.addListener('place_changed', function() {
infowindow.close();
var place = autocomplete.getPlace();
if (!place.geometry) {
return;
}
if (place.geometry.viewport) {
map.fitBounds(place.geometry.viewport);
} else {
map.setCenter(place.geometry.location);
map.setZoom(17);
}
// Set the position of the marker using the place ID and location.
marker.setPlace({
placeId: place.place_id,
location: place.geometry.location
});
marker.setVisible(true);
infowindow.setContent('<div><strong>' + place.name + '</strong><br>' +
'Place ID: ' + place.place_id + '<br>' +
place.formatted_address);
infowindow.open(map, marker);
});
}
Just take note that this example requires the Places library. Include
the libraries=places parameter when you first load the API. For
example:
script src="https://maps.googleapis.com/maps/api/jskey=YOUR_API_KEY&libraries=places"
Related
I'm using geocoder rails gem. I want to find near banks , schools , hotels from some location.
location = Geocoder.search([48.856614, 2.3522219])
From above i can get location but not near important places.
How i can use near method of geocoder on "location" object, and find near places like hotels , schools and banks etc?
Any help?
Thanks
There is no filter or search as you describe in the Geocoder gem. You can get the nearest 10, 20, 50 other geocoded objects in your database.
So in order to make it function like you are describing you would need to Geocode each Hospital, School, Bank etc. and store them in a table, and then call nearbys on your point.
ie. Bulk geocode every Bank Listing within 500km in the location you are serving and check against that.
This method would quickly go stale and become ineffective. What you need is the Google Places API. They have a JS library you can leverage to get your results.
[Google Places JS API][1]
Text Search Requests The Google Places Text Search service is a web
service that returns information about a set of places based on a
string — for example "pizza in New York" or "shoe stores near Ottawa".
The service responds with a list of places matching the text string
and any location bias that has been set. The search response will
include a list of places. You can send a Place Details request for
more information about any of the places in the response.
I use the google api extensively and have found it remarkably easy to work with. Simply make a call to the API to retrieve an array of results for say, 'restaurant'. The example below is taken directly from the Google Developer page and will plot every restaurant in the Pyrmont area that they know about. The 'results' in the callback method will be a JSON object you can utilize with JS;
var map;
var service;
var infowindow;
function initialize() {
var pyrmont = new google.maps.LatLng(-33.8665433,151.1956316);
map = new google.maps.Map(document.getElementById('map'), {
center: pyrmont,
zoom: 15
});
var request = {
location: pyrmont,
radius: '500',
query: 'restaurant'
};
service = new google.maps.places.PlacesService(map);
service.textSearch(request, callback);
}
function callback(results, status) {
if (status == google.maps.places.PlacesServiceStatus.OK) {
for (var i = 0; i < results.length; i++) {
var place = results[i];
createMarker(results[i]);
}
}
}
When using Google API what will be the data that is needed for an application that shows locations of different places,is latitude and longitude will be necessary , the locations that needed to shown is already in Google maps. Or just the address or the name of the place will be enough.
Can anybody help me ?
When placing markers on a map at a minimum you need the lat and long vertices.
You can use a various Google APIS for getting data like a Google Places library to search by a place name or address: https://developers.google.com/maps/documentation/javascript/examples/places-searchbox
You can also use a reverse geocode service to search by a lat long: https://developers.google.com/maps/documentation/javascript/examples/geocoding-reverse
Or if you have your own data you can create the markers like shown in this example:
var myLatlng = new google.maps.LatLng(36.166461, -86.771289);
app.marker = new google.maps.Marker({
position: (myLatlng),
data:this,
map: app.map,
title: 'Hello marker!!'
});
https://jsfiddle.net/loanburger/48asvsed/
I'm writing an app using PhoneGap Build, jQuery Mobile and Energize.js (to speed up clicks)
My wish is to bind an 'event' to the Google Analytics plugin for PhoneGap Build, so that I can track user clicks.
As you can see below, my tracking listener is bound to a 'touchstart' event. At the moment, none of the tracking is working. I am unsure if it is because I have bound to an incorrect action (which Energize.js may have changed) or if there is another issue in my code.
Any help would be appreciated. My account on Google Analytics is set as a 'Webpage' instead of 'Mobile App' as per the plugin guidelines.
$(document).on("touchstart", ".condition-list-item a", function() {
var deviceID = device.platform + '.' + device.uuid;
// Generates unique variable for each device
var conditionVar = $(this).attr("data-condition");
// Pulls ConditionName from list item clicked
PageButtonClicked(conditionVar);
// Fires 'Page' tracking to Google Analytics, with ConditionName as 'Page'
VariableButtonClicked(deviceID, conditionVar)
navigator.geolocation.getCurrentPosition(onSuccess);
// Creates an object 'Position' using Geolocation API
function onSuccess(position) {
var geo = position.coords.latitude + ', ' + position.coords.longitude;
}
});
Try this:
$(document).ready(function(){
$(".condition-list-item a").on("touchstart", function(e){
//your function
});
});
As follow-up, the above code is still not working. Google Analytics is currently attempting to track the information as if it were a website (as recommended by the plugin).
I change the settings on the Google Analytics website to track as if it were a Mobile App. I still think the above code is not working; however, Google Analytics' built in tracking takes care of much the same stuff and is working beautifully.
i want to get the user location information like city, area etc using html5 geolocation,i tried the below article but with that im getting latitude and longitude, i want to get the city and area information,
http://www.dotnetcurry.com/ShowArticle.aspx?ID=782
When you have the location of the user, you'll need to do a reverse geocoding to get information about the location such as city, etc.
Have a look at the reverse geocoding API for Google Maps.
navigator.geolocation.getCurrentPosition(function(pos){
var latlng = new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude);
geocoder.geocode({'latLng': latlng}, function(results, status) {
console.log(results);
// Analyze results and find the info you need.
});
});
Check out How to reverse geocode without Google for information about geocoding without using Google Maps.
I am new to html 5 geolocation, Is there any site or examples where it shows how, i can use html geolocation api to track the location of other devices. Also, for example how can i use use HTML5 geolocation to find the nearest car dealer say 5 or 10 miles from me?
Any examples sample would be really helpfull.
thanks
I can't speak to tracking devices, but i've been writing something that might help with the second part of your question.
//check browser support
if(navigator.geolocation) {
//do the geolocation stuff
navigator.geolocation.getCurrentPosition(function(position) {
//make a string out of the coordinates
var initloc_str = position.coords.latitude + ', ' + position.coords.longitude;
//pass it to a function that searches for donut shops near the coordinates
donutSearch(initloc_str);
});
} else { // if no browser support, error message or whatever
My donut shop search function is based on the local search in Google's Ajax search API, I found this article on webmonkey helpful for that bit. That API was recently deprecated but I haven't figured out how to do local search with their new Custom Search API yet.
You can register a function for tracking positions of a user:
var watchId = navigator.geolocation.watchPosition(function(position) {
console.log(position.coords.latitude);
console.log(position.coords.longitude);
});
And you can unregister:
navigator.geolocation.clearWatch(watchId);
This site will give you a awesome overview of the HTML5 geolocation Capability.