0

マーカーの設定ボタンを使用しようとすると、マーカーが設定されますが、間違った場所に配置されます。googleMap.setMyLocationEnabled(true) によって提供されるボタンを押すと、地図は正確な位置にある青い点に移動します。なぜ現在の場所を取得し、ランダムな場所を取得していますか?

package com.example.googlemaps;

//import java.io.IOException;
//import java.util.List;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

//import android.location.Address;
import android.location.Criteria;
//import android.location.Geocoder;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
//import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.support.v4.app.FragmentActivity;

public class MainActivity extends FragmentActivity {

    private GoogleMap googleMap;
    double currentLat, currentLong;


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

        Button setLocMarker = (Button)findViewById(R.id.set_marker);
        setLocMarker.setOnClickListener(setMarkerLocation);
    }

    private OnClickListener setMarkerLocation = new OnClickListener(){

        public void onClick(View v) {
            setMarker(currentLat, currentLong);
        }



    };
    private void setUpMapIfNeeded(){
        // Do a null check to confirm that we have not already instantiated the map.
        if(googleMap == null){
            //Try to obtain the map from the SupportMapFragment
            googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
            //check if we were successful in obtaining the map
            if(googleMap != null){
                setUpMap();
            }
        }


    }

    private void setUpMap() {

        getCurrentLocation();

        //set map type
        googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID );

        //creates a LatLng object for current location
        LatLng latLng = new LatLng(currentLat, currentLong);

        //show the current location in Google Map
        googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));

        //Zoom in the Google Map
        googleMap.animateCamera(CameraUpdateFactory.zoomTo(20));
        //setMarker(latitude, longitude);



    }
    public void getCurrentLocation()
    {
        //Enable MyLocation Layer of Google Map
                googleMap.setMyLocationEnabled(true);

                //Get LocationManager object from System Service LOCATION_SERVICE
                LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

                //Create a criteria object to retrieve provider
                Criteria criteria = new Criteria();

                //Get the name of the best provider
                String provider = locationManager.getBestProvider(criteria, true);

                //Get Current Location
                Location myLocation = locationManager.getLastKnownLocation(provider);

                //Get latitude of current location
                currentLat = myLocation.getLatitude();

                //Get longitude of current location
                currentLong = myLocation.getLongitude();
    }
    public void setMarker(double latitude, double longitude){
        googleMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)).title("Your set Marker!"));

    }

//  public void getLocFromAddress(String address){
//      
//      final Geocoder geoCoder = new Geocoder();
//      
//
//      try{
//          List<Address> addresses = geoCoder.getFromLocationName(address, 1);
//          if(addresses.size() > 0){
//              double latitude = addresses.get(0).getLatitude(); 
//              double longitude = addresses.get(0).getLongitude();
//          }
//      }catch(IOException e){
//          
//      }
//      
//      
//      
//  }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


}
4

0 に答える 0