Reputation: 55
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
Upvotes: 1
Views: 2609
Reputation: 15603
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() );
});
Upvotes: 1
Reputation: 3575
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.
Upvotes: 0