1 (edited by digitalrock 2017-01-12 22:10:03)

Topic: OnMapClick adds a marker to the wrong position

I'm not sure what happened as I'm sure this was working.  I have the latest version of online maps v2.5.1.5 and with the following code when I click on screen I get a marker in the wrong position from where I clicked:

    private void OnMapClick()
    {

        double lat;
        double lng;

        // Get the coordinates where the user clicked.
        OnlineMapsControlBase.instance.GetCoords (out lng, out lat);

        Debug.Log ("mouse click lat/lon: "+lat+"/"+lng);

        OnlineMapsGoogleGeocoding.Find(null, lat+","+lng).OnComplete += delegate(string s)
        {
            try
            {
                Vector2 position = OnlineMapsGoogleGeocoding.GetCoordinatesFromResult(s);
                if (position != Vector2.zero) _marker = OnlineMaps.instance.AddMarker(position,"");
                else Debug.Log(s);
            }
            catch
            {
                Debug.Log(s);
            }
        };
    }

The marker lands close, but it's very random as to where it lands in relation to where I've clicked.  I'm using OpenStreetMap zoom 14 with Tileset.  I have already tried the GetCoord() using the Vector2 rather than double.   Do you see anything wrong with this code?  Does it sound like something you've seen before?   What could I be doing wrong?

EDIT:  Added more details above.


Thanks

Re: OnMapClick adds a marker to the wrong position

In doing some research I have found the following from a different thread on this forum, and it worked perfectly. 

    private void OnMapClick()
    {
        Vector2 coords = OnlineMapsControlBase.instance.GetCoords();
        OnlineMaps.instance.AddMarker(coords);
        points.Add(coords);
        if (points.Count == 2) OnlineMaps.instance.AddDrawingElement(new OnlineMapsDrawingLine(points, Color.red, 3));
    }


Now this makes me wonder by my original implementation was so convoluted.  All I ever wanted was the latitude and longitude for where the user clicked.  My OP was what I came up with after searching the examples and docs, so clearly when I think I know how to use this asset, I really don't.

Re: OnMapClick adds a marker to the wrong position

Hello

Your original code did not work properly, because you send the coordinates to Google Geocoding API for reverse geocoding.
This service searches for information about the place by coordinates (approximate).
Usually it sticks to buildings, roads, etc.
So coordinate returned by this service can differ from coordinates of the request.

Kind Regards,
Infinity Code Team.

Boost your productivity a lot and immediately using Ultimate Editor Enhancer. Trial and non-commerce versions available.