Get location information from pincode

Suppose I have form of address filling. How can I get country, state, city from the pincode that is given? Also Can we use geolocation API for finding the location in ERPNext?

Why not.

Write a script to capture the country, city from geolocation API response and set it to respective address fields.

Thanks for your quick response. I tried to figure out how to write
script for it but unable to do so. Please show how to write script for
using geolocation API in ERPNext.

Can you share your API and it’s response here?

I have not created anything in ERPNext regarding this API but i have previously used this in javascript. I am very new to ERPNext. I want to know how can we use it in ERPNext.

You can write a Custom Script (javascript) for it.

simply hit your API and set response values to your fields on a form.
e.g. If you are getting city and country from API response. Use ajax or frappe.call

$.ajax({
	url: API URL,
	type: 'POST',
	dataType: 'json',
	success: function (data, textStatus, xhr) {
		cur_frm.set_value("city", data.get('city'))
		cur_frm.set_value("country", data.get('country'))
                cur_frm.refresh_fields();
	}
});

Thank You very much.

Here is my custom script where I am trying to get the location:

frappe.ui.form.on(“MyProjects”, {
$.ajax({
url: “https://maps.googleapis.com/maps/api/geocode/json?address=110057&key=xxxxxxxxxx”,
type: ‘POST’,
dataType: ‘json’,
success: function (data, textStatus, xhr) {
msgprint((“Success!!”));
}
error: function (data, textStatus, xhr) {
msgprint(
(“Failure!!”));
}
});
});

but I am getting the following error:

setup@http://localhost:8000/assets/js/form.min.js:2839:5
_f.Frm.prototype.setup@http://localhost:8000/assets/js/form.min.js:250:2
_f.Frm.prototype.refresh@http://localhost:8000/assets/js/form.min.js:573:24
load@http://localhost:8000/assets/js/form.min.js:133:3
show_doc/<@http://localhost:8000/assets/js/form.min.js:118:7
callback@http://localhost:8000/assets/js/desk.min.js:6907:29
callback@http://localhost:8000/assets/js/desk.min.js:1294:11
frappe.request.call/statusCode[200]@http://localhost:8000/assets/js/desk.min.js:1319:29
frappe.request.call/<@http://localhost:8000/assets/js/desk.min.js:1427:5
i@http://localhost:8000/assets/frappe/js/lib/jquery/jquery.min.js:2:27146
fireWith@http://localhost:8000/assets/frappe/js/lib/jquery/jquery.min.js:2:27914
z@http://localhost:8000/assets/frappe/js/lib/jquery/jquery.min.js:4:12057
c/<@http://localhost:8000/assets/frappe/js/lib/jquery/jquery.min.js:4:15619

Try it on a specific event.

e.g. on validate

frappe.ui.form.on("MyProjects", {
    	validate: function(frm) {
    	   //your script
    	}
    }

tried it on refresh.

what is the output

I guess it’s same

setup@http://localhost:8000/assets/js/form.min.js:2839:5
_f.Frm.prototype.setup@http://localhost:8000/assets/js/form.min.js:250:2
_f.Frm.prototype.refresh@http://localhost:8000/assets/js/form.min.js:573:24
load@http://localhost:8000/assets/js/form.min.js:133:3
show_doc/<@http://localhost:8000/assets/js/form.min.js:118:7
callback@http://localhost:8000/assets/js/desk.min.js:6907:29
callback@http://localhost:8000/assets/js/desk.min.js:1294:11
frappe.request.call/statusCode[200]@http://localhost:8000/assets/js/desk.min.js:1319:29
frappe.request.call/<@http://localhost:8000/assets/js/desk.min.js:1427:5
i@http://localhost:8000/assets/frappe/js/lib/jquery/jquery.min.js:2:27146
fireWith@http://localhost:8000/assets/frappe/js/lib/jquery/jquery.min.js:2:27914
z@http://localhost:8000/assets/frappe/js/lib/jquery/jquery.min.js:4:12057
c/<@http://localhost:8000/assets/frappe/js/lib/jquery/jquery.min.js:4:15619

missing comma after success().
Check this,

frappe.ui.form.on("MyProjects", {
	refresh: function(frm) {
		$.ajax({
			url: "https://maps.googleapis.com/maps/api/geocode/json?address=110057&key=xxxxxxxxxx",
			type: 'POST',
			dataType: 'json',
			success: function (data, textStatus, xhr) {
				console.log(data)
				msgprint(__("Success!!"));
			},
			error: function (data, textStatus, xhr) {
				msgprint(__("Failure!!"));
			}
		});
	}
});

And check your console for the response

comma is there. I rechecked it anyway. still same

Working for me. Check your script again, just copy paste given by me.

Thank you very much. It works now.