This question already has answers here:
Using Address Instead Of Longitude And Latitude With Google Maps API
(5 answers)
Closed last year.
I am trying to make a program that creates a google map based off the address given by user input. As i can not manually embed a link for each page, how can i make it so that when a user answers a form with the address I can take that address and make a google map. Do i have to convert the address to latitude and longitude and then do that?
Regarding the Google maps javascript API, you need the latitude (lat) and longitude (lng) to place a marker on map. To find the lat and lng of an adress, you need a geocoding service.
Geocoding is the process of converting addresses (like "1600 Amphitheatre Parkway, Mountain View, CA") into geographic coordinates (like latitude 37.423021 and longitude -122.083739), which you can use to place markers on a map, or position the map.
It exist a lot of geocoding services and Google Cloud Plateform provide one. First, you need to activate the Geocoding API in your acount (see this page from the documentation).
By using geocoding API
To find an adress (exemple: "1600 Amphitheatre Parkway, Mountain View, CA 94043, USA"), it's just a simple get request to the API with something like
https://maps.googleapis.com/maps/api/geocode/jsonaddress=1600+Amphitheatre+Parkway,+Mountain+View,+CA&key=YOUR_API_KEY
You can get the lat/lng from the JSON response and use it to place your marker on the map. To do it, just follow the instruction from the documentation and you be able to place your marker by doing something like
let myLatLng = {lat:myLatitude, lng:myLongitude};
new google.maps.Marker({
position: myLatLng,
map
});
By using Geocoder()
Like #JeremyW says, you can also call the geocoding service by using the Geocoder class from Google map javascript API (see this post).
let geocoder = new google.maps.Geocoder();
then...
let adress = "1600 Amphitheatre Parkway, Mountain View, CA 94043, USA";
geocoder.geocode( { 'address': address}, function(results, status){
if(status == google.maps.GeocoderStatus.OK){
if(status != google.maps.GeocoderStatus.ZERO_RESULTS){
let marker = new google.maps.Marker({
position: results[0].geometry.location,
map: map
});
}
else{
console.log("No results found");
}
}
else{
console.log("Geocode was not successful for the following reason: " + status);
}
}
Related
I have a database which contains a list of point of interests with latitude and longitude information. This data is not from Google Places.
I would like to get the information of the opening hours of the places based on their geolocation. Is it possible to do that using Google Places API ?
I have tried using Google Maps Geocoding API to get the place_id for given geocoding and then used Google Places API Web Service for getting the detail information of the place based on the place_id. However, it does not get the opening hours.
I wonder how to get the detail of a place using geocoding ?
For example in HERE using autocomplete place search, it is able to return the detail information about the place. However when I right clicked on the marker and copied the geolocation using what's this and tried to use Google Places API Web Service to query the detail of the place, some information is missing, for example, opening hours of the place.
For example Gyeongbokgung Palace the latitude and longitude stored in database is 37.5802 and 126.977 respectively as shown in the picture, the location from Google Maps is different even though they are not that far from each other.
With the Place details you can get all details for a specific place.
function initialize() {
var map = new google.maps.Map(document.getElementById('map-canvas'), {
center: new google.maps.LatLng(0, 0),
zoom: 15
});
var service = new google.maps.places.PlacesService(map);
service.getDetails({
placeId: 'ChIJod7tSseifDUR9hXHLFNGMIs' // Gyeongbokgung Palace
}, function (place, status) {
if (status === google.maps.places.PlacesServiceStatus.OK) {
console.log(place); // Log the place object
}
});
}
Below is a complete example on how to retrieve the opening hours for the place you mentioned.
JSFiddle demo
I am working on JavaScript.I have places in array format like ["Chennai","Hyderabad","Bangalore",....] I want to find latitude and longitude of them .
As per I know we can I find one place details at a time using geocode.but I want to find bunch of place details.For this I read Google batch request(link) concept, I didn't understand.
I can make API request one by one but it's not a good practice.Even If I follow same way I will get limitation error from Google.
can anyone help me, How can I find latitude and longitude details.
If anyone wants to down-vote, please let me know the reason.
Thanks.
Use the Geocoder:
var geocoder = new google.maps.Geocoder();
geocoder.geocode({ address: address },
function (results, status) {
if (status === google.maps.GeocoderStatus.OK) {
var location = results[0].geometry.location;
console.log('lng: ' + location. getLng() + ', lat: ' + location.getLat());
}
});
Note that you can't do a batch request with the geocoder API. But you can use the directions api to geocode up to 8 addresses. In the business class it allows up to 24 addresses. Read about it:
https://developers.google.com/maps/documentation/javascript/directions
I'm using Google Maps Geocoding function. I got some addresses which I load into a PHP variable. The addresses are defined by a textbox which the user can type in a zip code. I give an example:
A user types in the textbox a postal code (12345 for example). Now it loads addresses from the system (this is internal - but it works to load the addresses). And then it display some addresses. These addresses are now stored in a PHP variable ($address). And now I want to get the latitude and longitude from these different addresses.
I had a look at this thread: Javascript geocoding from address to latitude and longitude numbers not working
I used this code from the article:
var latitude = results[0].geometry.location.lat();
var longitude = results[0].geometry.location.lng();
alert(latitude + ' - ' + longitude);
This is nice but it just give me the latlng from the address I typed in the textbox, not from all the addresses I got.
Is there a way to get the latitude and longitude from all the different addresses? If yes, could someone please explain me? I'm new to JavaScript and Google Maps API.
Thanks in advance
You can use the example given here.
See the code below for specific functionality:
geocoder = new google.maps.Geocoder();
function findAddress(address)
{
// You can pass the address from your PHP to Javascript using AJAX. Here, I'm assuming that you have done it and that you're passing it as a string argument to this function
geocoder.geocode( { 'address': address}, function(results, status)
{
if (status == google.maps.GeocoderStatus.OK)
{
return position: results[0].geometry.location;
}
else
{
// Show some message that the address cannot be found on map.
}
});
}
I am using the Google Maps API and currently successfully adding markers based on exact locations, but I would like to also be able to add markers over countries without needing to have the coordinates of that country.
Is this possible?
It is highly possible indeed. Use GeoCoder : https://developers.google.com/maps/documentation/javascript/geocoding
GeoCoder can return the lat / lngs for a country, just by knowing its name.
Assuming you already have a map :
geocoder = new google.maps.Geocoder();
function getCountry(country) {
geocoder.geocode( { 'address': country }, 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("Geocode was not successful for the following reason: " + status);
}
});
}
getCountry('USA');
getCountry('Brazil');
getCountry('Denmark');
Will place markers on your map in the direct center of USA, Brazil and Denmark.
According to the Maps API, marker.position is a required property, and it must be of LatLng type. So at the very least you'd either need to know the coordinates at the centre of the country, or an bounded array of coordinates for the country from which you you can extrapolate the coordinates for the centre.
I believe that if you use the Geocoder with just the country name, the service will provide you with the coordinates for the exact centre of the country. I've not tested this, and I don't know what if there's a limit to how much you can use it, but it would be worth you checking it out as a solution to your question.
For example:
var geo = new google.maps.Geocoder();
geo.geocoder.geocode('Spain', function (data) {
// the coordinates are held in data[0].geometry.location.lat() and data[0].geometry.location.lng().
});
I am wondering if it possible to use Bing Maps API to perform geocoding (get long/lat based on Postal Code or address) as a batch process or at least without having to render a map with each geocode. With google maps API, the process looks like this
var geocoder=new google.maps.Geocoder();
geocoder.geocode({ 'address': pCodes[counter-1] }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK){
var pcode = results[0].address_components[0].long_name;
var lat = results[0].geometry.location.lat();
var lon = results[0].geometry.location.lng();
// do something with results
}
else {
// error condition
}
});
With Bing the VEMap map class, it looks like map and geocoding are bound together.
You are correct, the VEMap is for displaying maps.
If you just want location data, you can call the Bing Maps REST based Location API.
You can find out more about the API on MSDN at http://msdn.microsoft.com/en-us/library/ff701714.aspx