package com.example.ex18;
import java.net.*;
import java.io.*;
public class DaumAPI {
public static String main(String apiURL,String query) {
try {
String text = URLEncoder.encode(query, "UTF-8");
apiURL += "?query=" + text;
apiURL += "&size=10";
URL url = new URL(apiURL);
HttpURLConnection con = (HttpURLConnection)url.openConnection();
con.setRequestMethod("GET");
//Authorization
con.setRequestProperty("Authorization"," KakaoAK 6baa3500ff42695b48d705aa87132cb3");
int responseCode = con.getResponseCode();
BufferedReader br;
if(responseCode==200) { // 정상 호출
br = new BufferedReader(new InputStreamReader(con.getInputStream()));
} else { // 에러 발생
br = new BufferedReader(new InputStreamReader(con.getErrorStream()));
}
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = br.readLine()) != null) {
response.append(inputLine);
}
br.close();
System.out.println(response.toString());
return response.toString();
} catch (Exception e) {
System.out.println(e);
return e.toString();
}
}
}
package com.example.ex18;
public class LocalVO {
private String name;
private String address;
private String phone;
private double x;
private double y;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("LocalVO{");
sb.append("name='").append(name).append('\'');
sb.append(", address='").append(address).append('\'');
sb.append(", phone='").append(phone).append('\'');
sb.append(", x=").append(x);
sb.append(", y=").append(y);
sb.append('}');
return sb.toString();
}
}
package com.example.ex18;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.LinkAddress;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.ColorRes;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class LocalAdapter extends RecyclerView.Adapter<LocalAdapter.ViewHolder> {
ArrayList<LocalVO> array;
Context context;
public LocalAdapter(ArrayList<LocalVO> array, Context context) {
this.array = array;
this.context = context;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.item,null);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
holder.txtname.setText(array.get(position).getName());
holder.txtaddress.setText(array.get(position).getAddress());
holder.txtphone.setText(array.get(position).getPhone());
//실행중에 넓이,높이를 바꾸고 싶을때
/*LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
holder.item.setLayoutParams(params);
*/
if(position % 2 == 1){
holder.item.setBackgroundColor(Color.parseColor("#A1FFEB3B"));
}else{
holder.item.setBackgroundColor(Color.parseColor("#A48BC34A"));
}
holder.item.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
Intent intent=new Intent(context, MapsActivity.class);
Double lat=array.get(position).getY();
Double lng=array.get(position).getX();
intent.putExtra("lat",lat);
intent.putExtra("lng",lng);
intent.putExtra("name",array.get(position).getAddress());
intent.putExtra("phone",array.get(position).getPhone());
context.startActivity(intent);
return false;
}
});
}
@Override
public int getItemCount() {
return array.size();
}
//viewholder가 생성이될때 아이디를 읽어들인다.
public class ViewHolder extends RecyclerView.ViewHolder {
TextView txtname,txtaddress,txtphone;
RelativeLayout item;
public ViewHolder(@NonNull View itemView) {
super(itemView);
txtname=itemView.findViewById(R.id.txtname);
txtaddress=itemView.findViewById(R.id.txtaddress);
txtphone=itemView.findViewById(R.id.txtphone);
item=itemView.findViewById(R.id.item);
}
}
}
package com.example.ex18;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.LinkAddress;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.ColorRes;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class LocalAdapter extends RecyclerView.Adapter<LocalAdapter.ViewHolder> {
ArrayList<LocalVO> array;
Context context;
public LocalAdapter(ArrayList<LocalVO> array, Context context) {
this.array = array;
this.context = context;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.item,null);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
holder.txtname.setText(array.get(position).getName());
holder.txtaddress.setText(array.get(position).getAddress());
holder.txtphone.setText(array.get(position).getPhone());
//실행중에 넓이,높이를 바꾸고 싶을때
/*LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
holder.item.setLayoutParams(params);
*/
if(position % 2 == 1){
holder.item.setBackgroundColor(Color.parseColor("#A1FFEB3B"));
}else{
holder.item.setBackgroundColor(Color.parseColor("#A48BC34A"));
}
holder.item.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
Intent intent=new Intent(context, MapsActivity.class);
Double lat=array.get(position).getY();
Double lng=array.get(position).getX();
intent.putExtra("lat",lat);
intent.putExtra("lng",lng);
intent.putExtra("name",array.get(position).getAddress());
intent.putExtra("phone",array.get(position).getPhone());
context.startActivity(intent);
return false;
}
});
}
@Override
public int getItemCount() {
return array.size();
}
//viewholder가 생성이될때 아이디를 읽어들인다.
public class ViewHolder extends RecyclerView.ViewHolder {
TextView txtname,txtaddress,txtphone;
RelativeLayout item;
public ViewHolder(@NonNull View itemView) {
super(itemView);
txtname=itemView.findViewById(R.id.txtname);
txtaddress=itemView.findViewById(R.id.txtaddress);
txtphone=itemView.findViewById(R.id.txtphone);
item=itemView.findViewById(R.id.item);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edtsearch" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_search"
android:layout_alignParentRight="true"
android:layout_marginTop="3sp"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/edtsearch"
android:layout_marginTop="-2dp"
android:padding="10sp" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#A1FFEB3B"
android:padding="10sp"
android:id="@+id/item">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="상호명"
android:textSize="20sp"
android:id="@+id/txtname"
/>
<TextView
android:layout_below="@id/txtname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="전화번호"
android:textSize="20sp"
android:id="@+id/txtphone"
android:singleLine="true"/>
<TextView
android:layout_below="@id/txtphone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="주소"
android:singleLine="true"
android:textSize="20sp"
android:id="@+id/txtaddress"/>
</RelativeLayout>
values>googel_maps_api.xml>다음 키 등록
MapsActivity.java
package com.example.ex18;
import androidx.fragment.app.FragmentActivity;
import android.content.Intent;
import android.os.Bundle;
import com.google.android.gms.maps.CameraUpdate;
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;
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.
* 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.
*/
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
Intent intent=getIntent();
Double lat=intent.getDoubleExtra("lat",0.0);
Double lng=intent.getDoubleExtra("lng",0.0);
String name=intent.getStringExtra("name");
String phone=intent.getStringExtra("phone");
// Add a marker in Sydney and move the camera
LatLng local = new LatLng(lat, lng);
mMap.addMarker(new MarkerOptions().position(local).title(name).snippet(phone));
mMap.moveCamera(CameraUpdateFactory.newLatLng(local));
mMap.moveCamera(CameraUpdateFactory.zoomTo(15));
}
}
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MapsActivity" />
'Android' 카테고리의 다른 글
네이버 API를 이용한 도서검색,더보기 (2) | 2019.11.15 |
---|---|
네이버API를 이용한 블로그 검색,링크,더보기 (0) | 2019.11.15 |
네이버 API 뉴스 검색 (0) | 2019.11.14 |
주소록 만들기 (0) | 2019.11.14 |
RecyclerView를 이용한 상품목록만들기 (0) | 2019.11.13 |