1

I want in my application show multiple markers on google map .only address of the city or country is provided but without latitude and longitude of the city.any help will be appreciated

francis dcunha
  • 55
  • 3
  • 11

2 Answers2

1

In PHP:

$address will contains the address.

$address = str_replace(' ', '+', $address);
$geocode=file_get_contents('http://maps.google.com/maps/api/geocode/json?address='.$address.'&sensor=false');

$output= json_decode($geocode);

$lat = $output->results[0]->geometry->location->lat;
$long = $output->results[0]->geometry->location->lng;

This is in php and you can get lat and long.

In Javascript:

var address = //your address
var map = new google.maps.Geocoder();
map.geocode({'address' : address }, function(results, status){
    alert( "latitude : " + results[0].geometry.location.lat() );
    alert( "longitude : " + results[0].geometry.location.lng() );
});
Code Lღver
  • 15,573
  • 16
  • 56
  • 75
0

You can get the latitude and longitude of a city (or country) like this.

address_string = '1600 Pennsylvania Avenue NW Washington, DC 20500';
geocoder = new GClientGeocoder();
geocoder.getLatLng(
    address_string,
    function(point) {
        if (point !== null) {
            alert(point); // or do whatever else with it
        } else {
            // error - not found, over limit, etc.
        }
    }
);

Note that I don't have a Google maps API key so I didn't have the time to test this.

Rick Hoving
  • 3,585
  • 3
  • 29
  • 49