while generating google map i am sending address to a java script function.sample code:--
java script function code:--
function codeAddress(address) {
// var address = document.getElementById("address").value;
var address;
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("Geocode was not successful for the following reason: " + status);
}
});
`"
cs code :--
"
string addrs= "New York,USA";
Page.ClientScript.RegisterStartupScript(this.GetType(), "a", "codeAddress(" + addrs + ");", true);
"
i am using proper API Key and url. in html its working. but when i pass address from cs file its not working. why? how can i execute above java script function by passing address value.. is there any wrong in code... Thanks
Try
Page.ClientScript.RegisterStartupScript(this.GetType(), "a", "codeAddress('" + addrs + "');", true);
My guess is that it's not interpreted as a string without ''.
Related
Please provide me proper solution for following code
how can I pass the Google API Key
function load_map_from_address(mapid, address) {
// check if gps has been locally cached.
geocoder = new google.maps.Geocoder();
//alert(geocoder);
var geocoderAPIKey = 'geocoderAPIKey ';
geocoder.geocode({ 'address': address }, function (results, status) {
//alert(status);
if (status == "OK") {
var gps = results[0].geometry.location;
create_map(gps.lat(), gps.lng(), mapid);
}
else {
$('#' + mapid).html('<div class="map_canvas_text "><h4>address not found</h4></div>').show();
}
});
}
You can mention it when you include the Google map js . Something like .
Path-to-google-map js?key=yourApiKey
In you're script tag
I am trying to get formatted address of customer who access my web page.
For that I wrote function to get lat long and then Reverse geocode it to formatted address.
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script>
function coordinates_to_address(lat, lng) {
var latlng = new google.maps.LatLng(lat, lng);
var geocoder= new google.maps.Geocoder();
geocoder.geocode({'latLng': latlng}, function(results, status) {
if(status == google.maps.GeocoderStatus.OK) {
if(results[0]) {
$('#address_current').text(results[0].formatted_address);
} else {
alert('No results found');
}
} else {
var error = {
'ZERO_RESULTS': 'No address'
}
// alert('Geocoder failed due to: ' + status);
$('#address_new').html('<span class="color-red">' + error[status] + '</span>');
}
});
}
navigator.geolocation.getCurrentPosition(function(location) {
console.log(location.coords.latitude);
console.log(location.coords.longitude);
console.log(location.coords.accuracy);
var lat= location.coords.latitude;
var long= location.coords.longitude
coordinates_to_address(lat, long);
});
</script>
The log showed the output lat long
10.8888888888888888
76.43923530000001
40
But this function returned an error:
Google Maps API error: MissingKeyMapError https://developers.google.com/maps/documentation/javascript/error-messages#missing-key-map-error
http://maps.google.com/maps/api/js?sensor=false
Line 34
Is there any easier method to get the formatted address of a lat long?
Keys are now required with the Google Maps Javascript API v3 (but the sensor parameter is not).
Please suggest me the different ways how i get the longitude and latitude from address so that i can pass the value to get the navigation from current location in IONIC Framework.
Till Now I am able to get the lat long from address:
address$scope.geocodeAddress = function() {
var address = document.getElementById('from').value;
geocoder.geocode({
'address': address
}, function(results, status) {
if (status === 'OK') {
map.setCenter(results[0].geometry.location);
console.log("latlng-->" + results[0].geometry.location);
var latitude = results[0].geometry.location.lat();
var longitude = results[0].geometry.location.lng();
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
} else {
alert('Geocode was not successful: ' + status);
}
});
Just use HTML5 Geoloc, it will use Phone GPS.
<script>
var x = document.getElementById("demo");
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
x.innerHTML = "Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
}
</script>
There's a lot of ressource to help you out with it like W3S.
If you want to open Google Maps with directions to a specific address, you don't have to get the latitude and longitude. You can use the phonegap-launch-navigator-plugin to launch Google Maps with the address filled in and Google Maps will handle the rest. You can find documentation on how to use it here and as a bonus you can also open many other navigation apps.
The address I have is a concatenated results[1].formatted_address retrieved from a database containing 6 numbers e.g 530456. I then geocoded it and placed a marker on a google map. This part is successful.
I now want to display the full address, results[0].formatted_address in a infowindow of the marker, so what I did was to reverse geocode the latLng of the marker to obtain the full address but caused the entire map to disappear. The way I did it is as such:
var address;
var eventwindow;
function codeAddress(postal) {
geocoder.geocode( {'address': postal + ", Singapore"}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
eventwindow = new google.maps.InfoWindow();
var markerE = new google.maps.Marker({
map: map,
position: results[0].geometry.location,
});
address = getMarkerAddress(results[0].geometry.location);
google.maps.event.addListener(marker, 'click', function(){
if(eventwindow)
eventwindow.close();
eventwindow = new google.maps.InfoWindow({
content: address,
});
eventwindow.open(map,marker);
});
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
function getMarkerAddress(location) {
geocoder.geocode({'latLng': location}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if(results[0])
{
address = results[1].formatted_address.substring(10);
}
}
else {
alert("Geocoder failed due to: " + status);
}
});
I don't know if I'm doing this right and and not sure where I went wrong. Is there another way to this problem? If so how?
I don't know if I'm doing this right and and not sure where I went wrong. Is there another way to this problem? If so how?
If you know the "correct" address, use that. You haven't provided enough details of your application to make suggestions on how to do that. You can avoid the uncertainty of the geocoder by storing the coordinates that are returned from the geocoder in your database along with the address, then if you find an error, you can fix it.
This article on geocoding strategies might help.
I have a Javascript form where a user inputs an address, city, and state. When this form is submitted, I want the address to be converted into latitude and longitude, and to store those two values to a database. Any help at all is greatly appreciated!
Check this out:
http://batchgeo.com/lookup/
You can use the Google Maps API v3, example:
//get the input from user, in a normal looking address string
var add = this.ListingAddress + ', ' + this.ListingCity + ', ' + this.ListingZipCode;
var geocoder = new google.maps.Geocoder();
geocoder.geocode({ 'address': add }, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var LatLong = results[0].geometry.location; //here is the LatLong
} else {
alert("Address not found! Error Code: " + status);
}
});