繁体   English   中英

如何在Google地图中输入的地址上放置标记?

[英]How to place marker on entered address in Google Maps?

字符串“ newAddress”来自用户输入地址的弹出窗口。 p1包含地址的纬度和经度。 放置标记时,标记没有放置在正确的位置。 例如,当输入墨尔本时,标记将放置在厄瓜多尔。 我的代码有什么问题?

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback{

private GoogleMap mMap;
Button addNew;
Context context = this;

@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);

    //BRINGS YOU TO THE POPUP SCREEN
    addNew = (Button) findViewById(R.id.addNew);
    addNew.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MapsActivity.this, AddLocationActivity.class);
            MapsActivity.this.startActivity(intent);
        }
    });
}

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    Intent intent = getIntent();
    String newAddress = intent.getStringExtra("address");

    //THIS IS WHAT HAPPENS WHEN NO ADDRESS IS ENTERED
    if(newAddress == null||newAddress.matches("")) {Toast.makeText(context, "address is null", Toast.LENGTH_LONG).show();}

    //THIS IS WHAT HAPPENS WHEN AN ADDRESS HAS BEEN ENETERED
    else {Toast.makeText(context, newAddress, Toast.LENGTH_LONG).show();

        Geocoder coder = new Geocoder(this);
        List<Address> address;
        LatLng p1 = null;

        try {
            address = coder.getFromLocationName(newAddress,5);
            if (address==null) {
            }
            Address location=address.get(0);
            location.getLatitude();
            location.getLongitude();

            p1 = new LatLng((int) (location.getLatitude() * 1E6),
                    (int) (location.getLongitude() * 1E6));             

            //Turning the coordinates into a string
            TextView coordtext = (TextView)findViewById(R.id.coorText);
            coordtext.setText(onlyCoords);

            double latValue = p1.latitude;
            double longValue = p1.longitude;

            // Add a marker to entered address and move the camera
            LatLng newLocation = new LatLng(latValue, longValue);
            mMap.addMarker(new MarkerOptions().position(newLocation).title(newAddress));
            mMap.moveCamera(CameraUpdateFactory.newLatLng(newLocation));

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
}

我发现我不应该这样做

p1 = new LatLng((int) (location.getLatitude() * 1E6),
                (int) (location.getLongitude() * 1E6)); 

我必须做

p1 = new LatLng((int) (location.getLatitude()),
                (int) (location.getLongitude())); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM