Created
June 17, 2017 12:49
-
-
Save shelaf/0fea130e5106609b2d8aaa8aaa702eb3 to your computer and use it in GitHub Desktop.
Google Map API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
apply plugin: 'com.android.application' | |
apply plugin: 'com.cookpad.android.licensetools' | |
android { | |
compileSdkVersion 26 | |
buildToolsVersion "26.0.0" | |
defaultConfig { | |
applicationId "com.example.shela.myapplication" | |
minSdkVersion 23 | |
targetSdkVersion 26 | |
versionCode 1 | |
versionName "1.0" | |
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" | |
} | |
buildTypes { | |
release { | |
minifyEnabled false | |
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' | |
} | |
} | |
productFlavors { | |
} | |
} | |
dependencies { | |
compile fileTree(include: ['*.jar'], dir: 'libs') | |
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { | |
exclude group: 'com.android.support', module: 'support-annotations' | |
}) | |
compile 'com.android.support:appcompat-v7:26.+' | |
compile 'com.google.android.gms:play-services-maps:10.2.0' | |
compile 'com.google.maps:google-maps-services:0.1.20' | |
compile 'org.slf4j:slf4j-nop:1.7.25' | |
testCompile 'junit:junit:4.12' | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- artifact: com.android.support:+:+ | |
name: Android Support Libraries | |
copyrightHolder: The Android Open Source Project | |
license: The Apache Software License, Version 2.0 | |
licenseUrl: http://www.apache.org/licenses/LICENSE-2.0.txt | |
- artifact: com.google.android.gms:+:+ | |
name: Google Play Services | |
copyrightHolder: The Android Open Source Project | |
license: Apache License, Version 2.0 | |
- artifact: com.google.code.gson:gson:+ | |
name: Google Gson | |
copyrightHolder: Google Inc. | |
license: Apache License, Version 2.0 | |
url: https://github.com/google/gson | |
- artifact: com.google.maps:google-maps-services:+ | |
name: Java Client for Google Maps Services | |
copyrightHolder: Google Inc. | |
license: The Apache Software License, Version 2.0 | |
licenseUrl: http://www.apache.org/licenses/LICENSE-2.0.txt | |
url: https://github.com/googlemaps/google-maps-services-java | |
- artifact: org.slf4j:+:+ | |
name: SLF4J | |
copyrightHolder: QOS.ch | |
license: MIT license | |
licenseUrl: http://www.opensource.org/licenses/mit-license.php | |
url: http://www.slf4j.org | |
- artifact: joda-time:joda-time:+ | |
name: Joda-Time | |
copyrightHolder: Joda.org | |
license: Apache License, Version 2.0 | |
licenseUrl: http://www.apache.org/licenses/LICENSE-2.0.txt | |
url: http://www.joda.org/joda-time/ | |
- artifact: com.squareup.okhttp:+:+ | |
name: OkHttp | |
copyrightHolder: Square, Inc. | |
license: Apache License, Version 2.0 | |
url: http://square.github.io/okhttp/ | |
- artifact: com.squareup.okio:okio:+ | |
name: Okio | |
copyrightHolder: Square, Inc. | |
license: Apache License, Version 2.0 | |
url: https://github.com/square/okio |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.shela.myapplication; | |
import android.Manifest; | |
import android.content.pm.PackageManager; | |
import android.os.Bundle; | |
import android.support.v4.app.ActivityCompat; | |
import android.support.v4.app.FragmentActivity; | |
import android.support.v4.content.ContextCompat; | |
import android.util.Log; | |
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.PolylineOptions; | |
import com.google.maps.DirectionsApi; | |
import com.google.maps.GeoApiContext; | |
import com.google.maps.GeocodingApi; | |
import com.google.maps.errors.ApiException; | |
import com.google.maps.model.DirectionsResult; | |
import com.google.maps.model.GeocodingResult; | |
import com.google.maps.model.TravelMode; | |
import java.io.IOException; | |
import java.net.InetSocketAddress; | |
import java.net.Proxy; | |
import java.util.List; | |
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback { | |
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. | |
*/ | |
@Override | |
public void onMapReady(GoogleMap googleMap) { | |
mMap = googleMap; | |
// 位置情報取得権限を要求する(不完全) | |
// see http://qiita.com/akitaika_/items/27c4eaeea6960ac9bb0d | |
// see http://wptrafficanalyzer.in/blog/showing-current-location-in-google-maps-using-api-v2-with-supportmapfragment/ | |
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) | |
!= PackageManager.PERMISSION_GRANTED) { | |
if (ActivityCompat.shouldShowRequestPermissionRationale(this, | |
Manifest.permission.ACCESS_FINE_LOCATION)) { | |
// この画面側で処理継続的な選択をされた場合は、別途 ActivityCompat.requestPermissions をするべし | |
ActivityCompat.requestPermissions(this, | |
new String[]{ Manifest.permission.ACCESS_FINE_LOCATION }, 100); | |
} else { | |
// パーミッションの許可要求ダイアログを表示 | |
ActivityCompat.requestPermissions(this, | |
new String[]{ Manifest.permission.ACCESS_FINE_LOCATION }, 100); | |
} | |
} else { | |
mMap.setMyLocationEnabled(true); | |
} | |
String httpProxy = System.getProperty("http.proxyHost"); | |
int httpPort = Integer.valueOf(System.getProperty("http.proxyPort")); | |
GeoApiContext context = new GeoApiContext().setApiKey(getString(R.string.google_maps_key)); | |
context.setProxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(httpProxy, httpPort))); | |
try { | |
// 出発点(名古屋駅) | |
com.google.maps.model.LatLng departure = new com.google.maps.model.LatLng(35.17157, 136.884180); | |
// 目的地(豊田市駅)の緯度経度を検索 | |
GeocodingResult[] arrivalResults = GeocodingApi.geocode( | |
context, "豊田市駅").language("ja").await(); | |
com.google.maps.model.LatLng arrival = arrivalResults[0].geometry.location; | |
Log.d("tag", String.format("%s, %f, %f", arrivalResults[0].formattedAddress, arrival.lat, arrival.lng)); | |
// 出発点から目的地間のルート検索 | |
DirectionsResult directionResult = DirectionsApi.newRequest(context) | |
.origin(departure) | |
.destination(arrival) | |
.mode(TravelMode.DRIVING) | |
.language("ja") | |
.await(); | |
List<com.google.maps.model.LatLng> path = directionResult.routes[0].overviewPolyline.decodePath(); | |
// ルート表示 | |
PolylineOptions polylineOptions = new PolylineOptions(); | |
for (com.google.maps.model.LatLng latLng : path) { | |
polylineOptions.add(new LatLng(latLng.lat, latLng.lng)); | |
} | |
// 輪郭線 | |
mMap.addPolyline(polylineOptions.color(0xff000000).width(15)); | |
// 主線 | |
mMap.addPolyline(polylineOptions.color(0xff14a0ff).width(11)); | |
// 目的地マーカーを作成 | |
LatLng arrivalLatLng = new LatLng(path.get(path.size() - 1).lat, path.get(path.size() - 1).lng); | |
mMap.addMarker(new MarkerOptions().position(arrivalLatLng)); | |
// 出発点をズーム表示 | |
LatLng departureLatLng = new LatLng(path.get(0).lat, path.get(0).lng); | |
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(departureLatLng, 15.0f)); | |
} catch (ApiException | IOException | InterruptedException e) { | |
Log.e("tag", e.getMessage()); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Top-level build file where you can add configuration options common to all sub-projects/modules. | |
buildscript { | |
repositories { | |
jcenter() | |
maven { | |
url 'https://dl.bintray.com/cookpad-inc/maven/' | |
} | |
} | |
dependencies { | |
classpath 'com.android.tools.build:gradle:2.3.3' | |
classpath 'info.attosoft.licensetools:license-tools-plugin:0.19.1_r1' | |
// NOTE: Do not place your application dependencies here; they belong | |
// in the individual module build.gradle files | |
} | |
} | |
allprojects { | |
repositories { | |
jcenter() | |
} | |
} | |
task clean(type: Delete) { | |
delete rootProject.buildDir | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://stackoverflow.com/questions/34608517/map-setmylocationenabledtrue-not-working