-1

I am making pokemon go-like game. I want to update my map wherever I go using my GPS. But for now, I want to update the coordinates. Shall I use for loop?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class GoogleMap : MonoBehaviour {
    public RawImage image;
    string url;

    public float lon, lat;

    LocationInfo locationInfo;

    public enum mapType {roadmap, satellite, hybrid, terrain}
    public mapType mapSelected;
    public int scale;

    IEnumerator Map(){
        //url = "https://maps.googleapis.com/maps/api/staticmap?center=Brooklyn+Bridge,New+York,NY&zoom=13&size=600x300&maptype=roadmap&markers=color:blue%7Clabel:S%7C40.702147,-74.015794&markers=color:green%7Clabel:G%7C40.711614,-74.012318&markers=color:red%7Clabel:C%7C40.718217,-73.998284&key=AIzaSyB41TT9I-4eK5zcl_n4tarK79QzgyVD4lc";
        //url = "https://maps.googleapis.com/maps/api/staticmap?center=14.679046,120.540874&zoom=19&size=640x640&key=AIzaSyB41TT9I-4eK5zcl_n4tarK79QzgyVD4lc&scale=2";
        url = "https://maps.googleapis.com/maps/api/staticmap?&key=AIzaSyB41TT9I-4eK5zcl_n4tarK79QzgyVD4lc&center=14.679155295230377,120.54045265601641&zoom=18&format=png&maptype=roadmap&style=element:geometry%7Ccolor:0x1d2c4d&style=element:labels.text.fill%7Ccolor:0x8ec3b9&style=element:labels.text.stroke%7Ccolor:0x1a3646&style=feature:administrative.country%7Celement:geometry.stroke%7Ccolor:0x4b6878&style=feature:administrative.land_parcel%7Celement:labels.text.fill%7Ccolor:0x64779e&style=feature:administrative.province%7Celement:geometry.stroke%7Ccolor:0x4b6878&style=feature:landscape.man_made%7Celement:geometry.stroke%7Ccolor:0x334e87&style=feature:landscape.natural%7Celement:geometry%7Ccolor:0x023e58&style=feature:poi%7Celement:geometry%7Ccolor:0x283d6a&style=feature:poi%7Celement:labels.text.fill%7Ccolor:0x6f9ba5&style=feature:poi%7Celement:labels.text.stroke%7Ccolor:0x1d2c4d&style=feature:poi.business%7Cvisibility:off&style=feature:poi.park%7Celement:geometry.fill%7Ccolor:0x023e58&style=feature:poi.park%7Celement:labels.text%7Cvisibility:off&style=feature:poi.park%7Celement:labels.text.fill%7Ccolor:0x3C7680&style=feature:road%7Celement:geometry%7Ccolor:0x304a7d&style=feature:road%7Celement:labels.text.fill%7Ccolor:0x98a5be&style=feature:road%7Celement:labels.text.stroke%7Ccolor:0x1d2c4d&style=feature:road.highway%7Celement:geometry%7Ccolor:0x2c6675&style=feature:road.highway%7Celement:geometry.stroke%7Ccolor:0x255763&style=feature:road.highway%7Celement:labels.text.fill%7Ccolor:0xb0d5ce&style=feature:road.highway%7Celement:labels.text.stroke%7Ccolor:0x023e58&style=feature:transit%7Celement:labels.text.fill%7Ccolor:0x98a5be&style=feature:transit%7Celement:labels.text.stroke%7Ccolor:0x1d2c4d&style=feature:transit.line%7Celement:geometry.fill%7Ccolor:0x283d6a&style=feature:transit.station%7Celement:geometry%7Ccolor:0x3a4762&style=feature:water%7Celement:geometry%7Ccolor:0x0e1626&style=feature:water%7Celement:labels.text.fill%7Ccolor:0x4e6d70&size=640x640&scale=2";
        WWW www = new WWW (url);
        yield return www;
        image.texture = www.texture;
        image.SetNativeSize ();
    }

    // Use this for initialization
    void Start () {
        image = gameObject.GetComponent<RawImage> ();
        StartCoroutine (Map());
    }

    // Update is called once per frame
    void Update () {

    }
}
Andrii Omelchenko
  • 13,183
  • 12
  • 43
  • 79

2 Answers2

2

Just put the code in the Map() function inside a while loop and use WaitForSeconds to do the waiting.

//Every 2 seconds
float everyXtime = 2;

IEnumerator Map()
{
    while (true)
    {
        //url = "https://maps.googleapis.com/maps/api/staticmap?center=Brooklyn+Bridge,New+York,NY&zoom=13&size=600x300&maptype=roadmap&markers=color:blue%7Clabel:S%7C40.702147,-74.015794&markers=color:green%7Clabel:G%7C40.711614,-74.012318&markers=color:red%7Clabel:C%7C40.718217,-73.998284&key=AIzaSyB41TT9I-4eK5zcl_n4tarK79QzgyVD4lc";
        //url = "https://maps.googleapis.com/maps/api/staticmap?center=14.679046,120.540874&zoom=19&size=640x640&key=AIzaSyB41TT9I-4eK5zcl_n4tarK79QzgyVD4lc&scale=2";
        url = "https://maps.googleapis.com/maps/api/staticmap?&key=AIzaSyB41TT9I-4eK5zcl_n4tarK79QzgyVD4lc&center=14.679155295230377,120.54045265601641&zoom=18&format=png&maptype=roadmap&style=element:geometry%7Ccolor:0x1d2c4d&style=element:labels.text.fill%7Ccolor:0x8ec3b9&style=element:labels.text.stroke%7Ccolor:0x1a3646&style=feature:administrative.country%7Celement:geometry.stroke%7Ccolor:0x4b6878&style=feature:administrative.land_parcel%7Celement:labels.text.fill%7Ccolor:0x64779e&style=feature:administrative.province%7Celement:geometry.stroke%7Ccolor:0x4b6878&style=feature:landscape.man_made%7Celement:geometry.stroke%7Ccolor:0x334e87&style=feature:landscape.natural%7Celement:geometry%7Ccolor:0x023e58&style=feature:poi%7Celement:geometry%7Ccolor:0x283d6a&style=feature:poi%7Celement:labels.text.fill%7Ccolor:0x6f9ba5&style=feature:poi%7Celement:labels.text.stroke%7Ccolor:0x1d2c4d&style=feature:poi.business%7Cvisibility:off&style=feature:poi.park%7Celement:geometry.fill%7Ccolor:0x023e58&style=feature:poi.park%7Celement:labels.text%7Cvisibility:off&style=feature:poi.park%7Celement:labels.text.fill%7Ccolor:0x3C7680&style=feature:road%7Celement:geometry%7Ccolor:0x304a7d&style=feature:road%7Celement:labels.text.fill%7Ccolor:0x98a5be&style=feature:road%7Celement:labels.text.stroke%7Ccolor:0x1d2c4d&style=feature:road.highway%7Celement:geometry%7Ccolor:0x2c6675&style=feature:road.highway%7Celement:geometry.stroke%7Ccolor:0x255763&style=feature:road.highway%7Celement:labels.text.fill%7Ccolor:0xb0d5ce&style=feature:road.highway%7Celement:labels.text.stroke%7Ccolor:0x023e58&style=feature:transit%7Celement:labels.text.fill%7Ccolor:0x98a5be&style=feature:transit%7Celement:labels.text.stroke%7Ccolor:0x1d2c4d&style=feature:transit.line%7Celement:geometry.fill%7Ccolor:0x283d6a&style=feature:transit.station%7Celement:geometry%7Ccolor:0x3a4762&style=feature:water%7Celement:geometry%7Ccolor:0x0e1626&style=feature:water%7Celement:labels.text.fill%7Ccolor:0x4e6d70&size=640x640&scale=2";
        WWW www = new WWW(url);
        yield return www;
        image.texture = www.texture;
        image.SetNativeSize();

        //Wait for that X time
        yield return new WaitForSeconds(everyXtime);
    }
}
Programmer
  • 121,791
  • 22
  • 236
  • 328
  • How can I use that with my GPS? – Clyde Dexter Santiago Nov 14 '17 at 15:45
  • WWW www = new WWW(url); is executing every second. Isn't that bad? – Clyde Dexter Santiago Nov 14 '17 at 15:48
  • The-same thing. Initialize the GPS first in the `Start` function like [this](https://docs.unity3d.com/ScriptReference/LocationService.Start.html) then use `Input.location.lastData.latitude` and `Input.location.lastData.longitude` to construct the longitude and latitude in the url then pass them to to the url. – Programmer Nov 14 '17 at 15:55
  • Executing that every second is fine. Every frame is bad. I wish there is a better way Google provides access to their map that does not require connecting and disconnecting but the problem is that they want you to pay for every request. That's how they make their money. – Programmer Nov 14 '17 at 15:58
  • I used Fake gps and changed my location. I checked my location in Maps (Android App) it was updated. But when I checked my app, It stays on the same place. Check my code https://imgur.com/a/x8VMb – Clyde Dexter Santiago Nov 14 '17 at 16:18
  • You missed the crucial part of this answer in your code from the screenshot. Where is the `while` loop? Where is `WaitForSeconds`? Line 43 to 50 should be in a `while` loop. Read this answer again – PassetCronUs Nov 15 '17 at 20:14
  • Don't forget to accept this answer if it solved the problem asked in this question – Programmer Nov 17 '17 at 15:49
1

You can use raw NMEA data from GPS via NmeaListener like in this answer on java:

public class CustomNmeaListener implements GpsStatus.NmeaListener{

    private GoogleMap mGoogleMap;
    private Marker mMarkerPosition = null;
    private BitmapDescriptor mMarkerMoveDescriptor;
    private BitmapDescriptor mMarkerStopDescriptor;

    public CustomNmeaListener(GoogleMap googleMap, int markerMoveResource, int markerStopResource){
        this.mGoogleMap = googleMap;

        mMarkerMoveDescriptor = BitmapDescriptorFactory.fromResource(markerMoveResource);
        mMarkerStopDescriptor = BitmapDescriptorFactory.fromResource(markerStopResource);
    }

    @Override
    public void onNmeaReceived(long timestamp, String nmea) {
        double latitude;
        double longitude;
        float speed;
        float angle;

        // parse NMEA RMC sentence
        // Example $GPRMC,123519,A,4807.038,N,01131.000,E,022.4,084.4,230394,003.1,W*6A
        //   nmea    [0]   [1]  [2]  [3]   [4]   [5]   [6] [7]   [8]   [9]   [10]  [11]
        if (nmea.startsWith("$GPRMC")) {

            String[] nmeaParts = nmea.split(",");

            // if RMC data valid ("active")
            if (nmeaParts[2].equals("A")) {
                latitude = parseLatitude(nmeaParts[3], nmeaParts[4]);
                longitude = parseLongitude(nmeaParts[5], nmeaParts[6]);
                speed = parseSpeed(nmeaParts[7]);
                angle = parseAngle(nmeaParts[8]);

                // remove marker on "old" position
                if (mMarkerPosition != null) {
                    mMarkerPosition.remove();
                }

                MarkerOptions positionMarkerOptions;

                if (speed > 0) {
                    positionMarkerOptions = new MarkerOptions()
                            .position(new LatLng(latitude, longitude))
                            .icon(mMarkerMoveDescriptor)
                            .anchor(0.5f, 0.5f)
                            .rotation(angle);
                } else {
                    positionMarkerOptions = new MarkerOptions()
                            .position(new LatLng(latitude, longitude))
                            .icon(mMarkerStopDescriptor)
                            .anchor(0.5f, 0.5f)
                            .rotation(0);
                }
                mMarkerPosition = mGoogleMap.addMarker(positionMarkerOptions);
            }
        }

    }

    static float parseLatitude(String lat, String sign) {
        float latitude = Float.parseFloat(lat.substring(2)) / 60.0f;
        latitude +=  Float.parseFloat(lat.substring(0, 2));
        if(sign.startsWith("S")) {
            latitude = -latitude;
        }
        return latitude;
    }

    static float parseLongitude(String lon, String sign) {
        float longitude = Float.parseFloat(lon.substring(3)) / 60.0f;
        longitude +=  Float.parseFloat(lon.substring(0, 3));
        if(sign.startsWith("W")) {
            longitude = -longitude;
        }
        return longitude;
    }

    static float parseSpeed(String knots) {
        float speed;
        try {
            speed = Float.parseFloat(knots);
        } catch (Exception e) {
            speed = 0;
        }
        return speed;
    }

    static float parseAngle(String ang) {
        float angle;
        try {
            angle = Float.parseFloat(ang);
        } catch (Exception e) {
            angle = 0;
        }
        return angle;
    }
}

and you can set update interval via LocationManager.requestLocationUpdates() like that way:

 try {
    mLocationManager.requestLocationUpdates(
            LocationManager.GPS_PROVIDER,
            <your_update_interval>,
            LOCATION_DISTANCE,
            this
    );
 } catch (java.lang.SecurityException ex) {
    Log.d(TAG, "fail to request location update, ignore", ex);
 }
Andrii Omelchenko
  • 13,183
  • 12
  • 43
  • 79