Can't find point of interest with geocoding - javascript

I have a web app where I use javascript google geocoder:
var geocoder = new google.maps.Geocoder();
geocoder.geocode({ 'address': from }, function(results, status) {
var from_lat = results[0].geometry.location.lat();
var from_lng = results[0].geometry.location.lng();
});
It finds almost any address, but if I search for some points of interest, I get: ZERO_RESULTS in status.
For example:
"Azrieli Center, Derech Menachem Begin, Tel Aviv-Yafo, Israel"
How can I change my code in order to find this kind of place?

Businesses are excluded from Geocoding API, so you have to use a places library of Maps JavaScript API in order to find POI like this one.
Please have a look at this example on jsbin: http://jsbin.com/jayabe/edit?html,output

Related

How to make Google Maps detect location of a passed Google Maps URL

Is there any way to put the longitude and latitude from a passed URL into a marker. So essentially a user would copy and paste the 'Share' URL from Google maps.
E.G. Places: https://www.google.co.nz/maps/place/The+White+House/#38.8976763,-77.0387238,17z/data=!3m1!4b1!4m5!3m4!1s0x89b7b7bcdecbb1df:0x715969d86d0b76bf!8m2!3d38.8976763!4d-77.0365298?hl=en
or Direct Location:
https://www.google.co.nz/maps/place/38%C2%B054'53.8%22N+77%C2%B006'01.6%22W/#38.914936,-77.102638,17z/data=!3m1!4b1!4m5!3m4!1s0x0:0x0!8m2!3d38.914936!4d-77.100444?hl=en
I would like the initialisation code create a marker at that shared URL location.
So far from other question's I've seen the use of GeoCode API but I'm not sure how the example URL's above can be parsed and the data extracted in JS. Any examples of Ajax calls to API or something like this being done would be appreciated.
The URLs contain the latitude and longitude, so you can extract them easily. Calling putMarkerAtURLCoordinates(SHARED_URL) will place a marker at the coordinates of the url assuming your google maps instance is called "map".
function putMarkerAtURLCoordinates(url){
//get the latlng object from url
var myLatLng = getLatLngFromURL(url)
//create the marker and assign it to the map
var marker = new google.maps.Marker({
position: myLatLng,
map: map,
title: 'Marker title here'
});
}
function getLatLngFromURL(url){
//decode url incase its urlencoded
url = decodeURIComponent(url)
//find index of # character
var atindex = url.indexOf("#")
//cut off the part before the # and split the rest up by the comma separator
var urlparts = url.slice(atindex+1).split(",")
//parse the values as floats
var lat = parseFloat(urlparts[0])
var lng = parseFloat(urlparts[1])
//return as object
return {lat:lat,lng:lng}
}
This isn't a stable solution though... If google changes their URL scheme this won't work anymore.

language parameter in geocoder.geocode()?

I am trying to fetch the address components using Google Maps APIs.
geocoder = new google.maps.Geocoder();
marker = new google.maps.Marker(...);
geocoder.geocode({'latLng': marker.getPosition()}, function(...);
But for some cases when I drag the marker to point to exact location, I get few parts of address_components in response of GeoCoding API in localised versions even though the map language is set to "en" (English).
Is there any way I can pass in language param while creating Geocoder instance to force it to return address components in english only.
NOTE: I am not using geocoding webservice to get the address_components.

Show current user location in Google Maps (JavaScript)

I want to be able to show the current user location, and to automatically put a pin where the user is located. I need this in HTML/JavaScript, full code. Please help.
You've to use the Google API (JavaScript in this case) to show current location in maps. You've to import the library with:
<script src="https://maps.googleapis.com/maps/api/js?signed_in=true&callback=initMap" async defer>
And use the function initMap:
var map = new google.maps.Map(document.getElementById('map'), {
center: {lat: initLat, lng: initLon},
zoom: 6
});
When you obtain lat and lon from the navigation.geolocation, you've to update the map with map.setCenter(pos);, when pos is an array with lat and lon.
Here you've a full example.
Google official doc
I hope it helps :-)

output web page as text only from javascript

I have an ASP.NET page using a master page. On that I have a script, what I would like to do is to rewrite the output of the HTML to just give me a street name without all the HTML headers etc.
Here's the script, can you please advise on how to do this?
function getAddress(Latitude, Longitude) {
var geocoder = new google.maps.Geocoder();
var Lat = parseFloat(Latitude);
var Lng = parseFloat(Longitude);
var latlng = new google.maps.LatLng(Lat, Lng);
geocoder.geocode({ 'latLng': latlng }, function (results, status) {
if (status === google.maps.GeocoderStatus.OK) {
// document.clear();
document.open();
document.write(results[0].formatted_address);
document.close();
}
else {
// Do nothing
document.open();
document.write('');
document.close();
}
geocoder = null;
});
}
I don't have to use the master page (I might try it without after I post this question).
I know document.write is considered bad practice but above is only an example of what I'm trying to do.
I will be calling this page from my VB.NET application using HTTP request etc. so all I want back is a plain text file with the street name.
When you will load your page with ASP.NET, javascript will not be executed.
Moreover, you can't change the type of your page with javascript.
You may find another solution, a nicer one please :-)

Google Maps API v3 - changed in the last week or so?

I was using the following code to obtain the lat and lng values from user input. It seemed to be working fine, until just recently.
if (status == google.maps.GeocoderStatus.OK) {
var lat = parseFloat(results[0].geometry.location.wa).toFixed(3);
var lng = parseFloat(results[0].geometry.location.xa).toFixed(3);
....
Now if I console.log results[0].geometry.location I get (51.4793388, -2.5933342) { va=51.4793388, wa=-2.5933342}.
It appears as if xa has changed to va. What is the correct way to reference these values?
I recently ran into the same issue on my Google Map API 3.0 application. Basically, the wa and xa variables if i remember correctly are just LatLng() variables. So you can call them this way:
results[0].geometry.location.lat().toFixed(3);
results[0].geometry.location.lng().toFixed(3);
where va = lat and wa = lng

Categories

Resources