- 구글 맵 api, 구글 direction,구글 distancematrix 적용시켜
터치시 마커가 찍히고 그 마커간 대중교통 시간, 거리 json파싱하여 얻어오기 까지 마침

- 구글 장소 검색을 통해 장소를 검색해 그 사이의 거리를 알아내기 위해 구글 플레이스api를 이용 해보려함.
- 이게 시간이 달라짐에 따라 location에서 안먹히고 places를 따로 디팬던시에 넣어야 된다.
=>
https://stackoverflow.com/questions/38063648/cannot-import-google-places-placepicker-into-android-project
|
In play-services 9.2.0 the places API is no longer located in location . Those are now in their own places dependency. To resolve those you should add this to your build.gradle .
compile 'com.google.android.gms:play-services-places:9.2.0'
It's been answered here |
그래서 적용
* http://www.truiton.com/2015/04/using-new-google-places-api-android/
코드 참고 예제
1. build.gradle 에
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
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:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.google.android.gms:play-services-maps:11.0.4'
compile 'com.google.android.gms:play-services-location:11.0.4'
compile 'com.google.android.gms:play-services-places:11.0.4'
testCompile 'junit:junit:4.12'
compile 'com.google.android.gms:play-services-places:11.0.4'
을 추가한다.
2. 버튼 추가
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="PlacePicker"
android:text="PlacePicker"
android:layout_weight="0.3"/>
3. 버튼 클릭시 Place Picker ui 나옴
private static final int PLACE_PICKER_REQUEST =1;
button3.setOnClickListener(new Button.OnClickListener()
{ @Override
public void onClick(View view)
{
PlacePicker.IntentBuilder intentBuilder = new PlacePicker.IntentBuilder();
try {
Intent intent = intentBuilder.build(MainActivity.this);
startActivityForResult(intent,PLACE_PICKER_REQUEST);
} catch (GooglePlayServicesRepairableException e) {
e.printStackTrace();
} catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
}
}
});
4. 실행 결과

창은 뜨지만 결과를 받아오질 못하고 검색이 되지 않는다.
=>> 받은 구글 api key 를 끝에 띄어쓰기 하나를 모르고 지우지 않았었다.

api키 제대로 하니 검색이 잘된다.
5. 위의 intent 실행의 결과를 받아올수 있게 onActivityResult로 함수를 만들어 가져온다.
Intent intent = intentBuilder.build(MainActivity.this);
startActivityForResult(intent,PLACE_PICKER_REQUEST);
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if(requestCode == PLACE_PICKER_REQUEST && resultCode == Activity.RESULT_OK)
{
final Place place = PlacePicker.getPlace(this, data);
final CharSequence name = place.getName();
final CharSequence address = place.getAddress();
String attributions = (String) place.getAttributions();
if (attributions == null) {
attributions = "";
}
tv.setText("");
tv.append("name : " + name);
tv.append("address : " + address+"\n");
tv.append(Html.fromHtml(attributions));
}
else {
super.onActivityResult(requestCode, resultCode, data);
}
}
값을 받은후 textview에 넣어준다.
private static final LatLngBounds BOUNDS_VIEW = new LatLngBounds(
new LatLng(37.56, 126.98), new LatLng(37.57, 127.02));
처음 시작 위치를 설정해준다.
intentBuilder.setLatLngBounds(BOUNDS_VIEW);
6. 찾은 장소에 마커추가하기
pickMark(map,place.getLatLng());
tv.setText("");
tv.append("name : " + name + "\n");
tv.append("address : " + address+"\n");
//tv.append(Html.fromHtml(attributions));
map.moveCamera(CameraUpdateFactory.newLatLng(place.getLatLng()));
map.animateCamera(CameraUpdateFactory.zoomTo(13));
place에서 좌표 얻어와 마크 찍고 그쪽으로 카메라이동.
*결과창


1. 장소 찾아서 선택 후 마커가 찍히고
2. 다른 마커를 맵에서 터치로 선택후
3. 순서대로 경로를 그려주고
4. 거리와 시간(대중교통) 표시
https://github.com/littlecold2/googlemap_test