Author Topic: Add a city in mouse position  (Read 1922 times)

Siwar

  • Newbie
  • *
  • Posts: 35
    • View Profile
Add a city in mouse position
« on: May 11, 2016, 05:59:04 AM »
Hello,

I need to add cities to the map. I don't really need to specify its frontiers, just some sort of marker and a label.

So my first question is can I add my new city to the list of the map's cities?
And my second question is how do I get the location in the map that matches the mouse position ?

As always, your help is much appreciated.

Kronnect

  • Administrator
  • Hero Member
  • *****
  • Posts: 5390
    • View Profile
Re: Add a city in mouse position
« Reply #1 on: May 11, 2016, 06:50:18 AM »
Hi Siwar,

You may add new cities in two ways:

- Using the map editor: this will add permanent cities to the geodata file. Select the map in the hierarchy, go to the inspector and click "Map Editor". Then select the third button "Create" in the tools palette and choose city. Click over the map (in Scene View) where you want to add the new city and fill in its properties in the fields above the toolbar palette. Add any number of cities this way and remember to hit "Save" - don't play the scene or you will loose any change. Also, remember that Map Editor is not available during play mode.

- Adding cities during play mode: in this case, you can add any number of cities using the API. The code would be very similar to that for adding markers:

Code
		// use a custom location lat/lon. Example adding New Work:
map.calc.fromLatDec = 40.71f; // 40.71 decimal degrees north
map.calc.fromLonDec = -74.00f; // 74.00 decimal degrees to the west
map.calc.fromUnit = UNIT_TYPE.DecimalDegrees;
map.calc.Convert();
planeLocation = map.calc.toPlaneLocation;

// Send the prefab to the AddMarker API setting a scale of 0.1f (this depends on your marker scales)
GameObject city = Instantiate(Resources.Load<GameObject>("CitySprite"));

map.AddMarker(city, planeLocation, 0.02f);

Regards.

Siwar

  • Newbie
  • *
  • Posts: 35
    • View Profile
Re: Add a city in mouse position
« Reply #2 on: May 11, 2016, 07:02:33 AM »
Hi,

Thanks for the reply.

However, how do I add the city in the exact location of my mouse. How do I get the (x,y) of the mouse position in the map?

Kronnect

  • Administrator
  • Hero Member
  • *****
  • Posts: 5390
    • View Profile
Re: Add a city in mouse position
« Reply #3 on: May 11, 2016, 07:44:00 AM »
Alright, that's even easier. Just use map.cursorLocation property.

Code
map.AddMarker(cityGameObject, map.cursorLocation, 0.02f);

Siwar

  • Newbie
  • *
  • Posts: 35
    • View Profile
Re: Add a city in mouse position
« Reply #4 on: May 11, 2016, 07:48:35 AM »
Thanks for your help :)