How retrieve latitude and longitude via Google Maps API?

前端 未结 8 1897
粉色の甜心
粉色の甜心 2020-12-23 15:12

How retrieve latitude and longitude via Google Maps API?

相关标签:
8条回答
  • 2020-12-23 15:30

    I am not sure if you are requesting a way of getting latitude and longitude via code(although you did say API :) ), If you are then there are a million posts out there which will show you how(including the ones above), but if you are interested in some kinda utility which just tells you where you are clicking , then Google-maps already comes with it. check out his post here on how to enable that functionality.

    http://varunpant.com/posts/find-longitude-and-latitude-in-google-maps

    0 讨论(0)
  • 2020-12-23 15:34

    If you need to find out the latitude and longitude of an address using the Google Maps API, you need to use the Google Maps Geocoding Service:

    var map = new GMap2(document.getElementById("map_canvas"));
    var geocoder = new GClientGeocoder();
    
    var address = "1600 Amphitheatre Parkway, Mountain  View";
    geocoder.getLatLng(address, function(point) {
             var latitude = point.y;
             var longitude = point.x;  
    
             // do something with the lat lng
        });
    

    If you would like to get the latitude and longitude of a position clicked on your Google map, you can do this in the click event:

    GEvent.addListener(map, "click", function(marker,point) {
            var latitude = point.y;
            var longitude = point.x;
    
            // do something with the lat/lng
        });
    
    0 讨论(0)
  • 2020-12-23 15:37

    With API v3

    google.maps.event.addListener(map, 'click', function(event) {
       alert( 'Lat: ' + event.latLng.lat() + ' and Longitude is: ' + event.latLng.lng() );
    });
    

    Likewise you can retrieve when a marker is dragged dropped :

    google.maps.event.addListener(marker, 'dragstart', function(event) {
       alert( 'Lat: ' + event.latLng.lat() + ' and Longitude is: ' + event.latLng.lng() );
    });
    
    0 讨论(0)
  • 2020-12-23 15:39

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

    more info here https://developers.google.com/maps/documentation/geocoding/

    0 讨论(0)
  • 2020-12-23 15:43

    I have found the result via the following method:

    http://maps.googleapis.com/maps/api/geocode/json?address=woking&sensor=false

      console.log(response.results[0].geometry.viewport.northeast.lat);
      console.log(response.results[0].geometry.viewport.northeast.lng);
    
    0 讨论(0)
  • 2020-12-23 15:45

    To add to RedBlueThing's answer you can do stuff like:

    geocoded_by :address
    
    def address
     [street, city, state, country].compact.join(', ')
    end
    

    to help you narrow down the exact location, otherwise if you just send in the street the geocoder gem just returns the first instance of that street it finds which could be anywhere in the world

    0 讨论(0)
提交回复
热议问题