android Retrieving current position on mapsv2 dynamically

我们两清 提交于 2019-12-13 03:39:47

问题


Well , I have couple of topic related with this issue but still did not got the logic besides nobody provides a clear solution. Anyway, I will try to ask it again clearly and provide a screenshot (from a sample app) hope it would do the magic.

As you can see below there is a custom marker on google maps, I can implement it to my scenario by adding an Imageview. On above of the maps there is a TextView. In there address change dynamically when the markers position change on the maps. BUT we are not drag&drop the marker it is fixed to center and not draggable. What we are doing is just panning the map. When we stop panning the marker is show somewhere on the map and it quickly display as an address in a Textview.

I can change the address by handling onMarkerDragEnd() but this is a different scenario. Also there is no GPS connection I guess it is converting views screenPosition to latitude and longitude using Projection class. I've checked the official site it but I couldnt get the idea how to implement it.

So too sum up, How can I provide a dynamic address change in a TextView by not drag&drop a marker but panning the map?

also here is my code for you too see how I handle the drag method when dragging the marker ends it displays the current address in refer to this code

@Override
public void onMarkerDragEnd(Marker marker) {
    LatLng position=marker.getPosition();

    String filterAddress = "";
    Geocoder geoCoder = new Geocoder(
            getBaseContext(), Locale.getDefault());
    try {
        List<Address> addresses = geoCoder.getFromLocation(
                position.latitude, 
                position.longitude, 1);

        if (addresses.size() > 0) {
            for (int index = 0; 
            index < addresses.get(0).getMaxAddressLineIndex(); index++)
                filterAddress += addresses.get(0).getAddressLine(index) + " ";
        }
    }catch (IOException ex) {        
        ex.printStackTrace();
    }catch (Exception e2) {
        // TODO: handle exception

        e2.printStackTrace();
    }


    TextView myTextView = (TextView) findViewById(R.id.test);
    myTextView.setText("Address " + filterAddress);




    Log.d(getClass().getSimpleName(), String.format("Dragged to %f:%f",
            position.latitude,
            position.longitude));
}


回答1:


once you follow the url which i am providing

http://maps.google.com/maps/api/geocode/json?address=mumbai&sensor=false

which returns data in json format with lat/lng of address.

public static void getLatLongFromAddress(String youraddress) {
    String uri = "http://maps.google.com/maps/api/geocode/json?address=" +
                  youraddress + "&sensor=false"
    HttpGet httpGet = new HttpGet(uri);
    HttpClient client = new DefaultHttpClient();
    HttpResponse response;
    StringBuilder stringBuilder = new StringBuilder();

    try {
        response = client.execute(httpGet);
        HttpEntity entity = response.getEntity();
        InputStream stream = entity.getContent();
        int b;
        while ((b = stream.read()) != -1) {
            stringBuilder.append((char) b);
        }
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    JSONObject jsonObject = new JSONObject();
    try {
        jsonObject = new JSONObject(stringBuilder.toString());

        lng = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
            .getJSONObject("geometry").getJSONObject("location")
            .getDouble("lng");

        lat = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
            .getJSONObject("geometry").getJSONObject("location")
            .getDouble("lat");

        Log.d("latitude", lat);
        Log.d("longitude", lng);
    } catch (JSONException e) {
        e.printStackTrace();
    }

}


来源:https://stackoverflow.com/questions/19407202/android-retrieving-current-position-on-mapsv2-dynamically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!