読者です 読者をやめる 読者になる 読者になる

【Cordova】Google Geocoding API

 

①クライアントアプリの実装(HTML)

<script type="text/javascript" src="http://maps.google.com/maps/api/js?v=3&sensor=false"></script>

 

②クライアントアプリの実装(JS)

var latlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
  var options = {
    location: latlng
   // address: "tokyo"
};

var geocoder = new google.maps.Geocoder();
geocoder.geocode(options, function(results, status) {
  if(status == google.maps.GeocoderStatus.OK) {
    var options = {
      zoom: 15,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    // 地図を取得+マーカーを設定
    var map = new google.maps.Map(document.getElementById("mapCanvas"),                    options);
    var marker = new google.maps.Marker({
       position: latlng,
       map: map
    });
    var address = results[0].formatted_address;
    var zipcord = results[0].address_components[6].long_name;
    var div = "<div>" + address + "</div>";
    var infoWindow = new google.maps.InfoWindow({content: div});
    infoWindow.open(map, marker);
  } else {
    alert('Faild:' + status);
  }
});

 

▼補足 results(例)
results[0].formatted_address: "275-291 Bedford Ave, Brooklyn, NY 11211, USA",
results[1].formatted_address: "Williamsburg, NY, USA",
results[2].formatted_address: "New York 11211, USA",
results[3].formatted_address: "Kings, New York, USA",
results[4].formatted_address: "Brooklyn, New York, USA",
results[5].formatted_address: "New York, New York, USA",
results[6].formatted_address: "New York, USA",
results[7].formatted_address: "United States"