1 (edited by magglemitch 2017-11-03 09:12:51)

Topic: Bounding the map to certain areas?

I'm wanting to show a map which is interactable (zoom, pan etc) - but want to limit where the user can go. For instance, setting a minimum and maximum zoom, and not being able to pan past certain bounds, say- a country.

Anyone done anything like this?

Re: Bounding the map to certain areas?

Hello.

Example:
http://infinity-code.com/atlas/online-m … ample.html

Kind Regards,
Infinity Code Team.

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

Re: Bounding the map to certain areas?

Awesome thanks for that! Just found those examples now.

Any idea on how I can customise the markers so that when I click on them, I can have information pop up about the location? I’ve tried to use the label thing but it seems quite limited and only really works for hover, or always on rather than tap on/tap off which I’d be going for. Perhaps just spawning some UI at the location of the marker - but I can’t seem to find how to get that.

Also - is it easy to create a few UI buttons in Unity that I can cycle between locations? Eg. pressing button 3 will make the map centre on marker 3.

Thank you!

Re: Bounding the map to certain areas?

You need to draw tooltips with uGUI.
Example:
http://infinity-code.com/atlas/online-m … ample.html

It is very easy:

OnlineMaps.instance.position = someMarker.position;
Kind Regards,
Infinity Code Team.

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

Re: Bounding the map to certain areas?

Thanks Alex. Figured that stuff out now.

Only thing I'm having trouble with now is implementing a route animation, being able to press a button to animate a marker moving along a route from marker to marker. I've found the example script "Move Marker On Route Example", but can't seem to figure out how to do this with multiple locations where it can figure out the whole route at once.

Any idea on how I'd be able to customise this so that I'm able to input several locations (or markers) and it can draw a route between them for a seamless animation?

Thanks!

Re: Bounding the map to certain areas?

If you are asking about waypoints, use OnlineMapsGoogleDirections.Params.waypoints to find the route using these points.
http://infinity-code.com/doxygen/online … 45a1012e3e

If you mean your own set of points, then just use it instead of searching for a route.

Kind Regards,
Infinity Code Team.

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

Re: Bounding the map to certain areas?

Thanks Alex. Does this work if my points cross over different countries? I want to be able to draw a line between somewhere in England, to America, and then to Australia (which I have markers set to). Then be able to press play and a marker will animate across it.

Is this something that can be done without a route, and just taking the marker location?

Re: Bounding the map to certain areas?

Just use your set of locations instead of a route search in «Move Marker On Route Example».

Kind Regards,
Infinity Code Team.

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

Re: Bounding the map to certain areas?

Ah yep. I've figured out how to draw a line from inputting a range of different coordinates (using the Using Drawing API example), but can't seem to figure out how to change the MoveMarkerOnRoute Example to take inputted coordinates instead of the GoogleDirection.

Re: Bounding the map to certain areas?

Something like this:

using System.Collections.Generic;
using UnityEngine;

namespace InfinityCode.OnlineMapsExamples
{
    /// <summary>
    /// Example of simulation movement marker on the route.
    /// </summary>
    public class MoveMarkerOnRouteExample2 : MonoBehaviour
    {
        /// <summary>
        /// Speed of movement (km/h).
        /// </summary>
        public float speed = 60;

        /// <summary>
        /// Move map to marker position
        /// </summary>
        public bool lookToMarker = false;

        /// <summary>
        /// Orient marker on next point.
        /// </summary>
        public bool orientMarkerOnNextPoint = false;

        /// <summary>
        /// Reference to marker
        /// </summary>
        private OnlineMapsMarker marker;

        /// <summary>
        /// Array of route points
        /// </summary>
        private List<Vector2> points;

        /// <summary>
        /// Current point index
        /// </summary>
        private int pointIndex = -1;

        /// <summary>
        /// Current step progress
        /// </summary>
        private float progress;

        private List<Vector2> routePoints;
        private OnlineMapsDrawingLine route;

        private void OnGUI()
        {
            if (GUILayout.Button("Start"))
            {
                if (marker == null) marker = OnlineMaps.instance.AddMarker(points[0], "Player");

                routePoints = new List<Vector2>(points);
                pointIndex = 0;
                progress = 0;
            }
        }

        private void Start()
        {
            points = new List<Vector2>();
            route = new OnlineMapsDrawingLine(points);
            OnlineMaps.instance.AddDrawingElement(route);

            OnlineMapsControlBase.instance.OnMapClick += OnMapClick;
        }

        private void OnMapClick()
        {
            Vector2 pos = OnlineMapsControlBase.instance.GetCoords();

            points.Add(pos);
            OnlineMaps.instance.AddMarker(pos, "Marker " + points.Count);

            OnlineMaps.instance.Redraw();
        }

        private void Update()
        {
            if (pointIndex == -1) return;

            // Start point
            Vector3 p1 = points[pointIndex];

            // End point
            Vector3 p2 = points[pointIndex + 1];

            // Total step distance
            float stepDistance = OnlineMapsUtils.DistanceBetweenPoints(p1, p2).magnitude;

            // Total step time
            float totalTime = stepDistance / speed * 3600;

            // Current step progress
            progress += Time.deltaTime / totalTime;

            if (progress < 1)
            {
                marker.position = Vector2.Lerp(p1, p2, progress);
                routePoints[0] = marker.position;

                // Orient marker
                if (orientMarkerOnNextPoint) marker.rotation = 1.25f - OnlineMapsUtils.Angle2D((Vector2)p1, (Vector2)p2) / 360f;
            }
            else
            {
                marker.position = p2;
                routePoints.RemoveAt(0);
                pointIndex++;
                progress = 0;
                if (pointIndex >= points.Count - 1)
                {
                    Debug.Log("Finish");
                    pointIndex = -1;
                    OnlineMaps.instance.RemoveDrawingElement(route);
                }
                else
                {
                    // Orient marker
                    if (orientMarkerOnNextPoint) marker.rotation = 1.25f - OnlineMapsUtils.Angle2D((Vector2)p2, points[pointIndex + 1]) / 360;
                }
            }

            if (lookToMarker) OnlineMaps.instance.position = marker.position;
            OnlineMaps.instance.Redraw();
        }
    }
}

Click on the map to create a waypoint.
Click Start to begin the animation.

Kind Regards,
Infinity Code Team.

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

11 (edited by magglemitch 2017-11-13 12:08:27)

Re: Bounding the map to certain areas?

That's perfect! Thank you so much - this has helped heaps.

One last thing - any idea on how to disable the location permission when building the app for Android? Since adding the map asset to my project the app now asks for location permissions on install.

Thanks!

Re: Bounding the map to certain areas?

This is because Online Maps has an Online Maps Location Service class that uses location permissions.
If you do not use this and have some experience, you can try to remove this class and all dependencies.
Do not forget to backup before you try.

Kind Regards,
Infinity Code Team.

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

Re: Bounding the map to certain areas?

Hi Alex
For me the route gets drawn correctly but the marker is not moving at all. There is no error in the script as well. Is there any prerequisite for this script?

What could I be missing?

Thanks

Re: Bounding the map to certain areas?

Hello.

I just checked the work of this script, and it works correctly.
Did you press "Start" button?

Kind Regards,
Infinity Code Team.

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

Re: Bounding the map to certain areas?

I am using this script:
http://infinity-code.com/atlas/online-m … ample.html

It doesn't have code for Start button.

Re: Bounding the map to certain areas?

I changed the code a little bit and am now able to make it work.

/*     INFINITY CODE 2013-2017      */
/*   http://www.infinity-code.com   */

using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

namespace InfinityCode.OnlineMapsExamples
{
    /// <summary>
    /// Example of simulation movement marker on the route.
    /// </summary>
    [AddComponentMenu ("Infinity Code/Online Maps/Examples (API Usage)/MoveMarkerOnRouteExample")]
    public class MoveMarkerOnRouteExample : MonoBehaviour
    {
        /// <summary>
        /// Start location name
        /// </summary>
        public string fromPlace = "Los Angeles";

        /// <summary>
        /// End location name
        /// </summary>
        public string toPlace = "Hollywood";

        /// <summary>
        /// Speed of movement (km/h).
        /// </summary>
        public float speed = 60;

        /// <summary>
        /// Move map to marker position
        /// </summary>
        public bool lookToMarker = false;

        /// <summary>
        /// Orient marker on next point.
        /// </summary>
        public bool orientMarkerOnNextPoint = false;

        /// <summary>
        /// Reference to marker
        /// </summary>
        private OnlineMapsMarker marker;

        /// <summary>
        /// Array of route points
        /// </summary>
        private OnlineMapsVector2d[] points;

        /// <summary>
        /// Current point index
        /// </summary>
        private int pointIndex = -1;

        /// <summary>
        /// Current step progress
        /// </summary>
        private double progress;

        private void Start ()
        {
            // Looking for a route between locations.
            OnlineMapsGoogleDirections.Find (fromPlace, toPlace).OnComplete += OnComplete;
        }

        private void resetProgress() 
        {
            if (marker == null)
                marker = OnlineMaps.instance.AddMarker (points [0], "Player");
            pointIndex = 0;
            progress = 0;
        }

        private void OnGUI ()
        {
            if (GUILayout.Button ("Start")) {
                resetProgress ();
            }
        }


        private void OnComplete (string response)
        {
            Debug.Log ("OnComplete");

            OnlineMapsGoogleDirectionsResult result = OnlineMapsGoogleDirections.GetResult (response);
            if (result == null || result.routes.Length == 0) {
                Debug.Log ("Something wrong");
                Debug.Log (response);
                return;
            }

            OnlineMapsGoogleDirectionsResult.Route firstRoute = result.routes [0];
            List<OnlineMapsGoogleDirectionsResult.Step> steps = firstRoute.legs.SelectMany (l => l.steps).ToList ();

            // Create a new marker in first point.
            marker = OnlineMaps.instance.AddMarker (steps [0].start_location, "Car");

            // Gets points of route.
            points = firstRoute.overview_polylineD;

            // Draw the route.
            OnlineMapsDrawingLine route = new OnlineMapsDrawingLine (points, Color.red, 3);
            OnlineMaps.instance.AddDrawingElement (route);

            pointIndex = 0;
        }

        private void Update ()
        {
            if (pointIndex == -1)
                return;

            // Start point
            OnlineMapsVector2d p1 = points [pointIndex];

            // End point
            OnlineMapsVector2d p2 = points [pointIndex + 1];

            // Total step distance
            double dx, dy;
            OnlineMapsUtils.DistanceBetweenPoints (p1.x, p1.y, p2.x, p2.y, out dx, out dy);
            double stepDistance = Math.Sqrt (dx * dx + dy * dy);

            // Total step time
            double totalTime = stepDistance / speed * 3600;

            // Current step progress
            progress += 10000 * Time.deltaTime / totalTime;
            Debug.Log ("Progress: " + progress);

            OnlineMapsVector2d position;

            if (progress < 1) {
                position = OnlineMapsVector2d.Lerp (p1, p2, progress);
                marker.SetPosition (position.x, position.y);

                // Orient marker
                if (orientMarkerOnNextPoint)
                    marker.rotation = 1.25f - OnlineMapsUtils.Angle2D ((Vector2)p1, (Vector2)p2) / 360f;
            } else {
                position = p2;
                marker.SetPosition (position.x, position.y);
                pointIndex++;
                progress = 0;
                if (pointIndex >= points.Length - 1) {
                    Debug.Log ("Finish");
                    pointIndex = -1;
                    resetProgress ();
                } else {
                    // Orient marker
                    if (orientMarkerOnNextPoint)
                        marker.rotation = 1.25f - OnlineMapsUtils.Angle2D (p2, points [pointIndex + 1]) / 360;
                }
            }

            if (lookToMarker)
                OnlineMaps.instance.SetPosition (position.x, position.y);
            OnlineMaps.instance.Redraw ();
        }
    }
}

Re: Bounding the map to certain areas?

Excellent. Just what I was looking for.