views:

47

answers:

1

Hi there, 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!!

+1  A: 

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.

Mark
file_get_contents allows "cross-site" if the server is configured to allow it. json_decode is the correct function.
Galen
Thanks for the advice, but its a bit hard for me from your instruction, as I just a beginner of programmer. thanks anyway. I will try it.
xuanyinwen
well, you have to solve some things on your own to learn. it shouldn't be too hard... we're only talking like 10 lines of code.
Mark
Hi Mark, thanks again,
xuanyinwen
could you give me some example or link that I can get some idea from? cheers
xuanyinwen
see update.....
Mark
Hi Mark, many thanks for that, please check out the code I use for google map:http://www.xuanyinwen.com/test8.html
xuanyinwen
so should I put your script in the heater or or replace all script in the heater? sorry for anorring
xuanyinwen
put it in the header...?? you said you wanted this in *PHP*, so I gave you the answer *in PHP*. which means it needs to go into a file that ends with `.php`, and it appears *nowhere* in the HTML.
Mark
so could you give me some idea what can I do please?
xuanyinwen