简体   繁体   中英

Unable to fetch longitude,latitude,etc through geocoding with mapsmyindia

Attempt geocoding addresses mapsmyindia api. Here is url for reference: https://www.mapmyindia.com/api/advanced-maps/doc/geocoding-api .

The code is given as below

    $json_str = file_get_contents("http://apis.mapmyindia.com/advancedmaps/v1/<?=$key ?>/geo_code?addr=Singanallur Railway Station, Tamil Nadu&pin=");

$obj  = json_decode($json_str);
$data = $obj->lng;
echo $data;

Here is the output we got at best.

{"responseCode":"200","version":"181.2731","results":[{"houseNumber":"","houseName":"","poi":"Rajalakshmi Mills","street":"Kalimadai Road","subSubLocality":"TVH Vista Heights","subLocality":"Nanjappa Nagar","locality":"Singanallur","village":"","subDistrict":"Coimbatore South","district":"Coimbatore District","city":"Coimbatore;Kovai","state":"Tamil Nadu;TN","pincode":"641005","lat":"10.995464999999999","lng":"77.015103000000096","formatted_address":"Rajalakshmi Mills, Kalimadai Road, TVH Vista Heights, Nanjappa Nagar, Singanallur, Coimbatore South, Coimbatore District, Coimbatore, Tamil Nadu","place_id":"FC84F7","type":"POI"},{"formatted_address":"Rajalakshmi Mills, Ramanathapuram, Coimbatore South, Coimbatore District, Coimbatore, Tamil Nadu","place_id":"OLIC1G","type":"HOUSE_NAME"},{"formatted_address":"Rajalakshmi Mills High School, Anugraha Gardens, Singanallur, Coimbatore South, Coimbatore District, Coimbatore, Tamil Nadu","place_id":"8PXAIC","type":"HOUSE_NAME"},{"formatted_address":"Pankaja Mills, Coimbatore South, Coimbatore District, Coimbatore, Tamil Nadu","place_id":"V83JL9","type":"LOCALITY"},{"formatted_address":"Junction Mills, Coimbatore North, Coimbatore District, Coimbatore, Tamil Nadu","place_id":"IPCDQT","type":"LOCALITY"},{"formatted_address":"Lakshmi Mills, Coimbatore South, Coimbatore District, Coimbatore, Tamil Nadu","place_id":"7X8ZFU","type":"LOCALITY"},{"formatted_address":"GN Mills, Coimbatore North, Coimbatore District, Coimbatore, Tamil Nadu","place_id":"KACYTZ","type":"LOCALITY"},{"formatted_address":"Rajalakshmi Nagar, Dindigul, Dindigul District, Dindigul, Tamil Nadu","place_id":"T7Y5DF","type":"LOCALITY"}]}

Its working, but throws up data en bloc. That's not the output desired. Require values separately. For instance longitude, latitude should be outputted as separate values.

Look for way out.

According to the manual you link to the API returns a JSON String. So to access that data using PHP you need to convert that string into a PHP native data type using json_decode()

So for example

$json_str = file_get_contents("http://apis.mapmyindia.com/advancedmaps/v1/<?=$key ?>/geo_code?addr=Singanallur Railway Station, Tamil Nadu&pin=");

$obj  = json_decode($json_str);
$data = $obj->lng;
echo $data;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM