dongpao5658 2016-11-02 18:53
浏览 33

我在哪里可以获得Geolocation API [关闭]

I am looking for a geolocation API that I can plug into my PhpStorm project.

I need a dropdown that will firstly load all countries in the world, upon selection of a country another drop down will populate provinces/states based on country selected above. There after a dropdown that will populates cities based on the province or state above and lastly another dropdown that will populate surburbs of the city listed above.

  • 写回答

1条回答 默认 最新

  • duan111112 2016-11-06 17:25
    关注

    here is your complete solution

    use map api

    https://maps.googleapis.com/maps/api/js?sensor=false&libraries=geometry,places&ext=.js

    use this css

    http://vikku.info/programming/js/geodata-jsr-class.js

    <div class='main'>
    
    <h1></h1>
    
        <form id="myform">
            <div class="contents">
    <b>*Select area</b>
    
                <p><span>Continent:</span> 
                    <select name="continent" id="continent" onchange="getplaces(this.value,'country');">
                        <option value=""></option>
                    </select>
                </p>
                <p><span>Country:</span> 
                    <select name="country" id="country" onchange="getplaces(this.value,'province');">
                        <option value=""></option>
                    </select>
                </p>
                <p><span>State / Provice:</span> 
                    <select name="province" id="province" onchange="getplaces(this.value,'region')">
                        <option value=""></option>
                    </select>
                </p>
                <p><span>County / Region:</span> 
                    <select name="region" id="region" onchange="getplaces(this.value,'city')">
                        <option value=""></option>
                    </select>
                </p>
                <p><span>City:</span> 
                    <select name="city" id="city" onchange="zoomto(this.value);codeAddress(placedata[this.value].name)">
                        <option value=""></option>
                    </select>
                </p>
            </div>
        </form>
        <div id="map_canvas"></div>
    
    javascript
    
    
    var whos = null;
    var placedata = [];
    var map;
    var geocoder = new google.maps.Geocoder();
    
    function getplaces(gid, src) {
        if ( !! placedata[gid]) {
            map.setCenter({
                lat: parseFloat(placedata[gid].lat),
                lng: parseFloat(placedata[gid].lng)
            });
            switch (src) {
                case "continent":
                    map.setZoom(3);
                    break;
                case "country":
                    map.setZoom(5);
                    break;
                case "province":
                    map.setZoom(6);
                    break;
                case "region":
                    map.setZoom(7);
                    break;
                case "city":
                    map.setZoom(8);
                    break;
            }
            codeAddress(placedata[gid].name);
        }
    
        whos = src;
    
    
        //  var  request = "http://ws.geonames.org/childrenJSON?geonameId="+gid+"&callback=getLocation&style=long";
        var request = "http://www.geonames.org/childrenJSON?geonameId=" + gid + "&callback=listPlaces&style=long";
        aObj = new JSONscriptRequest(request);
        aObj.buildScriptTag();
        aObj.addScriptTag();
    }
    
    function listPlaces(jData) {
        counts = jData.geonames.length < jData.totalResultsCount ? jData.geonames.length : jData.totalResultsCount;
        who = document.getElementById(whos);
        who.options.length = 0;
    
        if (counts) who.options[who.options.length] = new Option('Select', '');
        else who.options[who.options.length] = new Option('No Data Available', 'NULL');
    
        for (var i = 0; i < counts; i++) {
            who.options[who.options.length] = new Option(jData.geonames[i].name, jData.geonames[i].geonameId);
            placedata[jData.geonames[i].geonameId] = jData.geonames[i];
        }
    
        delete jData;
        jData = null;
    }
    function zoomto(gid) {
       if ( !! placedata[gid]) {
            map.setCenter({
                lat: parseFloat(placedata[gid].lat),
                lng: parseFloat(placedata[gid].lng)
            });
           map.setZoom(14);
       }
    }
    function codeAddress(address) {
        geocoder.geocode({
            'address': address
        }, function (results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                if (!!results && !!results[0] && !!results[0].geometry && !!results[0].geometry.viewport) {
                    map.fitBounds(results[0].geometry.viewport);
                } else if (!!results && !!results[0] && !!results[0].geometry && !!results.geometry.bounds) {
                    map.fitBounds(results[0].geometry.bounds);
                } else {
                    map.setCenter(results[0].geometry.location);
                }
            } else {
                alert("Geocode was not successful for the following reason: " + status);
            }
        });
    }
    
    window.onload = function () {
        getplaces(6295630, 'continent');
        map = new google.maps.Map(document.getElementById('map_canvas'), {
            zoom: 3,
            center: {
                lat: 0,
                lng: 0
            }
        });
    };
    

    here is a fiddle :

    http://jsfiddle.net/geocodezip/xq4h95ux/3/

    if you simply want ip address and region of a user then you do it like so

    $.getJSON("http://ip-api.com/json/?callback=?", function (data) {
        console.log(data);
        $('#IP_Address').append("Your IP: " + data.query);
        $('#Country').append("Your Country: " + data.country);
        $('#City').append("Your City: " + data.city);
        $('#region').append("Your Region : " + data.region);
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
    <div id="IP_Address"></div>
    <div id="Country"></div>
    <div id="City"></div>
    <div id="region"></div>

    if you want to get these information in php then you could use

    http://www.geoplugin.net/php.gp

    if you have ip address then you can pass it like so

    http://www.geoplugin.net/php.gp?ip=$youIpAddress

    (for this case 49.200.208.108)

    it will dump

    a:18:{s:17:"geoplugin_request";s:14:"49.200.208.108";s:16:"geoplugin_status";i:200;s:16:"geoplugin_credit";s:145:"Some of the returned data includes GeoLite data created by MaxMind, available from <a href=\'http://www.maxmind.com\'>http://www.maxmind.com</a>.";s:14:"geoplugin_city";s:6:"Mumbai";s:16:"geoplugin_region";s:13:"Mahārāshtra";s:18:"geoplugin_areaCode";s:1:"0";s:17:"geoplugin_dmaCode";s:1:"0";s:21:"geoplugin_countryCode";s:2:"IN";s:21:"geoplugin_countryName";s:5:"India";s:23:"geoplugin_continentCode";s:2:"AS";s:18:"geoplugin_latitude";s:6:"18.975";s:19:"geoplugin_longitude";s:7:"72.8258";s:20:"geoplugin_regionCode";s:2:"16";s:20:"geoplugin_regionName";s:13:"Mahārāshtra";s:22:"geoplugin_currencyCode";s:3:"INR";s:24:"geoplugin_currencySymbol";s:7:"&#8360;";s:29:"geoplugin_currencySymbol_UTF8";s:3:"₨";s:27:"geoplugin_currencyConverter";s:6:"66.695";}

    unserialize the obtained data(or json_decode) and get all information

    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line