Skip to content

Instantly share code, notes, and snippets.

@jaakla
Created October 12, 2018 14:31
Show Gist options
  • Save jaakla/947ab4597aa7b293bd8f7a827d939403 to your computer and use it in GitHub Desktop.
Save jaakla/947ab4597aa7b293bd8f7a827d939403 to your computer and use it in GitHub Desktop.
LBS 2018
package com.ut.example.jaak.myapplication;
import android.annotation.SuppressLint;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.support.annotation.NonNull;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.widget.Toast;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.TileOverlay;
import com.google.android.gms.maps.model.TileOverlayOptions;
import com.google.android.gms.maps.model.TileProvider;
import com.google.android.gms.maps.model.UrlTileProvider;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.Locale;
public class MapsActivity extends FragmentActivity implements
OnMapReadyCallback, GoogleMap.OnMyLocationButtonClickListener,
GoogleMap.OnMyLocationClickListener {
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
@SuppressLint("MissingPermission")
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
// Add a marker in Sydney and move the camera
LatLng tartu = new LatLng(58.3779, 26.729);
mMap.addMarker(new MarkerOptions().position(tartu).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(tartu));
mMap.moveCamera(CameraUpdateFactory.zoomTo(14));
mMap.setMyLocationEnabled(true);
mMap.setOnMyLocationButtonClickListener(this);
mMap.setOnMyLocationClickListener(this);
TileProvider tileProvider = new UrlTileProvider(256, 256) {
@Override
public URL getTileUrl(int x, int y, int zoom) {
/* Define the URL pattern for the tile images */
String s = String.format("http://tiles.maaamet.ee/tm/tms/1.0.0/kaart@GMC/%d/%d/%d.png",
zoom, x, (1 << zoom) - 1 - y);
if (!checkTileExists(x, y, zoom)) {
return null;
}
try {
return new URL(s);
} catch (MalformedURLException e) {
throw new AssertionError(e);
}
}
/*
* Check that the tile server supports the requested x, y and zoom.
* Complete this stub according to the tile range you support.
* If you support a limited range of tiles at different zoom levels, then you
* need to define the supported x, y range at each zoom level.
*/
private boolean checkTileExists(int x, int y, int zoom) {
int minZoom = 12;
int maxZoom = 18;
if ((zoom < minZoom || zoom > maxZoom)) {
return false;
}
return true;
}
};
TileOverlay tileOverlay = mMap.addTileOverlay(new TileOverlayOptions()
.tileProvider(tileProvider));
}
@Override
public boolean onMyLocationButtonClick() {
Toast.makeText(this, "MyLocation button clicked", Toast.LENGTH_SHORT).show();
return false;
}
@Override
public void onMyLocationClick(@NonNull Location location) {
Geocoder geocoder = new Geocoder(this, Locale.JAPANESE);
try {
List<Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
if (addresses.size() > 0) {
Address fetchedAddress = addresses.get(0);
StringBuilder strAddress = new StringBuilder();
for (int i = 0; i <= fetchedAddress.getMaxAddressLineIndex(); i++) {
strAddress.append(fetchedAddress.getAddressLine(i)).append(" ");
}
Toast.makeText(this, "Current location:\n" + strAddress.toString(), Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "Current location (No address):\n" + location, Toast.LENGTH_LONG).show();
}
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(this, "Current location (Ex):\n" + location, Toast.LENGTH_LONG).show();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment