Automatically show street view map when inputting an address - google-maps

I want to put in a street address, and have it programatically determine the coordinates and heading for the purposes of generating a street view map. instantstreetview.com does this, so I know it can be done. From the docs:
function initialize() {
var fenway = {lat: 42.345573, lng: -71.098326};
var map = new google.maps.Map(document.getElementById('map'), {
center: fenway,
zoom: 14
});
var panorama = new google.maps.StreetViewPanorama(
document.getElementById('pano'), {
position: fenway,
pov: {
heading: 34,
pitch: 10
}
});
map.setStreetView(panorama);
}
Problem is, I don't know the heading of the address I want. Is there a simple way to achieve this?

You may want to look into getting the LatLng from the StreetViewLocation of the StreetViewPanorama rendered to compute the heading using the computeHeading() in the Geometry library in the Javascript API.
Take a look at this sample JSBin. I've created two Street View divs with the lat/lngs of two addresses across the street from each other. Notice that, regardless of the lat/lng, the same pano is returned, but there is no heading information, so the view is the same:
Now, look at this this modified JSBin. After computing the heading based on the StreetViewLocation.latLng of the panorama and lat/lng of the addresses passed into StreetViewService and using that as the pov for the panoramas, the correct Street View imagery is shown for each address:
The pertinent code:
var ll = new google.maps.LatLng(37.435292,-122.129517);
var svs = new google.maps.StreetViewService();
svs.getPanorama({location: ll, preference: 'nearest'}, function(data, status){
var pos = data.location.latLng;
var head = google.maps.geometry.spherical.computeHeading(pos, ll);
var panorama = new google.maps.StreetViewPanorama(
document.getElementById('street-view'),
{
pano: data.location.pano,
pov: {heading: head, pitch:0}
});
});
EDIT: Adding another simple JSBin showing the use of the Geocoder to get the LatLng for the addresses: Pertinent code:
var ll;
var geocoder = new google.maps.Geocoder();
var svs = new google.maps.StreetViewService();
geocoder.geocode({address: "757 Moreno Ave Palo Alto, CA"}, function(results, status) {
if (status == 'OK') {
ll = results[0].geometry.location;
svs.getPanorama({location: ll, preference: 'nearest'}, function(data, status){
var pos = data.location.latLng;
var head = google.maps.geometry.spherical.computeHeading(pos, ll);
panorama = new google.maps.StreetViewPanorama(
document.getElementById('street-view'),
{
pano: data.location.pano,
pov: {heading: head, pitch:0}
});
});
}
});

Related

Place Types Is showing wrong type of place on Maps API

My maps api is showing wrong places types and I can't figure out why. Se the code bellow, I've asked for:
type: ['store', 'school', 'church', 'park', 'university', 'cafe', 'gym', 'bakery']
See the entire code here:
<script>
var map;
var infowindow;
function initMap() {
var image = 'http://www.vittoriagardens.com/wp-content/uploads/2016/04/pin.png';
var myLatLng = {lat: 25.5244191, lng: -80.4063932};
map = new google.maps.Map(document.getElementById('map'), {
zoom: 15,
scrollwheel: false,
center: myLatLng
});
var principal = new google.maps.Marker({
position: myLatLng,
map: map,
title: 'Vittoria',
icon: image
});
infowindow = new google.maps.InfoWindow();
/*lojas*/
var placestore = new google.maps.places.PlacesService(map);
placestore.nearbySearch({
location: myLatLng,
radius: 2500,
type: ['store', 'school', 'church', 'park', 'university', 'cafe', 'gym', 'bakery']
}, 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 iconType = {};
iconType['school'] = "http://www.vittoriagardens.com/wp-content/uploads/2016/04/categoria_school.png";
iconType['church'] = "http://www.vittoriagardens.com/wp-content/uploads/2016/04/categoria_church.png";
iconType['park'] = "http://www.vittoriagardens.com/wp-content/uploads/2016/04/categoria_park.png";
iconType['university'] = "http://www.vittoriagardens.com/wp-content/uploads/2016/04/categoria_school.png";
iconType['store'] = "http://www.vittoriagardens.com/wp-content/uploads/2016/04/categoria_shopping_mall.png";
iconType['cafe'] = "http://www.vittoriagardens.com/wp-content/uploads/2016/04/categoria_bakery.png";
iconType['bakery'] = "http://www.vittoriagardens.com/wp-content/uploads/2016/04/categoria_bakery.png";
iconType['gym'] = "http://www.vittoriagardens.com/wp-content/uploads/2016/04/categoria_gym.png";
var marker = new google.maps.Marker({
map: map,
icon: iconType[place.types[0]],
position: place.geometry.location
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent(place.name);
infowindow.open(map, this);
});
}
</script>
I don't know why but on the Map that you can see here: Vittoria Gardens or in this image bellow:Print of the map on the website, for example, it's showing a Mexican Restaurant and a UPS Costumer center, and I didn't asked for this types of places.
Does anyone know why is this happening?
Thank you
From the documentation:
Warning: The implementation for types in text search requests is changing. The types parameter is deprecated as of February 16, 2016, replaced by a new type parameter which only supports one type per search request. Additionally, the establishment, place_of_worship, food, health, general_contractor and finance types will no longer be supported as search parameters (however these types may still be returned in the results of a search). Requests using the deprecated features will be supported until February 16, 2017, after which all text searches must use the new implementation.
While the text above specifically states "text search", it also applies to NearbySearch.
This won't work:
type: ['store', 'school', 'church', 'park', 'university', 'cafe', 'gym', 'bakery']

Google Maps API cannot process one specific address

We have a working Google Maps API (V2) module working correctly on our custom made CMS.
I cannot figure out why but this code seems to be having troubles processing one specific Address. What's weird is that the address can be found normally on Google Maps.
Here are two examples, one address that works perfectly and the address that Google Maps API cannot find :
Working Address : via Zups 2, 7013 Domat/Ems
<script>
initialize( "googlemap-viazups17013domat/ems", "via Zups 2 7013 Domat/Ems" )Marly" )
</script>
working HTML output (cleaned up)
Unworking Address : via Zups 1, 7013 Domat/Ems
<script>
initialize( "googlemap-viazups17013domat/ems", "via Zups 1 7013 Domat/Ems" )
</script>
non Working HTML output (cleaned up)
Also note that if I change the address to display Number 2 of the same street it works! But Number 1 never works.
––––
Edit :
Here is the initialize function :
function initialize( div, address ) {
var geocoder;
var map;
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(-34.397, 150.644);
var myOptions = {
zoom: 15,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP,
scrollwheel:false
}
map = new google.maps.Map(document.getElementById( div ), myOptions);
//map.disableDragging()
if (geocoder) {
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
} else {
alert("Impossible de localiser " + address);
}
});
}
}

Google places javascript library to search for places inside my database

I have a mysql database with places.Each place has a longitude/latitude.
What I want is to search for places inside an area, e.g. places inside London.
Is this possible to do with the Google places javascript library?
thanks!
All of this and more is possible using the Google Places JavaScript library.
There's a code snippet on the page above that does exactly that (searching for places based on longitude / latitude).
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'), {
mapTypeId: google.maps.MapTypeId.ROADMAP,
center: pyrmont,
zoom: 15
});
var request = {
location: pyrmont,
radius: '500',
types: ['store']
};
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++) {
var place = results[i];
createMarker(results[i]);
}
}
}

Find out distance between two points using Google map API based on medium (Road,Air,Ship)? [duplicate]

I have used the google map API to plot a way point on google map
I referred to the the following page:
https://developers.google.com/maps/documentation/javascript/directions
'Using Waypoints in Routes section' and modified it a bit so as to plot only 3 points on the map.
The following is my javascript code.
var directionDisplay;
var directionsService = new google.maps.DirectionsService();
var map;
function initialize() {
directionsDisplay = new google.maps.DirectionsRenderer();
var initialloc = new google.maps.LatLng(12.971599, 77.594563);
var mapOptions = {
zoom : 6,
mapTypeId : google.maps.MapTypeId.ROADMAP,
center : initialloc
}
map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);
directionsDisplay.setMap(map);
calcRoute();
}
function calcRoute() {
var lat = new Array();
var lon = new Array();
var start = new google.maps.LatLng(12.971599,77.594563);
var mid = new google.maps.LatLng(12.971558,77.594552);
var end = new google.maps.LatLng(12.971558,77.594552);
var waypts = [];
waypts.push( {
location : mid,
stopover : true
});
var request = {
origin : start,
destination : end,
waypoints : waypts,
optimizeWaypoints : true,
travelMode : google.maps.DirectionsTravelMode.DRIVING
};
directionsService.route(
request,
function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
var route = response.routes[0];
}
});
}
which works perfectly fine if the 3 locations are within same country, ie they have a road map.
My question is how to plot the map when the location are in different continents eg India and Australia?
Can anyone please help.
Thanks in Advance.
The problem is not about different continents, but whether the routing engine's database has information about all the countries between your start and endpoints, including car ferries. You can see that the same occurs in maps.google.com . Here's an intercontinental route from Europe to India, but if you try to move the B marker to the US or Canada, it doesn't get a route because it doesn't know a ferry across the Atlantic.
You can see what countries have coverage in this spreadsheet.

How to load markers from XML file with directions API in Google Maps

I'm trying to load markers from XML file on a map used for outputing directions. Basically, it's the combination of two demos found on Google's documentation pages.
Directions: https://google-developers.appspot.com/maps/documentation/javascript/examples/directions-panel
XML: http://gmaps-samples-v3.googlecode.com/svn/trunk/xmlparsing/downloadurl_info.html
I have first created the directions map and then tried to add XML file that contains markers.
I'm probably making a simple mistake, but since I'm not good with js and coding, can't find what. There are no errors displayed, only a blank page.
Here is my current code:
<script>
var directionDisplay;
var directionsService = new google.maps.DirectionsService();
function initialize() {
directionsDisplay = new google.maps.DirectionsRenderer();
var mapOptions = {
zoom: 7,
mapTypeId: google.maps.MapTypeId.ROADMAP,
center: new google.maps.LatLng(41.850033, -87.6500523)
};
var map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);
directionsDisplay.setMap(map);
directionsDisplay.setPanel(document.getElementById('directions-panel'));
var control = document.getElementById('control');
control.style.display = 'block';
map.controls[google.maps.ControlPosition.TOP].push(control);
}
var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
downloadUrl("http://gmaps-samples-v3.googlecode.com/svn/trunk/xmlparsing/moredata.xml", function(data) {
var markers = data.documentElement.getElementsByTagName("marker");
for (var i = 0; i < markers.length; i++) {
var latlng = new google.maps.LatLng(parseFloat(markers[i].getAttribute("lat")),
parseFloat(markers[i].getAttribute("lng")));
var marker = createMarker(markers[i].getAttribute("name"), latlng);
}
});
function calcRoute() {
var start = document.getElementById('start').value;
var end = document.getElementById('end').value;
var request = {
origin: start,
destination: end,
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
}
});
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
Here is the (non-working) jsFiddle: http://jsfiddle.net/ajJ3u/
Problems from a quick review:
you are creating the map twice.
you don't have a createMarker function. If that call came from one of the examples, you missed bringing it to the new map.
downloadUrl is subject to a cross-domain security restriction. If your page is not running in the "http://gmaps-samples-v3.googlecode.com" domain, it won't work. You need to access xml from the same domain as the page is running in or use a proxy.
Example of directions from/to markers from xml (translated to v3 from Mike Williams' v2 tutorial