Lon/Lat at mouse click
I want to get the lon/lat when i click on the map,for now i use map at specific location and i can update the map on runtime.
what i think i need is getting a position on a other tile and convert it to lonlat?
did you get an answer?
@neftosis You can get lat/long of a mouse click using the code here https://github.com/mapbox/mapbox-unity-sdk/blob/85fef42a7135b86751895722aa9b89a900751891/sdkproject/Assets/Mapbox/Examples/Scripts/QuadTreeCameraMovement.cs#L157
It comes with an example script in the SDK.
Thank you so much my friend!
@atripathi-mb I understood the example, but I think that just gets the lat lon of camera bounds, so when you move the map the lat long will change. But how can mouse pointer real be made?
Like this one? https://www.mapbox.com/mapbox-gl-js/example/mouse-position/
Ok. I think I found a solution but I have no idea why it works.
So in the example we assign distance of camera to ground plane to z. (Like this works just then you move the camera, so camera have new positions).
var worldPos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10.0f)); // With this it seems it works for mouse position not camera position
@atripathi-mb Ok so when I get latlongDelta and display it - it's a vector 2d in the console I can debug it and get where geo to world is _mapManager.GeoToWorldPosition(latlongDelta, true)
Latitude: 40.8732077404502 Longitude: -73.618106881813MyPOS: (-0.2, 1.8, -1.2)GeotoWOrld(0.0, 0.0, -11.0) UnityEngine.Debug:Log(Object)
Given this code from spawnOnMap:
var instance = Instantiate(_markerPrefab);
instance.transform.localPosition = _map.GeoToWorldPosition(_locations[i], true);
combined with the following from Quadcontroller
var mousePosScreen = Input.mousePosition;
//assign distance of camera to ground plane to z, otherwise ScreenToWorldPoint() will always return the position of the camera
//http://answers.unity3d.com/answers/599100/view.html
mousePosScreen.z = _referenceCamera.transform.localPosition.y;
var pos = _referenceCamera.ScreenToWorldPoint(mousePosScreen);
var latlongDelta = _mapManager.WorldToGeoPosition(pos);
Debug.Log("Latitude: " + latlongDelta.x + " Longitude: " + latlongDelta.y);
//_mapManager.UpdateMap(latlongDelta, _mapManager.Zoom);
it seems like I can't just use the latlongDetla's Vector2d properly to spawn it affixed to the map. EDIT: I have already changed variables away from array. when I assign like so
var markerInstance = Instantiate(_markerPrefab, mousePosScreen, Quaternion.identity);
It's able to put it on the worldspace of the mouse click but not the actual place where i want to
I know I have to somehow instantiate it with WorldToGeoPosition but nothing I'm trying is working so far...
ok - to try and distill the question - How do I take my instance.transform.localposition.z sit at the desired distance based on the input mouse position
Reading https://github.com/mapbox/mapbox-unity-sdk/issues/123 and https://github.com/mapbox/mapbox-unity-sdk/issues/441 first thanks man
Ok I have read everything and everything I have tried above fails. I can place objects fine with spawnonmap but my attempt to simply place something on mouse click is failing horribly. Please please help me - all the tutorials are referencing factories that I don't even see anymore, please let me know what I'm doing wrong! @atripathi-mb @brnkhy
Or alternately if I could get the tile or map position on mouse click from a raycast please advise!?
n't even see anymo
Hey if you would like to instantiate and place objects on the fly have a look at the following code.
This code is to be added to a new game component called something like (PlacePrefabScript)
attach the following script to the game object and then make sure to serialize you desired spawnable prefab.
make sure that your map has relevant mesh colliders enable otherwise your object will not spawn or snap to grid.
P.S.(I am still trying to figure out how to save that prefab in that position forever / any advice would be highly appreciated - either way this placement script is a good place to start)
using System.Collections; using System.Collections.Generic; using System; using UnityEngine; using Mapbox.Utils; using Mapbox.Unity.Map; using Mapbox.Unity.MeshGeneration.Factories; using Mapbox.Unity.Utilities;
public class PlacementControllerScript : MonoBehaviour { [SerializeField] private GameObject placeableObjectPrefabs;
private GameObject currentPlaceableObject;
private KeyCode ObjectHotKey = KeyCode.B;
private float mouseWheelRotation;
private int currentPrefabIndex = -1;
[SerializeField] public AbstractMap _map;
private void Update() { HandleNewObjectHotkey();
if (currentPlaceableObject != null)
{
MoveCurrentObjectToMouse();
RotateFromMouseWheel();
ReleaseIfClicked();
}
}
private void HandleNewObjectHotkey()
{
if (Input.GetKeyDown(KeyCode.B))
{
if (currentPlaceableObject != null)
{
Destroy(currentPlaceableObject);
}
else
{
currentPlaceableObject = Instantiate(placeableObjectPrefabs);
Debug.Log("OBJECT INSTANTIATED");
}
}
}
private void MoveCurrentObjectToMouse()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitInfo;
if (Physics.Raycast(ray, out hitInfo))
{
currentPlaceableObject.transform.position = hitInfo.point;
currentPlaceableObject.transform.rotation = Quaternion.FromToRotation(Vector3.up, hitInfo.normal);
}
}
private void RotateFromMouseWheel()
{
//Debug.Log(Input.mouseScrollDelta);
mouseWheelRotation += Input.mouseScrollDelta.y;
currentPlaceableObject.transform.Rotate(Vector3.up, mouseWheelRotation * 5f);
}
private void ReleaseIfClicked()
{
if (Input.GetKeyDown(KeyCode.N))
{
currentPlaceableObject = null;
}
}
}