language parameter in geocoder.geocode()? - javascript

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.

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.

Can't find point of interest with geocoding

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

Yandex Map - Get traffic data

I try to get traffic data at a spesific time and place using yandex maps. I look this page( api of yandex maps ). And I can show traffic data on my own map. Using geocoding, I extract some data(place names, coordinates etc) from yandex maps. But I don't know how I extract only traffic data. How can I do it? Is there any function on yandex map api?
My simple code that shows traffic data on map is below
ymaps.ready(init);
var myMap,
myPlacemark;
function init(){
myMap = new ymaps.Map ("mapId", {
center: [28.968484, 41.01771],
zoom: 7
});
myPlacemark = new ymaps.Placemark([28.968484, 41.01771], {
content: 'Moscow!',
balloonContent: 'Capital of Russia'
});
// This page should show traffic and the "search on map" tool
ymaps.load(['package.traffic', 'package.search'], addControls);
myMap.geoObjects.add(myPlacemark);
}
function addControls(map) {
myMap.controls.add('trafficControl').add('searchControl');
var trafficControl = new ymaps.control.TrafficControl({shown: true});
map.controls.add(trafficControl);
function updateProvider () {
trafficControl.getProvider('traffic#actual').update();
}
// We will send a request to update data every 4 minutes.
window.setInterval(updateProvider, 1 * 60 * 1000);
}
There is no legal way to do it. There are some notes at forums, where people use internal api of Yandex maps, but they as far as I know rapidly banned by Yandex team. Yandex does not provide you any traffic data separatly from maps. I recommened you to take a look at Google Maps Api, there you could get traffic info on route your have specified. Example:
final String baseUrl = "http://maps.googleapis.com/maps/api/directions/json";// path to Geocoding API via http
final Map<String, String> params = new HashMap<String, String>();
params.put("sensor", "false");// if data for geocoding comes from device with sensor
params.put("language", "en");
params.put("mode", "driving");// move mode, could be driving, walking, bicycling
params.put("origin", "Russia, Moscow, Poklonnaya str., 12");// start point of route as address or text value of lon and lat
params.put("destination", "Russia, Moscow, metro station Park Pobedy");// the same for end point
final String url = baseUrl + '?' + encodeParams(params);// generate final url
final JSONObject response = JsonReader.read(url);// perform request and read answer where we could also get coordinates
//results[0]/geometry/location/lng and results[0]/geometry/location/lat
JSONObject location = response.getJSONArray("routes").getJSONObject(0);
location = location.getJSONArray("legs").getJSONObject(0);
final String distance = location.getJSONObject("distance").getString("text"); // get distance for route
final String duration = location.getJSONObject("duration").getString("text"); // get duration for route
For more info just take a look at https://developers.google.com/maps/

Google maps api weather layer and geolocating

I'm currently trying to use google maps to first display a map of United States with the weather and cloud google map layers and then zoom into the user's location using geolocation google maps api. My problem right now is I am only able to display the map without any of the weather, cloud, or geolocation information and only using the iframe basic view mode maps api. I am fairly new to javascript but have a div with an id "map-canvas" in my body tags and a javascript file mapWeather.js in my head tags.
mapWeather.js:
function initialize() {
var mapOptions = {
zoom: 4,
center: new google.maps.LatLng(37.6,-95.665)
};
var map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);
var weatherLayer = new google.maps.weather.WeatherLayer({
temperatureUnits: google.maps.weather.TemperatureUnit.FAHRENHEIT
});
weatherLayer.setMap(map);
var cloudLayer = new google.maps.weather.CloudLayer();
cloudLayer.setMap(map);
}
google.maps.event.addDomListener(window, 'load', initialize);
You probably already know, but do note that this library has been deprecated and will stop working in June.

Google Maps API: Get url for current view

I have a form where users can introduce google maps urls to specify the address of some stuff.
I've been thinking in showing a map through Google Map API v3, letting user move to desired location and through a button or something automatically get the url of the place and copy it to an input.
I've been able to display the map using the tutorial, but I haven't been able to find in the documentation how I could get the url...
I think you will not need it, but this is the simple code I'm using:
var latlng = new google.maps.LatLng(-34.397, 150.644);
var options = {
zoom: 8,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP,
mapTypeControl: false
};
var map = new google.maps.Map(document.getElementById('map-box'), options );
I'd suggest using map.getCenter().toUrlValue() and map.getZoom() to obtain the centre and zoom state of the current map view. That information should let you build a URI that you can then use, bare in mind that you'll need to write some code to take the values off the URI and pass them to the map API.

Categories

Resources