如何在谷歌地图中创建一个新的标记

时间:2016-10-24 08:48:21

标签: java android google-maps google-maps-api-3

我无法创建一个位置为25.080582,121.600000的新标记 当我运行这个应用程序时,它给了我一个错误:

"  java.lang.RuntimeException: Unable to start activity ComponentInfo{bravoideas.google/bravoideas.google.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.MapFragment.getMapAsync(com.google.android.gms.maps.OnMapReadyCallback)' on a null object reference "

有人可以帮我解决问题吗?!

    public class MainActivity extends LocationBaseActivity implements OnMapReadyCallback {

    private WebView webView;
    private Button button;private
    ProgressDialog progressDialog;
    private TextView locationText;       

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        webView = (WebView) findViewById(R.id.webView);
        locationText = (TextView)findViewById(R.id.locationText);
        com.yayandroid.locationmanager.LocationManager.setLogType(LogType.GENERAL);
        getLocation();
        MapFragment mapFragment = (MapFragment) getFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(MainActivity.this);

        WebSettings webSetting = webView.getSettings();
        webSetting.setJavaScriptEnabled(true);
        webSetting.setDisplayZoomControls(true);
        String htmlFilename = "Map.html";
        AssetManager mgr = getBaseContext().getAssets();
        try {
            InputStream in = mgr.open(htmlFilename, AssetManager.ACCESS_BUFFER);
            String htmlContentInStringFormat = StreamToString(in);
            in.close();
            webView.loadDataWithBaseURL(null, htmlContentInStringFormat, "text/html", "utf-8", null);

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

    public LocationConfiguration getLocationConfiguration() {
        return new LocationConfiguration()
                .keepTracking(true)
                .askForGooglePlayServices(true)
                .setMinAccuracy(200.0f)
                .setWaitPeriod(ProviderType.GOOGLE_PLAY_SERVICES, 5 * 1000)
                .setWaitPeriod(ProviderType.GPS, 10 * 1000)
                .setWaitPeriod(ProviderType.NETWORK, 5 * 1000)
                .setGPSMessage("Would you mind to turn GPS on?")
                .setRationalMessage("Gimme the permission!");

    }

    @Override
    public void onLocationChanged(Location location) {
        dismissProgress();
        /*double a = location.getLatitude();*/
        String centerUrl = "javascript:centerAt("+location.getLatitude()+","+location.getLongitude()+")";
        webView.loadUrl(centerUrl);
        setText(location);
    }

    @Override
    public void onLocationFailed(int failType) {
        dismissProgress();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (getLocationManager().isWaitingForLocation()
                && !getLocationManager().isAnyDialogShowing()) {
            displayProgress();
        }
    }

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

    public void onMapReady(GoogleMap map) {
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(
                new LatLng(25.080582,121.600000), 16));

        // You can customize the marker image using images bundled with
        // your app, or dynamically generated bitmaps.
        map.addMarker(new MarkerOptions()
                .anchor(0.0f, 1.0f) // Anchors the marker on the bottom left
                .position(new LatLng(25.080582,121.600000)));
    }

    private void displayProgress() {
        if (progressDialog == null) {
            progressDialog = new ProgressDialog(this);
            progressDialog.getWindow().addFlags(Window.FEATURE_NO_TITLE);
            progressDialog.setMessage("Getting location...");
        }
        if (!progressDialog.isShowing()) {
            progressDialog.show();
        }
    }

    private void dismissProgress() {
        if (progressDialog != null && progressDialog.isShowing()) {
            progressDialog.dismiss();
        }
    }

    private void setText(Location location) {
        String appendValue = location.getLatitude() + ", " + location.getLongitude() + "\n";
        String newValue;
        CharSequence current = locationText.getText();

        if (!TextUtils.isEmpty(current)) {
            newValue = current + appendValue;
        } else {
            newValue = appendValue;
        }
        locationText.setText(newValue);
    }
    public static String StreamToString(InputStream in) throws IOException {
        if(in == null) {
            return "";
        }
        Writer writer = new StringWriter();
        char[] buffer = new char[1024];
        try {
            Reader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
            int n;
            while ((n = reader.read(buffer)) != -1) {
                writer.write(buffer, 0, n);
            }
        } finally {
        }
        return writer.toString();
    }  
}

1 个答案:

答案 0 :(得分:0)

googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.mapFragment)).getMap();

            Marker x=  googleMap.addMarker(new MarkerOptions().position(latlong).title("ICRE Polytechnic").snippet("Gargoti Rd, Gargoti, Maharashtra 416209"));