Search code examples
google-maps-api-3locationgwt2

Place a marker by Latitude and Longitude


I have this code that suppose to place the marker by the latlng

public void initialize() {  
    geocoder = Geocoder.create();
    myOptions = MapOptions.create();        
    myOptions.setZoom(18);
    myOptions.setMapTypeId(MapTypeId.HYBRID);
    myOptions.setMapMaker(true);
    map = GoogleMap.create(
        Document.get().getElementById("map_canvas"),myOptions);      

    GeocoderRequest request = GeocoderRequest.create();                 
    request.setLocation(LatLng.create(lat,lng));

    geocoder.geocode(request, new Callback() {
        public void handle(JsArray<GeocoderResult> rslts, GeocoderStatus status)
        {
            if (status == GeocoderStatus.OK) {
                location = rslts.get(0);
                map.setCenter(location.getGeometry().getLocation());
                marker = Marker.create();
                marker.setMap(map);
                marker.setPosition(location.getGeometry().getLocation());
            } 
        }
    });

When I tested with those coordinates (45.48592686713835, -73.49009937672122) I only get the nearest address which is the icon A. But what I really wanted is the exact location showed with the green arrow.

enter image description here

Am I missing something ?


Solution

  • You will always get the nearest address, if one may be determined, when using the Geocoder to do a reverse geocode (convert LatLng to address). If you want to use the original coordinates, just place the Marker using the original coordinates. If you are going to convert the LatLng to an address or the closest that the Google Geocoder can get to an address, the results will always give you the LatLng of the nearest known street_address, route, airport, transit_station, neighborhood, sublocality, locality, etc.