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
Related
Do you know any way to find correct location (lon,lat) using python or js
I am trying the following python code but it doesnt give me my correct current location
import json
from urllib.request import urlopen
from requests import get
import geocoder
url = 'http://ipinfo.io/json'
response = urlopen(url)
data = json.load(response)
g = geocoder.ip('me')
print(g.latlng)
"""loc = get('https://ipapi.co/{}/json/'.format(data['ip']))
print(loc.json())"""
send_url = 'http://freegeoip.net/json'
r = get(send_url)
j = json.loads(r.text)
lat = j['latitude']
lon = j['longitude']
print("lat {} lon {}".format(lat, lon))
Your IP Address doesn't/can never give you the correct location of where you are it. The location can from the internet can only be used to get the country and MAYBE the state of the user.
The location given is usually where the Sub-Station of your ISP is located.
In javascript you can do this
let position;
navigator.geolocation.getCurrentPosition((pos) => position = pos.coords &&
console.log(position));
after that you can just do:
-position.latitude
-position.longitude
It's not the most accurate precision, but in chase you want something more precise, you can look for watchPosition() of Geolocation in MDN documentation.
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.
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
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.
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 :-)