百度地图检索/Retrofit

百度地图检索/Retrofit

地图检索

**自定义类继承Application **

public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        SDKInitializer.initialize(this);
    }
}

Activity

public class MainActivity extends AppCompatActivity {

MapView mMapView;
private BaiduMap mBaiduMap;
private LocationClient mLocationClient;
private PoiSearch mPoiSearch;
Button button;
EditText city;
EditText woer;
LatLngBounds searchBounds;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mMapView = findViewById(R.id.map1);
    city = findViewById(R.id.ed1);
    woer = findViewById(R.id.ed2);
    mBaiduMap=mMapView.getMap();
    mBaiduMap.setMyLocationEnabled(true);

    initMap();
    inttpol();




}

public void bntpol(View view){
    searchBounds = new LatLngBounds.Builder()
            .include(new LatLng( 39.92235, 116.380338 ))
            .include(new LatLng( 39.947246, 116.414977))
            .build();


    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            mPoiSearch.searchInBound(new PoiBoundSearchOption()
                    .bound(searchBounds)
                    .keyword(woer.getText().toString()));
        }
    },300);

}
public void bntzhou(View view){


    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            mPoiSearch.searchNearby(new PoiNearbySearchOption()
                    .location(new LatLng(39.915446, 116.403869))
                    .radius(100)
                    .keyword(woer.getText().toString())
                    .pageNum(10));
        }
    },300);

}


private void inttpol() {

    mPoiSearch = PoiSearch.newInstance();

    mPoiSearch.setOnGetPoiSearchResultListener(listener);

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            mPoiSearch.searchInCity(new PoiCitySearchOption()
                    .city("北京") //必填
                    .keyword("美食") //必填
                    .pageNum(10));
        }
    },300);

}


OnGetPoiSearchResultListener listener = new OnGetPoiSearchResultListener() {
    @Override
    public void onGetPoiResult(PoiResult poiResult) {
        if (poiResult.error == SearchResult.ERRORNO.NO_ERROR) {
            mBaiduMap.clear();

            //创建PoiOverlay对象
            PoiOverlay poiOverlay = new PoiOverlay(mBaiduMap);

            //设置Poi检索数据
            poiOverlay.setData(poiResult);
            //将poiOverlay添加至地图并缩放至合适级别
            poiOverlay.addToMap();
            poiOverlay.zoomToSpan();
        }
    }
    @Override
    public void onGetPoiDetailResult(PoiDetailSearchResult poiDetailSearchResult) {

    }
    @Override
    public void onGetPoiIndoorResult(PoiIndoorResult poiIndoorResult) {

    }
    //废弃
    @Override
    public void onGetPoiDetailResult(PoiDetailResult poiDetailResult) {

    }
};


private void initMap() {
    //定位初始化
    mLocationClient = new LocationClient(this);

    //通过LocationClientOption设置LocationClient相关参数
    LocationClientOption option = new LocationClientOption();
    option.setOpenGps(true); // 打开gps
    option.setCoorType("bd09ll"); // 设置坐标类型
    option.setScanSpan(1000);

//设置locationClientOption
        mLocationClient.setLocOption(option);

    //注册LocationListener监听器
    MyLocationListener myLocationListener = new MyLocationListener();
    mLocationClient.registerLocationListener(myLocationListener);
//开启地图定位图层
        mLocationClient.start();
    }
public class MyLocationListener extends BDAbstractLocationListener {
    @Override
    public void onReceiveLocation(BDLocation location) {
        //mapView 销毁后不在处理新接收的位置
        if (location == null || mMapView == null){
            return;
        }
        MyLocationData locData = new MyLocationData.Builder()
                .accuracy(location.getRadius())
                // 此处设置开发者获取到的方向信息,顺时针0-360
                .direction(location.getDirection()).latitude(location.getLatitude())
                .longitude(location.getLongitude()).build();
        mBaiduMap.setMyLocationData(locData);
    }
}

@Override
protected void onResume() {
    mMapView.onResume();
    super.onResume();
}

@Override
protected void onPause() {
    mMapView.onPause();
    super.onPause();
}

@Override
protected void onDestroy() {
    mLocationClient.stop();
    mBaiduMap.setMyLocationEnabled(false);
    mMapView.onDestroy();
    mPoiSearch.destroy();
    mMapView = null;
    super.onDestroy();
}

}

布局

<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity">

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TextView
        android:textSize="20dp"
        android:text="在"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <EditText

        android:id="@+id/ed1"
        android:hint="北京"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:textSize="20dp"
        android:text="市内找"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <EditText
        android:id="@+id/ed2"
        android:hint="美食"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <Button
        android:onClick="bntpol"
        android:id="@+id/bnt1"
        android:text="POI区域检索"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <Button
        android:onClick="bntzhou"
        android:id="@+id/bnt2"
        android:text="POI周边检索"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

<com.baidu.mapapi.map.MapView
    android:id="@+id/map1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"></com.baidu.mapapi.map.MapView>

效果
在这里插入图片描述

GETRetrofit

定义接口

public interface GetByNet {

    //@get请求后面的声明参数重要拼接到我们的baseurl上,之后就会获取一个完整路径
    @GET("ios/cf/{path}")
    Call<Bean> getdata(@Path(("path"))String path,@Query("stage_id")String stage_id,@Query("limit")String limit,@Query("page")String page);



    @GET("ios/cf/{path}")
    Call<Bean> getdatabyMap(@Path("path")String path, @QueryMap Map<String,String> map);

    @GET
    Call<Bean> getdatabyurl(@Url String url);


}

Activity

public class GetActivity extends AppCompatActivity {

   private String baseUrl="http://www.qubaobei.com/";
   private String Url="http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=10&page=1";


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_get);

    initRetorfit();
    initMap();
    initUrl();

}

private void initUrl() {
    new Retrofit.Builder().baseUrl(baseUrl)
            .addConverterFactory(GsonConverterFactory.create())
            .build().create(GetByNet.class).getdatabyurl(Url)
            .enqueue(new Callback<Bean>() {
                @Override
                public void onResponse(Call<Bean> call, Response<Bean> response) {
                    Log.e("###url",response.body().toString());
                }

                @Override
                public void onFailure(Call<Bean> call, Throwable t) {

                }
            });

}

private void initMap() {
    Map<String,String> map=new HashMap<>();
    map.put("stage_id","1");
    map.put("limit","20");
    map.put("page","1");
    Call<Bean> beanCall = new Retrofit.Builder().baseUrl(baseUrl)
            .addConverterFactory(GsonConverterFactory.create())
            .build().create(GetByNet.class)
            .getdatabyMap("dish_list.php", map);
    beanCall.enqueue(new Callback<Bean>() {
        @Override
        public void onResponse(Call<Bean> call, Response<Bean> response) {
            Log.e("####map",response.body().toString());
        }

        @Override
        public void onFailure(Call<Bean> call, Throwable t) {

        }
    });

}

private void initRetorfit() {
    //获取retorfit的构建者
    Retrofit.Builder builder = new Retrofit.Builder();
    //传入baseurl
    builder.baseUrl(baseUrl);
    //给我们通过网络请求获取转化成我们的javaBean
    builder.addConverterFactory(GsonConverterFactory.create());
    //获取我们Retrofit实例
    Retrofit retrofit = builder.build();
    GetByNet getByNet = retrofit.create(GetByNet.class);

    Call<Bean> getdata = getByNet.getdata("dish_list.php", "1", "20", "1");

    getdata.enqueue(new Callback<Bean>() {
        @Override
        public void onResponse(Call<Bean> call, Response<Bean> response) {
            //获取请求成功后的数据
            Log.e("#####",response.body().toString());
        }

        @Override
        public void onFailure(Call<Bean> call, Throwable t) {
            //请求失败
        }
    });


}
}

PostRetrofit

定义接口

  public interface PostByNet {
        @FormUrlEncoded
        @POST("ios/cf/{path}")
        Call<Bean> getdata(@Path(("path"))String path, @Field("stage_id") String stage_id, @Field("limit") String limit, @Field("page") String page);

@POST("ios/cf/{path}")
Call<Bean> getdatabybody(@Path("path")String path, @Body Bodybean bodybean);

@POST
Call<Bean> geturl(@Url String url);
}

Activity

public class PostActivity extends AppCompatActivity {
    private String baseUrl="http://www.qubaobei.com/";
    private String Url="http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=10&page=1";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_post);
    initRetrofit();
    initBody();
    initurl();

}

private void initurl() {
    new Retrofit.Builder().baseUrl(baseUrl).addConverterFactory(GsonConverterFactory.create())
            .build().create(PostByNet.class).geturl(Url).enqueue(new Callback<Bean>() {
        @Override
        public void onResponse(Call<Bean> call, Response<Bean> response) {
            Log.e("####,posturl",response.body().toString());
        }

        @Override
        public void onFailure(Call<Bean> call, Throwable t) {

        }
    });
}

private void initBody() {
    Bodybean bodybean = new Bodybean();
    bodybean.setStage_id("1");
    bodybean.setLimit("1");
    bodybean.setPage("1");
    new Retrofit.Builder().baseUrl(baseUrl).addConverterFactory(GsonConverterFactory.create())
            .build().create(PostByNet.class).getdatabybody("dish_list.php",bodybean)
            .enqueue(new Callback<Bean>() {
                @Override
                public void onResponse(Call<Bean> call, Response<Bean> response) {
                    Log.e("###postBody",response.body().toString());
                }

                @Override
                public void onFailure(Call<Bean> call, Throwable t) {

                }
            });


}

private void initRetrofit() {
    new Retrofit.Builder().baseUrl(baseUrl).addConverterFactory(GsonConverterFactory.create())
            .build().create(PostByNet.class).getdata("dish_list.php","1","20","1")
            .enqueue(new Callback<Bean>() {
                @Override
                public void onResponse(Call<Bean> call, Response<Bean> response) {
                    Log.e("####post",response.body().toString());
                }

                @Override
                public void onFailure(Call<Bean> call, Throwable t) {

                }
            });
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值