【问题标题】:Location not correctly displaying when trying to set map from lat and long co-ordinates尝试从经纬度坐标设置地图时位置显示不正确
【发布时间】:2015-05-24 11:18:17
【问题描述】:

我的意图是从谷歌地图中选择一个位置并将双经度和纬度值发送到另一个意图。在那个活动中,我有一个地图片段,它应该根据我得到的纬度和经度值在地图上显示位置。我正确地获得了活动的双打值,但它没有显示正确的位置。我尝试调试了一整天,但由于没有错误,我似乎找不到我做错了什么。这是我的课

public class Loc extends FragmentActivity{

double latitude;
double longitude;
TextView lat;
TextView lng;
EditText one;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.showmap);
    lat = (TextView) findViewById(R.id.tv1);
    lng = (TextView) findViewById(R.id.tv2);
    one = (EditText) findViewById(R.id.et1);



    // Receiving latitude from MainActivity screen
    double latitude = getIntent().getDoubleExtra("lat", 0);

    // Receiving longitude from MainActivity screen
    double longitude = getIntent().getDoubleExtra("lng", 0);
    String lats = String.valueOf(latitude);
    String lngs = String.valueOf(longitude);
    lat.setText(lats);
    lng.setText(lngs);

    LatLng position = new LatLng(longitude, latitude);

    // Instantiating MarkerOptions class
    MarkerOptions options = new MarkerOptions();

    // Setting position for the MarkerOptions
    options.position(position);

    // Setting title for the MarkerOptions
    options.title("Position");

    // Setting snippet for the MarkerOptions
    options.snippet("Latitude:"+latitude+",Longitude:"+longitude);

    // Getting Reference to SupportMapFragment of activity_map.xml
    SupportMapFragment fm = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);

    // Getting reference to google map
    GoogleMap googleMap = fm.getMap();

    // Adding Marker on the Google Map
    googleMap.addMarker(options);

    // Creating CameraUpdate object for position
    CameraUpdate updatePosition = CameraUpdateFactory.newLatLng(position);

    // Creating CameraUpdate object for zoom
    CameraUpdate updateZoom = CameraUpdateFactory.zoomBy(4);

    // Updating the camera position to the user input latitude and longitude
    googleMap.moveCamera(updatePosition);

    // Applying zoom to the marker position
    googleMap.animateCamera(updateZoom);
}


}

这就是我调用这个类的地方

public class Map extends Activity {


Button ok;
WebView webview;
String q;
TextView add;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.map);
    TextView url = (TextView) findViewById(R.id.tvURL);
    add = (TextView) findViewById(R.id.tvAddress);
    Button done = (Button) findViewById(R.id.btnDone);

    webview = (WebView) findViewById(R.id.webView1);
    webview.setWebViewClient(new WebViewClient());
    webview.getSettings().setJavaScriptEnabled(true);
    //webview.loadUrl("http://maps.google.com/maps?" + "saddr=43.0054446,-87.9678884" + "&daddr=42.9257104,-88.0508355");
    webview.loadUrl("https://goo.gl/maps/Fz0KJ");



    //String url1 = uri.toString();


    done.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            try{
            String q1="";
            String str = webview.getUrl();
            //Uri uri = Uri.parse(str);
            //uri.getQueryParameter(q1);
            ///Log.d("lats",q1);
            String[] part1 = str.split(Pattern.quote("@"));
            String[] part2 = part1[1].split(Pattern.quote(","));

            double lat = Double.parseDouble(part2[0]);
            double lon = Double.parseDouble(part2[1]);
            add.setText(lat+","+lon);


            final Intent yourIntent = new Intent(Map.this, Loc.class);
            yourIntent.putExtra("lat", lat);
            yourIntent.putExtra("lng", lon);
            //setResult(101, yourIntent); 
            startActivity(yourIntent);
            //finish();
            }catch(NullPointerException ex){
                System.out.println (ex.toString());
                Toast.makeText(Map.this, "Slow internet. Please wait till the map loads", Toast.LENGTH_SHORT).show();
            }

        }
    });


}

【问题讨论】:

  • 位置有多大不同?目标和接收到的纬度、经度坐标会很好。
  • 它显示了靠近格陵兰岛的大海。我的经纬度是 1.532528,110.358054。应该是马来西亚砂拉越的斯威本大学
  • 你能不能先Marker marker =googleMap.addMarker(options);然后打印marker.get position()
  • @antonio,我照你说的做了。我得到 90.0 作为长值而不是 110.358054
  • 那么我认为@agamov 是正确的,并且在某些时候您正在更改纬度和经度。最大纬度值为 90,当 110 更大时,它被解释为 90

标签: android google-maps geocoding reverse-geocoding


【解决方案1】:

根据聊天中的cmets和私人对话,解决方案是使用CameraPosition.Builder创建一个CameraPosition,然后使用CameraPosition调用animateCamera()

CameraPosition cameraPosition = new CameraPosition.Builder() 
    .target(position) 
    .zoom(4) // Set your preferred zoom level here
    .build(); 
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

您还可以设置倾斜和方位。看看the documentation

【讨论】:

    【解决方案2】:

    试着反过来改变你的坐标。 这可能是你的情况。至少我总是陷入这种情况:)

    【讨论】:

    • 好主意。但是不,还是一样
    • 显然,标记被放置在正确的位置,但它放大到错误的位置。知道如何纠正这个问题吗?
    猜你喜欢
    • 2011-10-31
    • 1970-01-01
    • 2014-01-07
    • 2012-12-08
    • 1970-01-01
    • 1970-01-01
    • 2014-03-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多