Skip to content

Instantly share code, notes, and snippets.

@KartikShrivastava
Forked from derme302/GoogleMap.cs
Last active November 20, 2018 16:38
Show Gist options
  • Save KartikShrivastava/dea6ce38e36e2d080a8c700c8c20566a to your computer and use it in GitHub Desktop.
Save KartikShrivastava/dea6ce38e36e2d080a8c700c8c20566a to your computer and use it in GitHub Desktop.
Load a Google Map into Unity3D
/*
* Based on the Google Maps for Unity Asset
* https://www.assetstore.unity3d.com/en/#!/content/3573
* However the relience on UniWeb has been removed
*
*
Getting Started
---------------
1. Create a 3d Plane game object.
2. Assign this GoogleMap.cs to your plane game object.
3. Setup the parameters in the inspector.
3.1 Get your static-map API key: https://developers.google.com/maps/documentation/static-maps/
3.2 If you want to control the center point and zoom level, make sure that
the Auto Locate Center box is unchecked. Otherwise the center point is
calculated using Markers and Path parameters.
4. Each location field can be an address or longitude / latitude.
5. The markers add pins onto the map, with a single letter label. This label
will only display on mid size markers.
6. The paths add straight lines on the map, between a set of locations.
7. For in depth information on how the GoogleMap component uses the Google
Maps API, see:
https://developers.google.com/maps/documentation/staticmaps/#quick_example
*/
using UnityEngine;
using System.Collections;
public class GoogleMap : MonoBehaviour {
public enum MapType {
RoadMap,
Satellite,
Terrain,
Hybrid
}
public string GoogleApiKey;
public bool loadOnStart = true;
public bool autoLocateCenter = true;
public GoogleMapLocation centerLocation;
public int zoom = 13;
public MapType mapType;
public int size = 512;
public bool doubleResolution = false;
public GoogleMapMarker[] markers;
public GoogleMapPath[] paths;
private Renderer m_renderer;
void Start() {
m_renderer = GetComponent<Renderer> ();
if (loadOnStart)
Refresh();
}
public void Refresh() {
if (autoLocateCenter && (markers.Length == 0 && paths.Length == 0)) {
Debug.LogError("Auto Center will only work if paths or markers are used.");
}
StartCoroutine(_Refresh());
}
IEnumerator _Refresh() {
string url = "http://maps.googleapis.com/maps/api/staticmap";
string qs = "";
if (!autoLocateCenter) {
if (centerLocation.address != "")
qs += "center=" + WWW.UnEscapeURL(centerLocation.address);
else
qs += "center=" + WWW.UnEscapeURL(string.Format("{0},{1}", centerLocation.latitude, centerLocation.longitude));
qs += "&zoom=" + zoom.ToString();
}
qs += "&size=" + WWW.UnEscapeURL(string.Format("{0}x{0}", size));
qs += "&scale=" + (doubleResolution ? "2" : "1");
qs += "&maptype=" + mapType.ToString().ToLower();
var usingSensor = false;
#if UNITY_IPHONE
usingSensor = Input.location.isEnabledByUser && Input.location.status == LocationServiceStatus.Running;
#endif
qs += "&sensor=" + (usingSensor ? "true" : "false");
foreach (var i in markers) {
qs += "&markers=" + string.Format("size:{0}|color:{1}|label:{2}", i.size.ToString().ToLower(), i.color, i.label);
foreach (var loc in i.locations) {
if (loc.address != "")
qs += "|" + WWW.UnEscapeURL(loc.address);
else
qs += "|" + WWW.UnEscapeURL(string.Format("{0},{1}", loc.latitude, loc.longitude));
}
}
foreach (var i in paths) {
qs += "&path=" + string.Format("weight:{0}|color:{1}", i.weight, i.color);
if (i.fill)
qs += "|fillcolor:" + i.fillColor;
foreach (var loc in i.locations) {
if (loc.address != "")
qs += "|" + WWW.UnEscapeURL(loc.address);
else
qs += "|" + WWW.UnEscapeURL(string.Format("{0},{1}", loc.latitude, loc.longitude));
}
}
qs += "&key=" + WWW.UnEscapeURL(GoogleApiKey);
WWW req = new WWW(url + "?" + qs);
Debug.Log(url + "?" + qs);
// Create a texture in DXT1 format
m_renderer.material.mainTexture = new Texture2D(size, size, TextureFormat.DXT1, false);
while (!req.isDone)
yield return null;
if (req.error == null)
req.LoadImageIntoTexture((Texture2D)m_renderer.material.mainTexture);
}
}
public enum GoogleMapColor {
black,
brown,
green,
purple,
yellow,
blue,
gray,
orange,
red,
white
}
[System.Serializable]
public class GoogleMapLocation {
public string address;
public float latitude;
public float longitude;
}
[System.Serializable]
public class GoogleMapMarker {
public enum GoogleMapMarkerSize {
Tiny,
Small,
Mid
}
public GoogleMapMarkerSize size;
public GoogleMapColor color;
public string label;
public GoogleMapLocation[] locations;
}
[System.Serializable]
public class GoogleMapPath {
public int weight = 5;
public GoogleMapColor color;
public bool fill = false;
public GoogleMapColor fillColor;
public GoogleMapLocation[] locations;
}
@shawn107
Copy link

how do you make a texture in dxt1 texture

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment