简体   繁体   中英

how can I use php to create Latlng with address for google map V3

I done a lot research but can't find the way to create latlng in the google map as below, how can I use address for the latlng number?

the script is

function initialize()
{
    var latlng = new google.maps.LatLng(-41.251290,174.803638);
    var opt =
    { 
        center:latlng,
        zoom:10,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        disableAutoPan:false,
        navigationControl:true,
        navigationControlOptions: {style:google.maps.NavigationControlStyle.SMALL },
        mapTypeControl:true,
        mapTypeControlOptions: {style:google.maps.MapTypeControlStyle.DROPDOWN_MENU}
    };
    var map = new google.maps.Map(document.getElementById("map"),opt);
    var marker= new google.maps.Marker({
    position: new google.maps.LatLng(-41.251290,174.803638),
    title: "CodeGlobe",
    clickable: true,
    map: map
});

many thanks!!

Use their Web Services API

http://code.google.com/apis/maps/documentation/geocoding/

Make a request to something like

http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false

Using something like file_get_contents (can't remember if that supports cross-site..? whatever, I'm sure you can look up the appropriate function)

and then parse the results with json_decode I think is the function... been awhile since i touched PHP.

sigh c'mon man.

$address = "1600 Amphitheatre Parkway, Mountain View, CA";
$url = "http://maps.googleapis.com/maps/api/geocode/json?address=".urlencode($address)."&sensor=false";
$result_string = file_get_contents($url);
$result = json_decode($result_string, true);
list($lat, $long) = $result['geometry']['location'];

something like that. I haven't tested it, but give it a whirl. 5 lines of code.

我在Marks Answers取得$lat$long问题时遇到了问题,我必须这样做

$lat = $result['results'][0]['geometry']['location']['lat'];

$long = $result['results'][0]['geometry']['location']['lng'];

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